Monday, September 10, 2012

Code Comments

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};  ------> "byte" converts the datatype to a byte representation and when the number or value is outside of that range it converts it. The numbers are the array and they are zero indexed which means 4=0, 5=1, 6=2, etc when you use a loop. "ledPin" is the variable. This line is used to indicate which Arduino pin is being used.
int ledDelay(65); ------> "int" is a datatype for integer (numbers without a decimal point). "ledDelay(65)" is the variable and the number 65 is the value that's being assigned (the amount of time that is being delayed).
int direction = 1; ----> 
the number will either increase (+1) or decrease by one from the currentLED +(-1))
int currentLED = 0; -----> the initial value assigned to the current LED variable.

unsigned long changeTime; ----> the time since the button was pressed. It's a variable that can store many values (positive values).

void setup() { ----> function declaration.
  for(int x=0; x<10; x++) { ----> "for" is a statement. "x" is the number of the elements of the array that was previously declared. "x<10" is a logical test that decides whether the value assigned is true and whether it can be used. "x++" is the increment so it is being incremented by 1.
    pinMode(ledPin[x], OUTPUT);} ----> "pinMode" configures the specified pin (ledPin[x]) to behave as an INPUT or OUTPUT, so in this case it's OUTPUT.
    
    changeTime = millis(); ----> returns the value to milliseconds since Arduino can read milliseconds.
}

void loop() { ----> function declaration that repeats itself.

  
  if((millis() - changeTime) > ledDelay) { -----> a logical condition that based on a function (millis() - changeTime) > ledDelay)) the program will run if the value of milliseconds minus the time is bigger than the delayed time.
    changeLED(); -----> function condition for which LED lights up if it's true.
    changeTime = millis(); ----> condition for which timing changes by milliseconds if it's true.
  }
}

void changeLED() {
  for (int x = 0; x<10; x++) { -----> changes to the next LED and repeats the statement in the curly brackets.
    digitalWrite(ledPin[x], LOW); ----> turns all the LEDs off.
  }
  
  digitalWrite(ledPin[currentLED], HIGH); ----> turns the current LED on.
  currentLED += direction; ----> the direction equals the currentLED plus a high value.
  if(currentLED == 9) {direction = -1;} 
  if (currentLED == 0) {direction = 1;} ----> when it reaches the first or last value (9 or 0) it turns back around and goes the other way.
}




One thing you can modify is the delay time by changing the number and it stays more time or less time delayed.

No comments:

Post a Comment