Monday, September 10, 2012

commented code

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};     sets which pins to be activated by the functions (defines pins of array)
int ledDelay(65);                                                   integer - defining lighting delay
int direction = 1;                                                   increases pin number moving through the sequence
int currentLED = 0;                                                decreases pin number moving through the array
unsigned long changeTime;                                   compressed number system to be manageable?

void setup() {                          setup function parameters
  for(int x=0; x<10; x++) {        defining function if pin is less than 10 continue to move towards higher pin number
   inMode(ledPin[x], OUTPUT);}    per pin (x) receives data 
    
    changeTime = millis();            time measured in milliseconds
}

void loop() {                           defining loop parameters
  
  if((millis() - changeTime) > ledDelay) {       initializing the function, if-then statement
    changeLED();
    changeTime = millis();
  }
}

void changeLED() {
  for (int x = 0; x<10; x++) {            defines direction of array when reverse direction
    digitalWrite(ledPin[x], LOW);   turns the pin off
  }
  
  digitalWrite(ledPin[currentLED], HIGH);    turns the pin on, function as defined above
  currentLED += direction;       
  if(currentLED == 9) {direction = -1;}             when array reaches high pin/reverse direction                 
  if (currentLED == 0) {direction = 1;}             when array reaches low pin/reverse direction
}

if we were to add more lights we would add pin numbers to the byte defition/add to the array

No comments:

Post a Comment