Pushbutton Sequencer

Hi all,

Hoping someone can chime in on a project I am attempting at here.

What I have is a pushbutton sketch that I found here. I would like to add 3 LEDs and two buttons.

Push button1 and LED 1 lights for one second then turns off.
Push button1 again LED 2 lights for one second then turns off.
Push button1 again LED 3 lights for one second then turns off.

And then the reverse sequence with button2 for LED3, 2, and 1.

I already have my pullup circuits thinking tha I would find a working example;

I am having trouble with the language in putting it all together. I guess by now you can tell I am knew to the coding. I’ve been searched all day for a proper example but with no good results.

Any help would be greatly appreciated. Thanks

PushbuttonSeq.ino (1.29 KB)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Thanks you very much for the friendly reminder Tom;

Shall we begin again?

Hi! Can you please help me with my code? I attach a copy below. This code allows the led to respond to the button when pushed, but after the led turns off I would like the next led to do the same thing with the same button.

In my head its seems really simple but applying it is the sweet challenge for me.

I am using an Arduino Uno and would like to control 3 LEDs in sequence with two buttons.

Push button 1 and LED 1 lights for one second then turns off. Push the same button again and LED 2 lights for one second then turns off and then the same thing happens to LED 3 after button 1 is pushed.

Then I would like to reverse the sequence with button 2 for all three LEDs .

I am having trouble with the language in putting it all together. I guess by now you can tell I am newbie. I’ve been searched for a proper example but with no good results.

Any help would be greatly appreciated. Thanks

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 4, when pressing a push button attached to pin 2.


 The circuit:
 * LED attached from pin 4 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 4.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  4;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Posting an unchanged example of “light an led while button down” shows no effort on your side.

Where does a led blink for a second?

Where are the three leds?

Where are the two buttons?

But never mind, you seem to be the copy and paste type, so here you have your example

#include <Bounce2.h>

const byte ledPin[3] = { 10, 9, 8 }; // each to resistor - led - GND
const byte noOfLeds = sizeof(ledPin);
const byte upPin = 7;   // button closing to GND
const byte downPin = 6; // button closing to GND
const unsigned int duration = 1000;

Bounce bUp;
Bounce bDown;

byte whichLed = noOfLeds;
bool blinkOnce = false;
unsigned long blinkStart;

