Concept: For this project I wanted to create the flashing lights of police cars. Using the two buttons the user can either turn on the red LED or the blue one. While when both buttons are pressed both LEDs starting flashing rapidly creating the same visual effect as a police car or an ambulance.
Equipment: In this project I have used two red and blue LEDS, two push buttons, 4 resistors. Using the resistors is important not to just protect the LEDS but also they are necessary to protect the Arduino board and avoid creating a short circuit between the button, the pins 3 and 4 and ground.
Here is the design of the circuit:
ARDUINO SCRIPT:
const int ledPin1 = 7; const int ledPin2 = 4; const int buttonPin1 = 2; const int buttonPin2 = 3; const long rapidInterval = 100; // 100ms interval for rapid flashing void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin1, INPUT_PULLUP); pinMode(buttonPin2, INPUT_PULLUP); } void loop() { byte buttonState1 = digitalRead(buttonPin1); byte buttonState2 = digitalRead(buttonPin2); if (buttonState1 == LOW && buttonState2 == LOW) { rapidFlash(); } else { digitalWrite(ledPin1, buttonState1 == LOW ? HIGH : LOW); digitalWrite(ledPin2, buttonState2 == LOW ? HIGH : LOW); } } void rapidFlash() { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); delay(rapidInterval); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); delay(rapidInterval); }