Question

3. Write an Arduino program to do the following function: When a switch connected to digital pin 4 is pressed, three LEDs connected to digital pins 5, 7, and 9 will blink ON and OFF sequentially (i.e. one after the other) with half a second delay between when an LED turns ON and turns OFF.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
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:
- LEDS attached to pin 5,7,9 to ground
- pushbutton attached to pin 4 from +5V
- 10K resistor attached to pin 4 from ground


created 06/12/2018
by marysukanya <marysukanyaatgooglemail.com>
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 buttonPin = 4;     // the number of the pushbutton pin
const int ledPin1 = 5;      // the number of the LED pin
const int ledPin2= 7;      // the number of the LED pin
const int ledPin3= 9;      // the number of the LED pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH)
{
   digitalWrite(ledPin1, HIGH);
delay(500);                  // waits for a second
    digitalWrite(ledPin1, LOW);
  
    digitalWrite(ledPin2, HIGH);
delay(500);                  // waits for a second
    digitalWrite(ledPin2, LOW);

     digitalWrite(ledPin3, HIGH);
delay(500);                  // waits for a second
    digitalWrite(ledPin3, LOW);
  
}
}

/*please do like the work and rate good*/

Add a comment
Know the answer?
Add Answer to:
3. Write an Arduino program to do the following function: When a switch connected to digital...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT