dredre

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 }

dredre

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) {

//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);

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 pressedboolean led (boolean last){ boolean last = ledPin; if (last != currentled) { currentled = ledPin; } return currentled;}

//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);}

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}

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 involvedI have assumed that making the LED pins LOW as in your code turns them on, but that depends on how you have them wiredI 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.

Please do not send me PMs asking for help. Post in the forum then everyone will benefit from seeing the questions and answers.