Go Down

Topic: LED Blink with push button (Read 696 times) previous topic - next topic

ranjan131

Dec 13, 2018, 01:22 am Last Edit: Dec 16, 2018, 03:21 pm by ranjan131
Hi everybody,
   I have a project and want to code when I peressed a button  LED blink 3 time in given time dealy and stop automatically
And I want to control same LED with the help of 4 push buttons. With different timings.
Thnx

sibin_lal

Code: [Select]
/*
  Button

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

  The circuit:
  - LED attached from pin 13 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 13.

  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 buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int buttonPin3 = 4;     // the number of the pushbutton pin
const int buttonPin4 = 5;     // the number of the pushbutton pin


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

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status
int buttonState3 = 0;         // variable for reading the pushbutton status
int buttonState4 = 0;         // variable for reading the pushbutton status

int delay1 = 500; // delay for 1st led
int delay2 = 1000; // delay for 1st led
int delay3 = 1500; // delay for 1st led
int delay4 = 2000; // delay for 1st led
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {

    // 1st blink
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(delay1);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay1);

    // 2nd blink
    digitalWrite(ledPin, HIGH);
    delay(delay1);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay1);

    // 3rd blink
    digitalWrite(ledPin, HIGH);
    delay(delay1);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay1);

  }

  else if (buttonState2 == HIGH) {

    // 1st blink
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(delay2);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay2);

    // 2nd blink
    digitalWrite(ledPin, HIGH);
    delay(delay2);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay2);

    // 3rd blink
    digitalWrite(ledPin, HIGH);
    delay(delay2);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay2);

  }

  else if (buttonState3 == HIGH) {

    // 1st blink
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(delay3);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay3);

    // 2nd blink
    digitalWrite(ledPin, HIGH);
    delay(delay3);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay3);

    // 3rd blink
    digitalWrite(ledPin, HIGH);
    delay(delay3);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay3);

  }

  else if (buttonState4 == HIGH) {

    // 1st blink
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(delay4);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay4);

    // 2nd blink
    digitalWrite(ledPin, HIGH);
    delay(delay4);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay4);

    // 3rd blink
    digitalWrite(ledPin, HIGH);
    delay(delay4);
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(delay4);

  }
else
digitalWrite(ledPin, LOW);

}

jackthom41

You need to create different LED Blinking routines and then attach them to each button. Change delays in separate routines. It's quite an easy code to design, I would recommend you to try Proteus and simulate it first. You can download Arduino Library for Proteus.
Technopreneur

Go Up