Bike Light project - 1 push button, several LED patterns, how to program!?

Hi everyone

I've been assigned a project to make a bike light, I have one push button and a high powered LED.

I've programmed 3 different basic patterns for the LED to flash to, when the push button is pressed the LED starts flashing the next pattern but it does not stay on that particular pattern. Instead it cycles through all the patterns and returns to the (250)Delay pattern and stays on that.

I'll post my coding next, any advice is appreciated. I'm a beginner and surprised I've already got this far :stuck_out_tongue_closed_eyes: thanks, Emma

const int buttonPin = 2;      // the number of the pushbutton pin
const int ledPin =  7;       // the number of the LED pin

// variables will change:
int ledState = HIGH;            // the current state of the output pin
int buttonState = 0;           // variable for reading the pushbutton status
int lastButtonState = LOW;    // the previous reading from the input pin

//the following variables are unsigned long's because the time, measured in miliseconds
//will quicklt become a igger number than can be storedin 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() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);     // turn the LED on (HIGH is the voltage level)
  delay(150);                         // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(150); 
  digitalWrite(LED_BUILTIN, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(150);                        // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(150);
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(150);                       // wait for a second
  pinMode(LED_BUILTIN, OUTPUT);
  
  //set initial LED state
  digitalWrite(ledPin, ledState);
}

// the loop function runs over and over again forever
void loop(){
// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin); //read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  //check to see if you just pressed the button
  //(i.e.the input went from LOW to HIGH), and you've waited
  //long enough since the last press to ignore any noise:
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(150);


   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 sstate:
  
//If the switch changed, due to noise or pressing;
if(reading != lastButtonState) {
  //reset the debouncing timer
  lastDebounceTime = millis();
  digitalWrite(ledPin, HIGH);     //turn the LED on (HIGH is the voltage level)
  delay(1000);                   //wait for a second
  digitalWrite(ledPin, LOW);    //turn the LED off by making the voltage LOW
  delay(1000);                 //wait for a second
}

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 sstate:


  //if the button state has changed:
  if(reading !=lastButtonState) {
    lastDebounceTime = millis();
  digitalWrite(ledPin, HIGH);     //turn the LED on (HIGH is the voltage level)
  delay(250);                    //wait for a second
  digitalWrite(ledPin, LOW);    //turn the LED off by making the voltage LOW
  delay(250);               //wait for a second
}
  
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) {
    lastDebounceTime = millis();
    digitalWrite(ledPin, HIGH);     //turn the LEDon (HIGH is the voltage level)
    delay(50);                     //wait for a second
    digitalWrite(ledPin, LOW);    //turn the LED off by making the volatage LOW
    delay(50);                   //wait for a second
  }

  //only toggle the LED if the new button state is HIGH
    if (buttonState = HIGH) {
      ledState = !ledState;
    }
  }
 }

//set the LED:
 digitalWrite(ledPin, ledState);


//save the reading. Next time through the loop,
//it'll be the lastButtonState
lastButtonState = reading;
}
}
  1. To not use delay() anywhere.
  2. Learn about state change detection and increment a pattern selector with it
  3. learn about button bounce

You can combine 2 and 3 into using a library like Bounce2. But 1 is a big problem.

And
4) Use the smallest variable type that's still big enough and not just an int for everything

And ...
5). DON'T CROSS-POST

Duplicate thread deleted.

at299:
Hi everyone

I've been assigned a project to make a bike light, I have one push button and a high powered LED.

I've programmed 3 different basic patterns for the LED to flash to, when the push button is pressed the LED starts flashing the next pattern but it does not stay on that particular pattern. Instead it cycles through all the patterns and returns to the (250)Delay pattern and stays on that.

I'll post my coding next, any advice is appreciated. I'm a beginner and surprised I've already got this far :stuck_out_tongue_closed_eyes: thanks, Emma

const int buttonPin = 2;      // the number of the pushbutton pin

const int ledPin =  7;      // the number of the LED pin

// variables will change:
int ledState = HIGH;            // the current state of the output pin
int buttonState = 0;          // variable for reading the pushbutton status
int lastButtonState = LOW;    // the previous reading from the input pin

//the following variables are unsigned long's because the time, measured in miliseconds
//will quicklt become a igger number than can be storedin 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() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(150);                        // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(150);
  digitalWrite(LED_BUILTIN, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(150);                        // wait for a second
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED off by making the voltage LOW
  delay(150);
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(150);                      // wait for a second
  pinMode(LED_BUILTIN, OUTPUT);
 
  //set initial LED state
  digitalWrite(ledPin, ledState);
}

// the loop function runs over and over again forever
void loop(){
// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin); //read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  //check to see if you just pressed the button
  //(i.e.the input went from LOW to HIGH), and you've waited
  //long enough since the last press to ignore any noise:
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(150);

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 sstate:
 
//If the switch changed, due to noise or pressing;
if(reading != lastButtonState) {
  //reset the debouncing timer
  lastDebounceTime = millis();
  digitalWrite(ledPin, HIGH);    //turn the LED on (HIGH is the voltage level)
  delay(1000);                  //wait for a second
  digitalWrite(ledPin, LOW);    //turn the LED off by making the voltage LOW
  delay(1000);                //wait for a second
}

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 sstate:

//if the button state has changed:
  if(reading !=lastButtonState) {
    lastDebounceTime = millis();
  digitalWrite(ledPin, HIGH);    //turn the LED on (HIGH is the voltage level)
  delay(250);                    //wait for a second
  digitalWrite(ledPin, LOW);    //turn the LED off by making the voltage LOW
  delay(250);              //wait for a second
}
 
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) {
    lastDebounceTime = millis();
    digitalWrite(ledPin, HIGH);    //turn the LEDon (HIGH is the voltage level)
    delay(50);                    //wait for a second
    digitalWrite(ledPin, LOW);    //turn the LED off by making the volatage LOW
    delay(50);                  //wait for a second
  }

//only toggle the LED if the new button state is HIGH
    if (buttonState = HIGH) {
      ledState = !ledState;
    }
  }
}

//set the LED:
digitalWrite(ledPin, ledState);

//save the reading. Next time through the loop,
//it'll be the lastButtonState
lastButtonState = reading;
}
}

I made a video recently explaining exactly what you are looking for. You would need to modify very little to get it working.

Here's the link:

Cheers,
Vlad