Monday, September 10, 2012

Comments on Coding!


byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //IDs ports 4-13
int ledDelay(65); //65 millisecond delay between each LED
int direction = 1; //IDs the direction of LEDs
int currentLED = 0; //start at the first port
unsigned long changeTime; //remembers to repeat the series

void setup() { //defines the action
  for(int x=0; x<10; x++) { //starting place at 0, executes for <10
    pinMode(ledPin[x], OUTPUT);} //the active pin will output
   
    changeTime = millis(); // changing the time to milliseconds
}

void loop() { //defines an action that repeats itself
 
  if((millis() - changeTime) > ledDelay) { /* if the time is >65ms the LED will turn off*\
    changeLED(); //states an action defined below
    changeTime = millis(); //makes sure the time is milliseconds
  }
}

void changeLED() { //this actions sets up to turn the pin off
  for (int x = 0; x<10; x++) {
    digitalWrite(ledPin[x], LOW); //sets the LED off
  }
 
  digitalWrite(ledPin[currentLED], HIGH); /*this action reverses the direction*\
  currentLED += direction;
  if(currentLED == 9) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
}


You can modify this code anywhere there is a number or set of parentheses or within a series of info within a bracket (ports 3,4,5, etc.) You could also change which ports are used. Also, you can change whether or not something will turn ON or OFF (HIGH or LOW).

Kim Calderone & Adam Germann

No comments:

Post a Comment