Using two push buttons to On Off Led

Hi everyone. I have been trying to use 2 push buttons to on and off the LED using Arduino Mega 2560.
I am quite new in this and i really need your kind help in this.

The conditions are;

  1. After clicking 1 time (once) on S1 push button, the led need to be ON and to be continuously ON after the S1 is released.
  2. The LED has to be switched OFF after I press the S2 push button 3 times consecutively, the counter need to be reset to zero.
  3. Repeat step 1 and 2.

Below are the codes I have done, hopefully someone can correct me where i got it wrong.
So far there was no error detected but the LED does not switch OFF after i clicked the S2 button 3 times and above.

'
const int ledPin = 13; // LED connected to digital pin 13
const int switch1Pin = 2; // S1 switch connected to digital pin 2
const int switch2Pin = 3; // S2 switch connected to digital pin 3
int lastswitch1State,lastPin2State;
int buttonPressCount = 0; // Variable to count the number of button presses

void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switch1Pin, INPUT_PULLUP);// Set the S1 switch pin as input with internal pull-up resistor enabled
pinMode(switch2Pin, INPUT_PULLUP);// Set the S2 switch pin as input with internal pull-up resistor enabled
}

void loop()
{
int switch1State = digitalRead(switch1Pin); // Read the state of S1 switch
int switch2State = digitalRead(switch2Pin); // Read the state of S2 switch

if (switch1State == HIGH) { // Check if S1 switch is pressed
digitalWrite(ledPin, HIGH); // Turn on the LED
while (digitalRead(switch1State) == HIGH) {
// Wait until the switch is released
}
}

if (switch2State == HIGH) // Check if S2 switch is pressed
{(buttonPressCount++); // Increment the button press count
if (buttonPressCount >=3 ) ; // If button press count reaches 3
{digitalWrite(ledPin, LOW); // Turn off the LED
buttonPressCount = 0; // Reset the button press count

}

}

}

'

Welcome

Remove the ; after the if

Implement debouncing for both buttons

  1. Done removal.
  2. Not sure how to use it. Can you show me an example if you don't mid, please...

Try the Bounce2 - Arduino Reference library.

@selenz84
When I paste text that looks like code, the system bounces back a message that "hey, that looks like code" and would I like to do something about that.
Has that been your experience?

Thanks but I just started using this today and I dont really understand how to use the bounce library as stated in it. Thank you BTW.

For an easy start use delay(150); or less (but see post #4 @dougp for fix)

And format your code as noted by @runaway_pancake

const int ledPin = 13; // LED connected to digital pin 13
const int switch1Pin = 2; // S1 switch connected to digital pin 2
const int switch2Pin = 3; // S2 switch connected to digital pin 3
int lastswitch1State, lastPin2State;
int buttonPressCount = 0; // Variable to count the number of button presses

void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as output
  pinMode(switch1Pin, INPUT_PULLUP);// Set the S1 switch pin as input with internal pull-up resistor enabled
  pinMode(switch2Pin, INPUT_PULLUP);// Set the S2 switch pin as input with internal pull-up resistor enabled
}

void loop()
{
  int switch1State = digitalRead(switch1Pin); // Read the state of S1 switch
  int switch2State = digitalRead(switch2Pin); // Read the state of S2 switch

  if (switch1State == HIGH) { // Check if S1 switch is pressed

    delay(150); // the easy way to debounce a switch... but learn to use millis()

    digitalWrite(ledPin, HIGH); // Turn on the LED
    while (digitalRead(switch1State) == HIGH) {
      // Wait until the switch is released
    }
  }

  if (switch2State == HIGH)  { // Check if S2 switch is pressed

    delay(150); // the easy way to debounce a switch... but learn to use millis()

    (buttonPressCount++); // Increment the button press count
    if (buttonPressCount >= 3 ) ; // If button press count reaches 3
    { digitalWrite(ledPin, LOW); // Turn off the LED
      buttonPressCount = 0; // Reset the button press count
    }
  }
}
1 Like

@selenz84, what is supposed to happen if you press the first button again before you've run up three presses on the second button?

How do you want to handle pressing the second button before the first has been pressed?

If you don't plan for these, something will happen which may or may not be what you want.

Look at the code @xfpd offers and see if you can predict the behaviour of those cases.

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.