I'm trying to program a button to turn on three leds one by one. I've only managed to write code so that 2 turn on and they are turning on together. I know I've done something very wrong with the boolean function and there's probably a much better way to do it as well.
boolean currentled;
int ledPin =3;
boolean last;
void setup() {
pinMode(2, INPUT); // set the switch pin to be an input
pinMode(3, OUTPUT); // set the yellow LED pin to be an output
pinMode(4, OUTPUT); // set the red LED pin to be an output
pinMode(5, OUTPUT); //set green pin as output
}
boolean led (boolean last)
{
{
boolean last = ledPin;
if (last !=currentled)
{
currentled= ledPin;
}
return currentled;
}
}
void loop() {
// read the switch input:
if (digitalRead(2)==LOW)
{ digitalWrite(3, LOW); // turn on the yellow LED
digitalWrite(4, LOW); // turn off the red LED
digitalWrite (5, LOW);
}
// else if (digitalRead(2) == HIGH && currentled =!led(last)) {
// digitalWrite(3, HIGH); // turn on the yellow LED
// digitalWrite(4, LOW); // turn off the red LED
// digitalWrite (5, LOW);
// }
//
else
if (currentled =led(last) && digitalRead(2)) {
digitalWrite(3, LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
// } else {
digitalWrite(3, LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
void setup() {
pinMode(2, INPUT); // set the switch pin to be an input
pinMode(3, OUTPUT); // set the yellow LED pin to be an output
pinMode(4, OUTPUT); // set the red LED pin to be an output
pinMode(5, OUTPUT); //set green led as output
}
//function to find out if the 3rd led was the last one pressed
boolean led (boolean last)
{
{
boolean last = ledPin;
if (last !=currentled)
{
currentled= ledPin;
}
return currentled;
}
}
void loop() {
// read the switch input:
//if no button pressed then no leds should be on
if (digitalRead(2)==LOW)
{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite (5, LOW);
}
//if pressed then turn on the 3rd led
else {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite (5, LOW);
}
//if the last led that was on was the 3rd then turn on the 5th
while (currentled =led(last)) {
digitalWrite(3, LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
} //else turn on the 4th
digitalWrite(3, LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
Ummm well it looks the same to me but I'm only on my third cup of coffee. Use the code tags (# button) when composing your post, so it comes out looking like this. The Preview button is helpful too.
boolean currentled;
int ledPin = 3;
boolean last;
void setup()
{
pinMode(2, INPUT); // set the switch pin to be an input
pinMode(3, OUTPUT); // set the yellow LED pin to be an output
pinMode(4, OUTPUT); // set the red LED pin to be an output
pinMode(5, OUTPUT); //set green led as output
}
//function to find out if the 3rd led was the last one pressed
boolean led (boolean last)
{
boolean last = ledPin;
if (last != currentled) {
currentled = ledPin;
}
return currentled;
}
void loop() {
// read the switch input:
//if no button pressed then no leds should be on
if (digitalRead(2) == LOW) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
//if pressed then turn on the 3rd led
else {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
//if the last led that was on was the 3rd then turn on the 5th
while (currentled =led(last)) {
digitalWrite(3, LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
} //else turn on the 4th
digitalWrite(3, LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
}
Still not in code tags (# button when posting your message)
Highlight your code then click the # button above the edit window.
As to your code, I think that you have made it more complicated than it needs to be.
I would do it something like this
int buttonPin = 2;
int yellowLed = 3;
int redLed = 4;
int greenLed = 5;
int currentLed = 2;
void setup() {
pinMode(buttonPin, INPUT); // set the switch pin to be an input
pinMode(yellowLed, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLed, OUTPUT); // set the red LED pin to be an output
pinMode(greenLed, OUTPUT); //set green pin as output
}
void loop()
{
if (digitalRead(buttonPin) == LOW)
{
switch (currentLed)
{
case 2: // no LEDs on
digitalWrite(yellowLed, LOW); // turn on the yellow LED
currentLed++;
break;
case 3: //yellow LED is on
digitalWrite(redLed, LOW); // turn on the red LED
currentLed++;
break;
case 4: //red & yellow LEDs are on
digitalWrite(greenLed, LOW); // turn on the green LED
currentLed++;
break;
default: //all 3 LEDs are on
digitalWrite(yellowLed, HIGH); // turn off the yellow LED
digitalWrite(redLed, HIGH); // turn off the red LED
digitalWrite(greenLed, HIGH); // turn off the green LED
currentLed = 2; //reset the counter
}
}
}
NOTE - I have not tested this so it could still be full of holes, but you will see the principle involved
I have assumed that making the LED pins LOW as in your code turns them on, but that depends on how you have them wired
I have also assigned names to the pins used to make the code easier to read.
IMPORTANT NOTE - there is nothing in the code at the moment to debounce the button, so pressing it once may cycle through several states instead of moving on to the next one.