3-Way Switch, Lighting two LED's Opposite each other

Hello all,

I am trying to figure out how to code what I thought would be relatively simple, but maybe I'm missing something. Hoping someone may be able to help.

I have 2 momentary (NO) push button switches that control a green LED and a red LED. (3-way switch type setup)

My goal is that when I push either button once, if the red is ON, the red will then turn OFF and the green will turn ON. when the button is pressed again, the green will turn OFF and the red will turn ON.

I have been basing my approach off of the switch circuit in the Arduino tutorials.
I can successfully make the red LED turn on and off with the push of a button, I cannot figure out how to make the green LED do the opposite of the red. maybe I'm going in the complete wrong direction with it, maybe not. I just don't know, hoping someone with more knowledge that me would be able to point me into the right direction. I have searched all over the internet and the forum to see if I can find anything that would point me in the right direction, maybe i'm missing it.

I greatly appreciate any advice and wisdom anyone give me.

Thank You in advance!

Below is the Arduino example code, I cannot figure out how to make a green LED, do the opposite of the red LED. so for this sake outPin 13 would be the red and a green can be added. My code is garbage so I figure this is the cleanest point to base any help off of.

(TUTORIAL ARDUINO CODE)
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin

int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers

void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(outPin, state);

previous = reading;
}

So state indicates on or off.

Add a Serial.begin to setup() and print the value of state in loop(). Below a demo that only uses setup() and shows how you can use the inverse value of state.

void setup()
{
  Serial.begin(57600);

  int state = LOW;
  Serial.print("state = "); Serial.println(state);
  Serial.print("inverse state = "); Serial.println(!state);

  state = HIGH;
  Serial.print("state = "); Serial.println(state);
  Serial.print("inverse state = "); Serial.println(!state);

}

void loop()
{
}

Up to you to use the inverse value in a second digitalWrite

Note
Please read How to use this forum - please read., specifically point #7 about posting code.

How about something like:

digitalWrite(REDoutPin, (state==HIGH)?HIGH:LOW);
digitalWrite(GREENoutPin, (state==HIGH)?LOW:HIGH);

You could use "!state" for the 2nd LED if you're sure "HIGH" and "LOW" are '1' and '0' respectively.

You probably have to explain your code in a little more detail to OP :wink: