Need help LED Toggle between red and green!

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() 
  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);
  if (LEDValue == 1); {
  digitalWrite(RedLED, HIGH);
  if (LEDValue != 1); {
    digitalWrite(RedLED, LOW);


  while (digitalRead(buttonPin) == false); {


Take a look at the Arduino State Machine example, as that is the basic concept that you need.


Okay Thanks

You are not debouncing your button, so will toggle an indeterminate number of times on each press.

You CAN do it in code; but its much easier AND better just to add a small capacitor across the button.

Check my page here