How to output only 1 Serial entry when 2 LEDS change state?

I have 2 LEDS and 1 button hooked up to an arduino. At start Red LED is on and Green LED is off. When the button is clicked LED A turns off and LED B turns on. Then back and forth over and over. I'd like a serial message to display only the current LED that is on. So upon the first click the serial message "Green" will output, then if clicked again "Red" displays. Right now I have "Red Green" displaying. I'm a giant noob to C++ and arduino so I apologize if the code is bad in other ways. I've customized a tutorial I found online.

Here's the code

int led1 = 4;
int led2 = 5;
int bt1 = 8;
boolean bt1g = true;
int bt1t = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(bt1, INPUT_PULLUP);
}

void loop()
{
  if (bt1g) {
    if (digitalRead(bt1) == LOW ) {
      bt1t = (bt1t + 1)%2;
      Serial.println("Red");
      bt1g = false;
    }
  }
  if (!bt1g) {
    if (digitalRead(bt1) == HIGH) {
      Serial.println("Green");
      bt1g = true;
    }
  }

  if(bt1t == 0) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
  }
  if(bt1t == 1) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
  }
  
  delay(15);
}

You do realize you only need a single o/p to have the two LEDs be complimentary.

+5v — green anode/cathode — 220R — o/p pin — 220R — red anode/cathode — GND

LOW on o/p turns green LED on, HIGH turns on red LED.

larryd:
You do realize you only need a single o/p to have the two LEDs be complimentary.

+5v — green anode/cathode — 220R — o/p pin — 220R — red anode/cathode — GND

LOW on o/p turns green LED on, HIGH turns on red LED.

I didn't know this! I may actually end up having 3 LEDs but for now 2 suffices. I'm not quite sure I follow your schematic though. How would that look?

It looks like you're printing "Green" when the button is released. I think where you're currently printing "Red", you need to check bt1t and print red or green based on its value.

Similar to this:

You are also way over complicating your code. Have a look at the StateChangeDetection Example

const int ledRedPin = 4;
const int ledGreenPin = 5;
const int buttonPin = 8;
int buttonState = 0;
int lastButtonState = 1;

void setup()
{
  Serial.begin(9600);
  pinMode(ledRedPin, OUTPUT);
  pinMode(ledGreenPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    lastButtonState = buttonState;
    if (buttonState == LOW ) {
      Serial.println("Red");
      digitalWrite(ledRedPin, HIGH);
      digitalWrite(ledGreenPin, LOW);
    } else {
      Serial.println("Green");
      digitalWrite(ledRedPin, LOW);
      digitalWrite(ledGreenPin, HIGH);
    }
  }
}

blh64:
You are also way over complicating your code. Have a look at the StateChangeDetection Example

const int ledRedPin = 4;

const int ledGreenPin = 5;
const int buttonPin = 8;
int buttonState = 0;
int lastButtonState = 1;

void setup()
{
  Serial.begin(9600);
  pinMode(ledRedPin, OUTPUT);
  pinMode(ledGreenPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {
    lastButtonState = buttonState;
    if (buttonState == LOW ) {
      Serial.println("Red");
      digitalWrite(ledRedPin, HIGH);
      digitalWrite(ledGreenPin, LOW);
    } else {
      Serial.println("Green");
      digitalWrite(ledRedPin, LOW);
      digitalWrite(ledGreenPin, HIGH);
    }
  }
}

That code doesn't seem to work. It makes it so when I hold down the button it changes but when I release it goes back to it's original state. I'd like it so by default its Red-On and Green-Off then I click and release it is now Red-Off and Green-On.

I figured it out. Simply changed it to show whatever the button counter was and removed the line about serial Green. For anyone curious here is the new code

void loop()
{
  if (bt1g) {
    if (digitalRead(bt1) == LOW ) {
      bt1t = (bt1t + 1)%2;
      Serial.print("Status");
      Serial.println(bt1t);
      bt1g = false;
    }
  }

This code

 bt1t = (bt1t + 1)%2;

is an overly complex way to express

 bt1t = !bt1t;