Monday, September 10, 2012

Code Comments




byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
//initializing the variables to be used
int ledDelay(65);
//time delay
int direction = 1;
//which way the led's will blink or flash?
int currentLED = 0;


unsigned long changeTime;
//makes the numbers not be negative

void setup() {
  for(int x=0; x<10; x++) {
    pinMode(ledPin[x], OUTPUT);}
    //the for initiates/stops the look, how the array is able to repeat itself, 
    //then x starts at 0 then if x<10 then it increases by 1, then the pinMode
    //is to behave as a output
    
    changeTime = millis();
    //it will return the number ini milliseconds since the program startes and changed
}

void loop() {
  
  if((millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
    //if the milliseconds - the changeTime is > then ledDelay then the led will change to 
    //millis so that it is greater then the ledDelay so it will slow it down til it catches up
  }
}

void changeLED() {
  for (int x = 0; x<10; x++) {
    digitalWrite(ledPin[x], LOW);
    //the pinMode has been set to output so the digitalWrtie is being set to correspond with 
    //low which is ground
  }
  
  digitalWrite(ledPin[currentLED], HIGH);
  currentLED += direction;
  if(currentLED == 9) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
  //the digitalWrite is now set to high so corresponding with 5V then it is setting up the
  //way the led will move so when it gets to 9 it will reverse and 0 go back the other way
}

No comments:

Post a Comment