Hello, I am a newbie and I am trying to figure out how to toggle 3 leds with one button. I can do it with two leds, but I have been stuck for many hours trying to do it with 3. I want to turn on only led1 with first button push, turn on led2 and turn off led 1 with second button push, turn on led 3 and turn off led2 with third button push.
int ledPin1 = 11;
int ledPin2 = 10;
int ledPin3 = 9;
int switchPin = 8;
int count = 0;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
//debounce function to stabalize the button
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop() {
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH && count == 0)
{
ledOn = !ledOn;
}
count++;
digitalWrite(ledPin1, ledOn);
if (currentButton == HIGH && count == 1 )
{
ledOn = !ledOn;
}
digitalWrite(ledPin2, ledOn);
count++;
if (currentButton == HIGH && count == 2 )
{
ledOn = !ledOn;
}
digitalWrite(ledPin3, ledOn);
lastButton = currentButton;
count = 0;
}
thanks bunches. I changed it a little because I wanted one light to be on with each click. I still don't have it quite yet. Now one light goes on with a click, then the next click turns it on, then the next click turns the next one on, then the next click turns that one off and so forth. I want it to turn one on with a click, then the next click will turn it off while also turning on the next one. So basically 3 clicks will cycle through all three lights.
int ledPin1 = 11;
int ledPin2 = 10;
int ledPin3 = 9;
int switchPin = 8;
int count = 0;
boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
count = 0;
}
//debounce function to stabilise the button
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop() {
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == false && currentButton == true)
{
if (count == 0)
{
ledOn = !ledOn;
count++;
digitalWrite(ledPin1, ledOn);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
else if (count == 1)
{
ledOn = !ledOn;
count++;
digitalWrite(ledPin3, ledOn);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, LOW);
}
else if (count == 2)
{
ledOn = !ledOn;
count = 0;
digitalWrite(ledPin2, ledOn);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
}
}
}
Luisilva you are a bad ass. It is always something simple right? HAHAHA. That is why I prefer PDE's to programming any day All jokes aside, thank you very much. I bought a few uno boards with the sole purpose of trying to get me nephew interested in positive things, but I have to teach myself the basics before we can jump into some really cool things.
Actually, I did realise that my version did something strange (I tested it, and still have the example running beside me) but that was the consequence of your original design and I thought it would not hurt you to think through it a little more deeply and explain just what you really wanted it to do.
I merely got the cycling function working by fixing your basic "debounce" code which took a little while as I have my own code which approaches it entirely differently and in a far more robust and extensible fashion. It was by all means an interesting exercise.
Paul__B thank you for the push. My programming skills are lacking. I just have a hard time thinking like that sometimes. I am in my junior year in an electrical engineering program, so I should be better at programming, but I am more into math and physics. I took a class programming in C my first year, and a VHDL class. This is very similar to C, so I am just practicing. I am sure that you will see more of my posts asking for help ;). This Arduino community is awesome Thank you very much.