Here are the comments for the code:
// Project 5 - LED Chase Effect
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins
int ledDelay(65); // delay between changes
int direction = 1; //starting direction
int currentLED = 0; //the current LED
unsigned long changeTime; //stores a 32-bit variable
void setup() {
//starts the sketch
for (int x=0; x<10; x++) { // set all pins to output
pinMode(ledPin[x], OUTPUT); }//for numbers less than 10, the pinMode increases by 1.
changeTime = millis();//time changes by milliseconds
}
void loop() {
//loops the sketch
if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change
changeLED();
//function that's defined below
changeTime = millis();
}
}
void changeLED() {
for (int x=0; x<10; x++) { // turn off all LED's
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
// change direction if we reach the end
if (currentLED == 9) {direction = -1;}//once the LED reaches the last position it goes back the other way
if (currentLED == 0) {direction = 1;}//once it reaches the first position, it goes the other way
}
No comments:
Post a Comment