Devices & Components
Arduino Uno Rev3
Pushbutton switch 12mm
Breadboard (generic)
LEDs
Single Turn Potentiometer- 10k ohms
Jumper wires (generic)
Resistor 221 ohm
Project description
Code
6 chasing LEDS
c_cpp
1 2 byte ledPin[] = {8, 9, 10, 11, 12, 13}; // Create array for LED pins 3 int ledDelay; // delay between changes 4 int direction = 1; 5 int currentLED = 0; 6 unsigned long changeTime; 7 int potPin = 2; // select the input pin for the potentiometer 8 int buttonPin = 4; 9 int buttonState = 0; 10 11 12void setup() { 13 pinMode(buttonPin, INPUT); 14 15 for (int x=0; x<6; x++) { // set all pins to output 16 pinMode(ledPin[x], OUTPUT); } 17 changeTime = millis(); 18 } 19 20void loop() 21 { 22 buttonState = digitalRead(buttonPin); 23 ledDelay = analogRead(potPin); // read the value from the pot 24 if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change 25 changeLED(); 26 changeTime = millis(); 27 } 28 } 29 30void changeLED() { 31 for (int x=0; x<6; x++ ) { // turn off all LED's 32 digitalWrite(ledPin[x], LOW); 33 } 34 35 if (buttonState == HIGH) { 36 digitalWrite(ledPin[currentLED], HIGH);} // turn on the current LED 37 else digitalWrite (ledPin[currentLED], LOW); 38 39 currentLED += direction; // increment by the direction value 40 41 if (currentLED == 6){currentLED = 0; 42 } 43 } 44 45
6 chasing LEDS
c_cpp
1 2 byte ledPin[] = {8, 9, 10, 11, 12, 13}; // Create array for LED pins 3 int ledDelay; // delay between changes 4 int direction = 1; 5 int currentLED = 0; 6 unsigned long changeTime; 7 int potPin = 2; // select the input pin for the potentiometer 8 int buttonPin = 4; 9 int buttonState = 0; 10 11 12void setup() { 13 pinMode(buttonPin, INPUT); 14 15 for (int x=0; x<6; x++) { // set all pins to output 16 pinMode(ledPin[x], OUTPUT); } 17 changeTime = millis(); 18 } 19 20void loop() 21 { 22 buttonState = digitalRead(buttonPin); 23 ledDelay = analogRead(potPin); // read the value from the pot 24 if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change 25 changeLED(); 26 changeTime = millis(); 27 } 28 } 29 30void changeLED() { 31 for (int x=0; x<6; x++ ) { // turn off all LED's 32 digitalWrite(ledPin[x], LOW); 33 } 34 35 if (buttonState == HIGH) { 36 digitalWrite(ledPin[currentLED], HIGH);} // turn on the current LED 37 else digitalWrite (ledPin[currentLED], LOW); 38 39 currentLED += direction; // increment by the direction value 40 41 if (currentLED == 6){currentLED = 0; 42 } 43 } 44 45
Downloadable files
6 chasing LEDS with UNO
6 chasing LEDS with UNO
6 chasing LEDS with UNO
6 chasing LEDS with UNO
6 chasing LEDS with TRINKET 5V
6 chasing LEDS with TRINKET 5V
Comments
Only logged in users can leave comments