Blink LED with Button

The LED Blinks randomly with a time of 1-5 seconds, the duration changes each time you press the button   Component List:
  • 1 x Arduino Uno R3
  • Jumper Wires
  • 1 x Button
Wiring Guide:
  1. Connect the Button:
    • Connect one side of the button to digital pin 7 on the Arduino.
    • Connect the other side to the GND pin on the Arduino (or breadboard).
    CODE:
  int button=0; //Button
  int delayTime=1000;

  void setup()
    {
    pinMode(LED_BUILTIN, OUTPUT); //Set the Builtin LED as an Output
    pinMode(7,INPUT_PULLUP); //Usually you need to use a resistor to make the button not trigger uncontrollably. However you can also use PullUp feature instead
    }

void loop() {

  button=digitalRead(7); //Read the button status, 1 is open, 0 is pressed

  if(button==0) //check if the button is pressed
  {
    delayTime=random(1000,5001); //Generate a random number between 1000 and 5000
    Serial.println("Button press detected");
  }

  else //if the button is not pressed, then continue to use the delayTime generated
  {
    digitalWrite(LED_BUILTIN, HIGH); //Turn on the LED
    delay(delayTime); //Set Delay
    digitalWrite(LED_BUILTIN, LOW); //Turn off the LED
    delay(delayTime); //Set Delay
  }

}
 
Looking to build this project? Check out our custom, Bahraini Assembled, starter kit here!https://www.bitware.store/product/arduino-uno-r3-compatible-with-arduino-ide/

[bitware_arabic_portfolio_content]

Project Details

Level: Beginner (1/5)
Category: Basics
Platform: Arduino
Creator: BitWare Store