Tuesday, September 11, 2012

Erin | Ch. 3 P5 Comment Code


byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};    // assigns pins 4-13 to recieve data in this case LEDs
int ledDelay(65);                                      // delay between changes **can change the speed here
int direction = 1;                                     // the initial directin is add on to sequence
int currentLED = 0;                                    // the current LED is off
unsigned long changeTime;                       // the change is measured in postive numbers


void setup() {                                      // setup function  runs once
  for(int x=9; x>=0; x--) {                    // starting with the first one it counts up until it reaches 9 0,1,2,3,4,5...9
    pinMode(ledPin[x], OUTPUT);}      // each of the LEDs is an output ledPin[1]=4, OUTPUT (result)
 
    changeTime = millis();                      //call the changetime function described below give it the change time unit of ms
}

void loop() {                                      // loop the described funtions below runs on repeat

  if((millis() - changeTime) > ledDelay) {          // if the millis(function) - changeTime is less than the ledDelay of 65 milliseconds (if 65ms has passed)
    changeLED();                                    // do changeLED function decribed below
    changeTime = millis();                          // redo every 65ms
  }
}

void changeLED() {                                  // sets the changeLED function
  for (int x = 0; x<10; x++) {                      // when the current LED is 0 up to 9 the program counts up
    digitalWrite(ledPin[x], LOW);                    // turns off all the LED
  }

  digitalWrite(ledPin[currentLED], HIGH);            // the currentLED turns on
  currentLED += direction;                            //  determines the direction
  if(currentLED == 9) {direction = -1;}                // when the current LED is 9 change direction
  if (currentLED == 0) {direction = 1;}                // when the current LED is 0 change direction
}

No comments:

Post a Comment