Pages: [1]   Go Down
Author Topic: 3 leds one button  (Read 977 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2395
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read the post at the top of this forum. Use code tags and auto format your code before posting. The look up "scope".

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry. Hope this is clearer.


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

}




Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4094
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry. Hope this is clearer.

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.

Code:
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);
}

Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4398
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 650
Posts: 50838
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (currentled =led(last))
= is an assignment operator. == is the equality operator.

Why are you using a while statement here?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the code. It's working better.
I'm not sure about the while statement. I was just trying different things since I'm new to coding.
Logged

Pages: [1]   Go Up
Jump to: