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;
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.
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.
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
@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?
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
}
}
}