void setup() {
  for (byte i = 0; i < noOfLeds; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
  bUp.attach(upPin, INPUT_PULLUP);
  bDown.attach(downPin, INPUT_PULLUP);
}

void loop() {
  if (blinkOnce) {
    if (millis() - blinkStart >= duration) {
      blinkOnce = false;
      digitalWrite(ledPin[whichLed], LOW);
    }
  } else {
    if (bUp.update()) {
      if (bUp.fell()) {
        if (++whichLed >= noOfLeds) {
          whichLed = 0;
        }
        blinkOnce = true;
        blinkStart = millis();
        digitalWrite(ledPin[whichLed], HIGH);
      }
    }
    if (bDown.update()) {
      if (bDown.fell()) {
        if (--whichLed >= noOfLeds) {
          whichLed = noOfLeds - 1;
        }
        blinkOnce = true;
        blinkStart = millis();
        digitalWrite(ledPin[whichLed], HIGH);
      }
    }
  }
}

Whandall:
Posting an unchanged example of “light an led while button down” shows no effort on your side.

Where does a led blink for a second?

Where are the three leds?

Where are the two buttons?

But never mind, you seem to be the copy and paste type, so here you have your example

#include <Bounce2.h>

const byte ledPin[3] = { 10, 9, 8 }; // each to resistor - led - GND
const byte noOfLeds = sizeof(ledPin);
const byte upPin = 7;  // button closing to GND
const byte downPin = 6; // button closing to GND
const unsigned int duration = 1000;

Bounce bUp;
Bounce bDown;

byte whichLed = noOfLeds;
bool blinkOnce = false;
unsigned long blinkStart;

void setup() {
  for (byte i = 0; i < noOfLeds; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
  bUp.attach(upPin, INPUT_PULLUP);
  bDown.attach(downPin, INPUT_PULLUP);
}

void loop() {
  if (blinkOnce) {
    if (millis() - blinkStart >= duration) {
      blinkOnce = false;
      digitalWrite(ledPin[whichLed], LOW);
    }
  } else {
    if (bUp.update()) {
      if (bUp.fell()) {
        if (++whichLed >= noOfLeds) {
          whichLed = 0;
        }
        blinkOnce = true;
        blinkStart = millis();
        digitalWrite(ledPin[whichLed], HIGH);
      }
    }
    if (bDown.update()) {
      if (bDown.fell()) {
        if (–whichLed >= noOfLeds) {
          whichLed = noOfLeds - 1;
        }
        blinkOnce = true;
        blinkStart = millis();
        digitalWrite(ledPin[whichLed], HIGH);
      }
    }
  }
}

Thank you very much for your response Whandall.

As stated earlier I am new to the coding world, I’ve been watching the lots of videos and reading literature to get up to speed , however I would try me best to decipher your example you suggested.

The code I uploaded was an example to show the type of push button and led reaction I was looking for, it is not one of my sketches. As for the led blinking for one second, again that would be a part of my preference.

I attempted to modify it but it was not pretty at all.

On my Arduino Uno the Push button up in located on pin2 and down is on pin 3.

The three Leds are on 4, 5, and 6.

THuan:
I attempted to modify it but it was not pretty at all.

In which case you should have posted your attempt.

You will learn a lot more from our comments on your attempt.

THuan:
The three Leds are on 4, 5, and 6.

Do you need a full example code for the programming logic you described?

If I'd do the programming, I'd possibly NOT use wrong LED pin numbers like;
const byte ledPin[3] = { 10, 9, 8 }
but perhaps something like that using correct pin numbers:
const byte ledPins = { 4,5,6}

BTW: You cannot use same pin numbers for button input and for LED output.

Each LED pin (and and button pin must be unique number, only used once!

So please THINK and make up your mind:
Which are the three pin numbers for the LEDs and which two pin numbers are for the buttons?

I love this forum, your responses allow me to learn more as I dig deeper. Thanks again.

Below you would find my code. I just cant seem to implement the desired language for proper execution.

As you can see I reassigned the pins to what is preferred.

Button up pin6
Button down pin 7
Led 8, 9, 10.

At start the 1st Led is on, I preferred it to be off until I push the up button 6 for ledPin 8 to turn on and then turn off after 1 second.

Then when I push that same button again it would light ledPin 9 for 1 second and turn off.
Same goes for ledPin 10

And then the reverse order with the down button.

The code as you can see lights each led from 8 - 10 but stays high through each up and down cycle.

int sequence = 1;
// output pins for led
int button = 6;     // button up
int button1 = 7;     // button down 
int ledA = 8; 
int ledB = 9;
int ledC = 10;

// input pins for up down /  with pullup resistors
int sequenceup = 7;
int sequencedown = 6;
   
void setup(){  
pinMode(ledA, 1);
pinMode(ledB, 1);
pinMode(ledC, 1);
pinMode(sequenceup,0);
pinMode(sequencedown,0);
}

void loop(){
  if(sequence == 1){
digitalWrite(ledA,1);
digitalWrite(ledB,0);
digitalWrite(ledC,0);

}
if(sequence == 2){
digitalWrite(ledA,0);
digitalWrite(ledB,1);
digitalWrite(ledC,0);
}
if(sequence == 3){
digitalWrite(ledA,0);
digitalWrite(ledB,0);
digitalWrite(ledC,3);
}

//takes care of sequence switching

delay(250);//delay to prevent going through sequence too quick from holding the button or pressing too long. But I would prefer this to not happen, even if I hold the button continuously.

sequence += digitalRead(sequencedown) - digitalRead(sequenceup); // non debounced! But may not be a problem because of the delay by sequence change
if(sequence < 1) sequence = 1;
if(sequence > 3) sequence = 3;
//limits to actual sequence


}

Please use the predefined constants; it makes it a lot easier to understand. Change

void setup()
{
  pinMode(ledA, 1);
  pinMode(ledB, 1);
  pinMode(ledC, 1);
  pinMode(sequenceup, 0);
  pinMode(sequencedown, 0);
}

to

void setup()
{
  pinMode(ledA, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(ledC, OUTPUT);
  pinMode(sequenceup, INPUT);
  pinMode(sequencedown, INPUT);
}

Next you mention pull-up resistors in your comments. Do you use external pull-ups? If not, you need to change the last two statements to

  pinMode(sequenceup, INPUT_PULLUP);
  pinMode(sequencedown, INPUT_PULLUP);

Also, please use the predefined constants for the digitalWrite in your code; 1 would be HIGH and 0 would be LOW. e.g.

  if (sequence == 3)
  {
    digitalWrite(ledA, LOW);
    digitalWrite(ledB, LOW);
    digitalWrite(ledC, HIGH);
  }

Next your code always will execute sequence 1, regardless if a button is pressed or not. Your code also repeats the selected sequence. Are both the intention?

  delay(250);//delay to prevent going through sequence too quick from holding the button or pressing too long. But I would prefer this to not happen, even if I hold the button continuously.

Based on the last part of the comment, you need to have a look at the state-change-detection example that comes with the IDE. You want to react on the falling edge (when the button becomes pressed, the signal goes from HIGH to LOW) and do nothing till a rising edge is detected (button is released, signal goes from LOW to HIGH).

The code as you can see lights each led from 8 - 10 but stays high through each up and down cycle.

Because you never write it low again :wink:

ahaa!...

Thanks...