Hello,
I am new with Arduino and I'm having trouble figuring out a code that will make LED lights toggle from red to green (and vice-versa) when a button is pushed.
Thanks!
Brandon
Hello,
I am new with Arduino and I'm having trouble figuring out a code that will make LED lights toggle from red to green (and vice-versa) when a button is pushed.
Thanks!
Brandon
For starters, what kind of LED's are you using? Are they completely separate red and green LED's, or are they bicolor? If they're bicolor, are the red and green opposing polarity or do they share a common anode or cathode?
I'm using tri-color LEDs. These to be exact: LED - RGB Clear Common Cathode - COM-00105 - SparkFun Electronics
I suggest you start with the debounce sketch in the examples that are included with the IDE. Use ledPin to drive the red portion of the LED, and use a second digital output (call it ledGreenPin) to drive the green portion of the LED.
Add this line:
digitalWrite(ledGreenPin, !buttonState);
just after this one:
digitalWrite(ledPin, buttonState);
That should do it for you.
One more thing: it would be a good idea to relocate the original ledPin from digital pin 13 to another digital pin so that the built-in LED on the Arduino pcb does not interfere with the operation of the multicolor one.