Hi folks as a beginner I am planning to make a start light simulation by using 3 push buttons and nrf24l01 modules. I am having some issues with the pushbutton code implementation. when the button for red pressed for one time I want it to lit constantly. then I press for green and it should lit the same way. Finally when i push the reset all leds should go off. You can see the diagram and code but code is not working properly.
What could be the problem? Appreciate for any help.
const int buttonPin_one = 1;
const int ledPin_one = 13;
const int buttonPin_two=3;
const int ledPin_two=11;
const int button_reset=5;
int buttonPushCounter_one = 0;
int buttonState_one = 0;
int lastButtonState_one = 0;
int buttonPushCounter_two = 0;
int buttonState_two = 0;
int lastButtonState_two = 0;
//int buttonPushCounter_three = 0;
boolean buttonState_three = false;
boolean lastButtonState_three = false;
int state=0;
void setup() {
pinMode(buttonPin_one, INPUT_PULLUP);
pinMode(ledPin_one, OUTPUT);
pinMode(buttonPin_two,INPUT_PULLUP);
pinMode(ledPin_two, OUTPUT);
pinMode(button_reset,INPUT);
}
void loop()
{
buttonState_one = digitalRead(buttonPin_one);
buttonState_two = digitalRead(buttonPin_two);
buttonState_three=digitalRead(button_reset);
///////////////////////////////////////////////////////////////
if (buttonState_one != lastButtonState_one)
{
if (buttonState_one == HIGH)
{
buttonPushCounter_one++;
}
delay(20);
}
lastButtonState_one = buttonState_one;
if (buttonPushCounter_one % 2 == 0)
{
digitalWrite(ledPin_one, HIGH);
}
////////////////////////////////////////////////////////////////
if (buttonState_two != lastButtonState_two)
{
if (buttonState_two == HIGH)
{
buttonPushCounter_two++;
}
delay(20);
}
lastButtonState_two = buttonState_two;
if (buttonPushCounter_two % 2 == 0)
{
digitalWrite(ledPin_two, HIGH);
}
///////////////////////////////////////////////////////////////////
if (buttonState_three != lastButtonState_three) {
if (buttonState_three == HIGH) {
digitalWrite(ledPin_two, LOW);
digitalWrite(ledPin_one, LOW);
}
delay(50);
}
lastButtonState_three = buttonState_three;
}
traffic_light.ino (2.16 KB)