1

I'm doing a project using Arduino Uno R3 and I'm trying to use two buttons to change LED state between HIGH and LOW. I have already a code but someting its not right because the LED should be starting in LOW state but it's starting in HIGH state. I checked the circuit and its all good none of the components is connected in wrong place or not connected corretly and the resistors I used are the right ones.

Here is the code I'm using:

 int ledpin = 13;
 int pushbutton1 = 8;
 int pushbutton2 = 7;
 boolean lastbuttonstate1 = LOW;
 boolean lastbuttonstate2 = LOW;

void setup() 
{ 
  pinMode(ledpin,OUTPUT);
  pinMode(pushbutton1,INPUT);
  pinMode(pushbutton2,INPUT);
}

void loop() {


static bool ledState = false; // LED is off by default
 if ((digitalRead(pushbutton1) == LOW) && !ledState) { // 1 pressed & led is off
    digitalWrite(ledpin, HIGH);
    ledState = true;
 }
  if ((digitalRead(pushbutton2) == LOW) && ledState) { // 2 pressed & led is on
    digitalWrite(ledpin, LOW);
    ledState = false;
  }
}

3 Answers 3

1

You need some PULLUP there - either external resistors or INPUT_PULLUP settings, otherwise disconnected pin have not defined valou (so it can be LOW even if button is not pressed.

3
  • I tryed the INPUT_PULLUP and it worked but now the LED state turns LOW when I stop pressing the button and the other button does the same thing.
    – BassMaster
    Commented Jan 11, 2017 at 13:30
  • I tried that on my arduino and it works - when I press button1, LED goes ON when I release it, the LED is still ON. When I press button 2, LED goes OFF and stays OFF even when I release the button. I would check all your connections if they are reliable (do not lose contact when you manipulate the buttons - I have universal shield with LEDS and buttons soldered fast - I had recently some problems with some breadboards) and if the led is on pin 13(the build in is), while buttons are between pins 7,8 and ground. (and that the script uploaded correctly)
    – gilhad
    Commented Jan 11, 2017 at 16:08
  • Also note, that your loop can be as simple as: loop() { if ( ! digitalRead(pushbutton1) ) digitalWrite(ledpin, HIGH); if ( ! digitalRead(pushbutton2) ) digitalWrite(ledpin, LOW); }
    – gilhad
    Commented Jan 11, 2017 at 16:09
2

This is an excellent tutorial for exactly that: http://www.instructables.com/id/How-to-use-a-Push-Button-Arduino-Tutorial/

Its a bit hard to give you a specific answer without seeing you circuit. but as a start, i would set the ledPin to LOW in the setup routine.

void setup() 
{ 
  pinMode(ledpin,OUTPUT);
  pinMode(pushbutton1,INPUT);
  pinMode(pushbutton2,INPUT);

  //initialize the pin to low
  digitalWrite(ledpin,LOW);

}
1

I think you have just minor changes. First of all, you haven't applied pullup. And here, what is the use of boolean lastbuttonstate1 = LOW; and boolean lastbuttonstate2 = LOW;? I think it not useful for the code so I remove it. And always initialize flag or something in starting of the code means globally. I update your code, just check it out. It might be helpful.

int ledpin = 13;
int pushbutton1 = 8;
int pushbutton2 = 7;

static bool ledState = false; // LED is off by default

void setup() 
{ 
  pinMode(ledpin,OUTPUT);

  pinMode(pushbutton1,INPUT_PULLUP); // Provide pullup
  pinMode(pushbutton2,INPUT_PULLUP); // Provide pullup
}

void loop()
{
  if((digitalRead(pushbutton1) == LOW) && !ledState) // 1 pressed & led is off
  { 
    digitalWrite(ledpin, HIGH);
    ledState = true;
  }
  if((digitalRead(pushbutton2) == LOW) && ledState) // 2 pressed & led is on
  {         
    digitalWrite(ledpin, LOW);
    ledState = false;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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