Monday, September 10, 2012

Code Comment

//You tell it what pins are going to be used with the LEDs
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

//You tell how long the delay between LEDs 
int ledDelay(65);

//sets direction to positive but allows it to change to negative so the lights will go back and forth
int direction = 1;

//initial LED
int currentLED = 0;

//makes sure time isnt negative
unsigned long changeTime;





//setup runs once 
void setup() {
  
  //the for loop that's calling the array of LEDs     
  for(int x=0; x<10; x++) {
    //deciding which LED to use
    pinMode(ledPin[x], OUTPUT);}
    //making the time into milliseconds 
    changeTime = millis();
}





//makes the loop continuous
void loop() {
  //checking if the milliseconds-changeTime is greater than leddelay (~60)
  if((millis() - changeTime) > ledDelay) {
    //if the above statement is true then it changes the LED to the next one
    changeLED();
    //if the above statement is true then it makes the changTime equal to the milliseconds
    changeTime = millis();
  }
}




//the function that changes the LED to the next one
void changeLED() {
  //turns the LEDs off
  for (int x = 0; x<10; x++) {
    //turns LEDs off
    digitalWrite(ledPin[x], LOW);
  }
  
  //turns LED on
  digitalWrite(ledPin[currentLED], HIGH);
  //turns the next one on
  currentLED += direction;
  //determines directions, alternating directions of LED movement
  if(currentLED == 9) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
}

No comments:

Post a Comment