daskapital
Published © GPL3+

Blinking bicycle light with LED

Push a button to change blinking program of 4 front and 3 back LED lights. ON/BLINK1/BLINK2/OFF/

BeginnerProtip1,086
Blinking bicycle light with LED

Things used in this project

Hardware components

Arduino Nano 33 IoT
Arduino Nano 33 IoT
I used a Arduino Nano copy that is not listed. But I guess the Arduino Nano 33 IoT will do.
×1
High brightness LED white
LED EL 5-7150WW from Everlight
×4
High brightness LED red
×3
Resistor 36 Ohm
×2
Resistor 120 Ohm
×3
Resistor 75 Ohm
×1
Resistor 10 kOhm
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Scheme for blinking light

Scheme for blinking light

Code

Blinking LED light with 4 program modes changed with push button

C#
/*
Program to switch trough 4 program modes by pressing a simple push button. 
Mode 0 = All LEDs turned on
Mode 1 = Blinking LEDs. The LEDs are dividied into two groups. When one group is off, the other LED group is on and opposite.
Mode 2 = All LEDs are blinking fast at the same time.
Mode 3 = All LEDs turned off
  

  The circuit:
  - LED attached from pin 2, 3, 4, 5, 6, 7 to ground
  
  - pushbutton attached from pin 12 to +5V with 10kOhm in between to lower current
  

  created 03 May 2020
  by Thomas Wolters

*/
// constants won't change. They're used here to set pin numbers of button and LEDs:
const int buttonPin = 12;    // the number of the pushbutton pin
const int    ledPin2=  2;     // the number of the LED pin
const int   ledPin3 =  3;     // the number of the LED pin
const int   ledPin4 =  4;     // the number of the LED pin
const int   ledPin5 =  5;     // the number of the LED pin
const int   ledPin6 =  6;     // the number of the LED pin
const int   ledPin7 =  7;     // the number of the LED pin
//
enum ZUSTAENDE:byte {ON, Blink, Blink_fast,OFF};  //Definition of program modes
byte zustand = ON;
// Variables will change:
int  ledState;           // the current state of the output pin
int  ledState2;         // the current state of the output pin
byte buttonState;             // the current reading from the input pin
byte counter = 0;
byte lastcounter =0;
byte Mode = 0;
int  lastButtonState = LOW;// the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, 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

const long interval = 150;           // interval at which to blink at mode 1 (milliseconds)
const long interval_1 = 150;           // interval at which to blink at mode 2 (milliseconds)
unsigned long previousMillis = 0;        // will store last time LED was updated          
void setup() {
  pinMode(buttonPin,INPUT);
  pinMode(ledPin2,  OUTPUT);
  pinMode(ledPin3,  OUTPUT);
  pinMode(ledPin4,  OUTPUT);
  pinMode(ledPin5,  OUTPUT);
  pinMode(ledPin6,  OUTPUT);
  pinMode(ledPin7,  OUTPUT);
 //set initial LED state
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  digitalWrite(ledPin7, HIGH);
}
void loop() {
    // 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:

  // 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;
           // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
  // hier die cases einfügen        ledState = !ledState;
                        counter = counter+1;
                        zustand = counter;
                        if (zustand ==4){
                          zustand =0;
                          }
                        }
    switch (zustand) {
      case ON:
           Mode = 0;
      break;
      case Blink:
           Mode = 1;
      break;
      case Blink_fast:
           Mode = 2;
      break;
         case OFF:
           Mode = 3;
      break;
        }
      }
    }
 if (counter == 4){
 counter =0;
   }
// Second Whattodo
  unsigned long currentMillis = millis();
    switch (Mode) {
      case 0: //Turn light ON
           
          digitalWrite(ledPin2,  HIGH);
          digitalWrite(ledPin3,  HIGH);
          digitalWrite(ledPin4,  HIGH);
          digitalWrite(ledPin5,  HIGH);
          digitalWrite(ledPin6,  HIGH);
          digitalWrite(ledPin7,  HIGH);
             break;
      case 1: //Turn BLINK
           if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
      Serial.print("previousmillis is: ");
  Serial.println(previousMillis);
    Serial.print("currentmillis is: ");
  Serial.println(currentMillis);
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
      ledState2 = LOW;
    } else {
      ledState = LOW;
      ledState2 = HIGH;
    }
    // set the LED with the ledState of the variable:
   digitalWrite (ledPin2, ledState2); //Vorderlicht 1 40mA
   digitalWrite (ledPin3, ledState);  //Vorderlicht 2 40mA
   digitalWrite (ledPin4, ledState2); //Vorderlichter Seite 2xparallel 38mA
   digitalWrite (ledPin5, ledState);  //Rucklicht Haupt 40mA
   digitalWrite (ledPin6, ledState2);  //Rücklicht links 20mA
   digitalWrite (ledPin7, ledState);  //Rücklicht rechts 20mA
    }   
         break;
      case 2: //BlinkFast
 //  unsigned long currentMillis = millis();
          if (currentMillis - previousMillis >= interval_1) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite (ledPin2, ledState);
   digitalWrite (ledPin3,  ledState);
   digitalWrite (ledPin4,  ledState);
   digitalWrite (ledPin5,  ledState);
   digitalWrite (ledPin6,  ledState);
   digitalWrite (ledPin7,  ledState);
           break;
         case 3: //Turn light OFF
          digitalWrite(ledPin2,  LOW);
          digitalWrite(ledPin3,  LOW);
          digitalWrite(ledPin4,  LOW);
          digitalWrite(ledPin5,  LOW);
          digitalWrite(ledPin6,  LOW);
          digitalWrite(ledPin7,  LOW);
      }   
         break;
    }
  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
  lastcounter = counter;
}

Credits

daskapital
0 projects • 0 followers

Comments