I have been trying to program a green LED to turn on at the press of a button, and when I press the button again it turns off AND turns ON a Red LED. (Swapping between green and red LEDs being on at the press of a button)
I have been so close to getting this to work but I seriously CANNOT figure it out.
Any help would be greatly appreciated! Thanks
I have tried to keep the code simple but it might have to be changed a lot. I am fairly new to coding so I'm trying to understand what I've been doing wrong.
int GreenLED = 3;
int RedLED = 4;
int buttonPin = 7;
int toggleState = false;
void setup()
{
Serial.begin(9600);
pinMode(GreenLED, OUTPUT);
pinMode(RedLED, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() // Main loop
{
if (digitalRead(buttonPin) == false) {
toggleState = !toggleState;
digitalWrite(GreenLED, toggleState);
int LEDValue = digitalRead(GreenLED);
Serial.println(LEDValue);
if (LEDValue == 1); {
digitalWrite(RedLED, HIGH);
delay(50);
}
if (LEDValue != 1); {
digitalWrite(RedLED, LOW);
delay(50);
}
}
while (digitalRead(buttonPin) == false); {
delay(50);
}
}