MichDragstar
Published © GPL3+

ARDUINO UNO or TRINKET PRO 5V 6 Chasing LEDS with POT and PB

6 chasing LEDS with POT and pushbutton indicating waterpump is working. (PART 2 of 3)

BeginnerFull instructions provided4,089
ARDUINO UNO or TRINKET PRO 5V 6 Chasing LEDS with POT and PB

Things used in this project

Story

Read more

Schematics

6 chasing LEDS with UNO

6 chasing LEDS with TRINKET 5V

Code

6 chasing LEDS

C/C++
    byte ledPin[] = {8, 9, 10, 11, 12, 13};    // Create array for LED pins
    int ledDelay; // delay between changes
    int direction = 1;
    int currentLED = 0;
    unsigned long changeTime;
    int potPin = 2;    // select the input pin for the potentiometer
    int buttonPin = 4;   
    int buttonState = 0;    


void setup() {
    pinMode(buttonPin, INPUT);  
      
    for (int x=0; x<6; x++) {   // set all pins to output
    		pinMode(ledPin[x], OUTPUT); }
      	changeTime = millis();     
   }

void loop() 
   {
     buttonState = digitalRead(buttonPin);
     ledDelay = analogRead(potPin); // read the value from the pot 
     if ((millis() - changeTime) > ledDelay) {      // if it has been ledDelay ms since last change
  		changeLED();
   		changeTime = millis();
  	}
    }

void changeLED() {
    for (int x=0; x<6; x++ ) {   // turn off all LED's
    		digitalWrite(ledPin[x], LOW);
  	}
 
    if (buttonState == HIGH) {
    digitalWrite(ledPin[currentLED], HIGH);} // turn on the current LED    
    else digitalWrite (ledPin[currentLED], LOW);
    	
   currentLED += direction; // increment by the direction value

   if (currentLED == 6){currentLED = 0;
   }
   }
   

Credits

MichDragstar

MichDragstar

7 projects • 17 followers

Comments