0

I have made a simple Arduino program that uses a potentiometer to dim a LED.

This program also makes a second LED blink.

Finally, I have added a button that controls a third LED; this part of my program does not work: the 3rd LED is always lit as the button always reads HIGH.

Here is the code:

//sensePin is wired up to the FSR
int sensePin = 2;

//this is wired up to an LED
int ledPin = 9;

//Wired up to a button
int inputPin = 1;

int val = 0;

//I was broed so added the default Blink
int ledPinX = 13;

//Wired up to a second LED
int ledPinY = 7;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin, INPUT);
  pinMode(ledPinX, OUTPUT);
  pinMode(ledPinY, OUTPUT);
  val = digitalRead(inputPin);
}

void loop() {
  if (val == HIGH){
    //This should ask "If your button is pressed, then do the code below
    digitalWrite(ledPinY, HIGH);
    //but for some reason the light is always on. I've tried it with more than one button
  }
  else if(val == LOW){
    digitalWrite(ledPinY, LOW);
  }
  int value;

  /*
 The rest of this stuff works. Well, it did before adding in the button. Now the FSR acts as a digital input. Please help.
   */
  value = analogRead(sensePin) / 4;
  analogWrite(ledPin, value);
  digitalWrite(ledPinX, HIGH);
  delay(500);
  digitalWrite(ledPinX, LOW);
  delay(500);
}

What did I do wrong in this code?

EDIT:: diagram and pic at: instagram.com/p/m3AmHWmp4e

1
  • 3
    Could you the part of your circuit showing how the button is wired to the Arduino? – jfpoilpret Apr 16 '14 at 4:54
2

You read the button once, in setup(). The function is not called every time the variable's value is read, only when the line is executed and the variable assigned to. Move the digitalRead() call and assignment into loop(), before you check the variable's value.

4
  • ok that helps a bit but when I run it the ledPinY stays on the entire time instead of responding to the button – SaraMaeBee Apr 16 '14 at 17:22
  • pastebin.com/tBiHX7Zw Ok the button still wont read input and I just revamped the code from scratch just to get the button working then I'll implement sensePin. It compiles fine but always digitalWrites HIGH to ledPinX – SaraMaeBee Apr 16 '14 at 17:29
  • EDIT:: Taking a look at the button example i figured out whree I went wrong. I may just use that and integrate the FSR code EDIT:: It doesnt work either – SaraMaeBee Apr 16 '14 at 17:54
  • ummm.. even witht the button gone and input jumper cables pulled that output is screwed up – SaraMaeBee Apr 16 '14 at 17:58
1

The main problem with your code is that you read the button state only once at the beginning of your program in setup(), so it keeps the initial value forever.

You should modify loop() as follows:

void loop() {
  digitalWrite(ledPinY, digitalRead(inputPin));
  ...
}

You can also remove the val variable, it is totally useless here; then you must also simplify setup():

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin, INPUT);
  pinMode(ledPinX, OUTPUT);
  pinMode(ledPinY, OUTPUT);
  // val = digitalRead(inputPin);
}
1
  • I added the diagram to the original quesiton – SaraMaeBee Apr 17 '14 at 16:39
0

Pin 1 is used for the serial output and can't be used if you enable serial using serial.begin(9600).

Change button to pin 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.