Control 2 led with push button

Hi

i want to control 2led with one pushbutton so when i click the pushbutton the two led turn on and when click the pushbutton again led1 turn off and led2 turn off after 5 sec so please what the code will be

If you find a website where you can just ask people to write code for you for free, please let me know!

Please read this first: How to use this forum

Pieter

Untested code. Written from minimal specs, with lots of assumptions. Use at your own risk.

Assumptions:

  • LEDs are wired to pins 2 and 3, with current limiting resistors.

  • Push button is wired between pin 4 and ground.

  • You don't care if the button is held down for more than 5 seconds. It will immediately turn the LEDs back on.

  • The Arduino doesn't have anything else useful to do. Delays and spin loops are used with no excuses.

const byte led1Pin = 2;                     // Modifiy these to the pins you are using.
const byte led2Pin = 3;
const byte pushButtonPin = 4;               // Using internal pull-up.

void setup() {
  pinMode(ledPin1, OUTPUT);
  digitalWrite(led1Pin, LOW);               // Start the LEDs off
  pinMode(led2Pin, OUTPUT);
  digitalWrite(led2Pin, LOW);
  pinMode(pushButtonPin, INPUT_PULLUP);
}

void loop() {
  while (digitalRead(pushButtonPin) == HIGH);  // Wait for first button press
  digitalWrite(led1Pin, HIGH);                 // Turn on both LEDs
  digitalWrite(led2Pin, HIGH);
  delay(20);                                   // debounce

  while (digitalRead(pushButtonPin) == LOW);   // Wait for first button release
  delay(20);                                   // debounce

  while (digitalRead(pushButtonPin) == HIGH);  // Wait for second button press
  digitalWrite(led1Pin, LOW);                  // Turn off first LED
  delay(5000);                                 // Wait 5 seconds
  digitalWrite(led2pin, LOW);                  // Turn off second LED
}