Sunday, September 16, 2012

Commented Code for Project 5 Chapter 3

...is it just me or is this not the same code we commented in pairs in class? Either way, here is my commented code for Project 5, Chapter 3 (which I posted about a week ago.


// Project 5 - LED Chase Effect
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// Array of which pins hold the LED’s (labels the LED’s)
int ledDelay(65);
// delay between changes
int direction = 1;
// direction of light movement (ex: from LED 0 to LED 1, to LED 2, one LED at a time)
int currentLED = 0;
// the current LED
unsigned long changeTime; 
//changing time with a "long" data type


void setup() {
//setup the program
for (int x=0; x<10; x++) {
//while these conditions are true [starting at x=0 while x is less than ten, add 1]

pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
// change the time in milli seconds
}
void loop() {
// tells the program to constantly loop (everything in the void loop() will loop automatically
if ((millis() - changeTime) > ledDelay) {
//if ((millis() - changeTime) > ledDelay) is true, start the “change LED” function (next line below) and change the time to milliseconds (last line before } bracket)
changeLED();
changeTime = millis();
}
}
void changeLED() { 
// change LED function (that was called on previously) and everything below is what happens in this function
for (int x=0; x<10; x++) {
// while these conditions are true [starting at x=0 while x is less than ten, add 1] turn off all LED's (line below)
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
// when the “if” statement is not true, turn on the current LED
currentLED += direction;  
// increment by the direction value
if (currentLED == 9) {direction = -1;}
// change direction if we reach the end
if (currentLED == 0) {direction = 1;}
// change direction if we reach the beginning again
}

No comments:

Post a Comment