Sunday, September 9, 2012

COmmenting code so that you know what is going on

This is for in-class for tomorrow.


Please in your own words comment the following code without referring to the book. Tell me what this code is doing line by line to the best of your ability. What are some aspects of the code that you could change? What would you have to do if you added 3 more lights?

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay(65);
int direction = 1;
int currentLED = 0;
unsigned long changeTime;

void setup() {
  for(int x=0; x<10; x++) {
    pinMode(ledPin[x], OUTPUT);}
   
    changeTime = millis();
}

void loop() {
 
  if((millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
  }
}

void changeLED() {
  for (int x = 0; x<10; x++) {
    digitalWrite(ledPin[x], LOW);
  }
 
  digitalWrite(ledPin[currentLED], HIGH);
  currentLED += direction;
  if(currentLED == 9) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
}

No comments:

Post a Comment