Description: A row of LEDs light up in sequence one by one and then stay ON while the limit switch is held down, but when the limit switch is released the LEDs turn OFF in sequence one by one.
Supplies:
1 – Breadboard 830 Points
1 – Arduino Nano >> Link <<
1 – SPDT Micro Limit Switch >> Link <<
4 – LEDs
4 – 220 Ohm Resistors
1 – 10k Ohm Resistor
Arduino Code:
int ledPin = 2; int ledPin2 = 3; int ledPin3 = 4; int ledPin4 = 5; int buttonPin = 13; int timer = 100; int buttonValue = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); pinMode(buttonPin, INPUT); Serial.begin(9600); } void loop() { int buttonValue = digitalRead(buttonPin); Serial.println(buttonValue); int button = digitalRead(buttonPin); if (button== HIGH) { digitalWrite(ledPin, LOW); delay(timer); digitalWrite(ledPin2, LOW); delay(timer); digitalWrite(ledPin3, LOW); delay(timer); digitalWrite(ledPin4, LOW); } else { digitalWrite(ledPin4, HIGH); delay(timer); digitalWrite(ledPin3, HIGH); delay(timer); digitalWrite(ledPin2, HIGH); delay(timer); digitalWrite(ledPin, HIGH); delay(timer); } }