Basic LED changer

Hello,

I'm doing a really simple thing with my arduino, the Psuedocode is as follows: (using a tri-color LED)

Push the button once, LED turns on red
push the button again, the LED turns on blue
push the button again, the LED turns on green
push it the final time and it goes back to red

It seems simple but I haven't been able to figure it out.

Can anyone help???

Sure we can.
Show us the sketch that you have so far.
Attach it with the </> icon.

Hi,
I think you mean.

  • Push the button once, RED LED turns ON
  • Push the button again, RED LED turns OFF, BLUE LED turns ON
  • Push the button again, BLUE LED turns OFF, GREEN LED turns ON
  • Push the button again, GREEN LED turns OFF, RED LED turns ON
  • Loop back to 2.

Don't forget you have to command ON and OFFs.

Tom.... :slight_smile:

Yeah TomGeorge, that’s exactly what I mean, but I’m having issues with the syntax part of it, as in what loops to use. Here’s what I’ve been trying so far:

[void setup()
{
pinMode(12, INPUT);//switch
pinMode(9, OUTPUT);//blue
pinMode(10, OUTPUT);//green
pinMode(11, OUTPUT);//red
}

void loop()
{
bool sw1 = digitalRead(12);

if (sw1);
{
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}

if (sw1);
{
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}

if (sw1);
{
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
}

}//end of void loop

Look at the StateChangeDetection example in the IDE to see how to detect when a switch becomes pressed rather than is pressed. When the switch becomes pressed increment a counter. then later in loop() set the LED states according to the current value of the counter.

Thanks a lot, I'll look into it! :slight_smile:

Hi,
When you have your counter setup you could use SwitchCase to set the three different combinations of your LEDS

Tom... :slight_smile:

const byte buttonPin = 2;    // the number of the pushbutton pin
const byte redled = 3, blueled = 4, greenled = 5; // the number of the LED pins


byte ledState1 = LOW, ledState2 = LOW, ledState3 = LOW ;   // the current state of the output pins
byte buttonState =HIGH;             // the current reading from the input pin
byte lastButtonState = LOW;   // the previous reading from the input pin
byte buttonPushCounter = 0;
// the following variables are "long" because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  Serial.begin(9600);

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(redled, OUTPUT);
  pinMode(blueled, OUTPUT);
  pinMode(greenled, OUTPUT);

  // set initial LED state
  digitalWrite(redled, ledState1);
  digitalWrite(blueled, ledState2);
  digitalWrite(greenled, ledState3);
}

void loop() {
  // read the state of the switch into a local variable:
  byte reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from HIGH to LOW),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      
      if (buttonState == LOW) {
        buttonPushCounter++;
        Serial.print(F("Pushes: "));
        Serial.print(buttonPushCounter);
        Serial.print(F(" time(s)"));
      }
      else {
        Serial.println(F("\n"));
        Serial.println(F("Button not pressed"));
      }
    }
  }
  // save the reading.  Next time through the loop,
  lastButtonState = reading;


  if (buttonPushCounter == 1)
  { digitalWrite(redled, HIGH);
    Serial.println(F("redled on"));
  }

  if (buttonPushCounter == 2)
  { digitalWrite(redled, LOW);
    digitalWrite(blueled, HIGH);
    Serial.println(F("redled off / blueled on"));
  }
  if (buttonPushCounter == 3)
  { digitalWrite(blueled, LOW);
    digitalWrite(greenled, HIGH);
    Serial.println(F("blueled off / greenled on"));
  }
  if (buttonPushCounter == 4)
  { digitalWrite(greenled, LOW);
    digitalWrite(redled, HIGH);
    Serial.println(F("greenled off/ redled on"));
  }
  if (buttonPushCounter > 4)
  {
    buttonPushCounter = 2;
  }

}

Digital pin 2 for your button.

arduino pin 2 <----------> one leg of your button ------------the other leg ----arduino ground

(No resistors needed)

int redled = 3, blueled = 4, greenled = 5;

Connect your three color leds to these three pins with three resistors then.

I have fixed the comments and tidied up the code a bit.

Sad to say, I have failed to use % in the code.

The code is working well on my side, tho.

Thanks everyone.

I have shown a bad example of copying and pasting example code. :slight_smile:

There is surely some bad code in some of those examples.

I’d better be careful.

int ledState1 = LOW, ledState2 = LOW, ledState3 = LOW ;   // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int buttonPushCounter = 0;

Why waste memory on ints when bytes will do ?

 if (buttonPushCounter > 4)
  {
    buttonPushCounter = 2;
  }

Never go back to 1 ?

It is neater to use

buttonPushCounter++;
 buttonPushCounter =  buttonPushCounter % 4;

and use the 0 to 3 values but the if statement will do the job

Hi,

Thanks for your inputs, pal.

Why waste memory on ints when bytes will do ?

int is larger than byte It is true

Actually I copied and pasted stuff from two examples to make this working example.

if (buttonPushCounter > 4)
  {
    buttonPushCounter = 2;
  }


Never go back to 1 ?

when pushcount reaches 4 the redled is on

if i write

buttonPushCounter = 1

I cannot make the leds --- one on the other off and so on

but when it is 4 (redled = on ) I go to 2 to turn it off and the blueled on

it goes smoothly

About %, I have tried it too but I seldom use it so I decided to use this way, which has turned out to be a bad way.

long lastDebounceTime = 0;
Should be:
unsigned long lastDebounceTime = 0;

Would you attach your current sketch using the </> icon in the posting menu.

Thanks, Larry.

That part is from the example, too.

Guess there is some bad code in some of the examples.

unsigned long ( no negative numbers) sounds logical.

Thanks

Thank you everyone, I got it working!

siutoejai, I used your code and just re-defined all the inputs and outputs for my Arduino.

Thanks again!

:slight_smile:

Congratulations!