Wednesday, September 30, 2009

Lighting three LED lights

//My code using for loops and if statements to turn on 3 seperate LED lights.
/*
int ledPinOne = 13;
int ledPinTwo = 10;
int ledPinThree = 7;
int buttonPin = 3;
int buttonState = 0;


void setup(){

pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {

for (int i = 0; i<100; i++) {
digitalWrite(ledPinOne, HIGH);
delay(i);
digitalWrite(ledPinTwo, HIGH);
delay(i);
digitalWrite(ledPinThree, HIGH);
delay(i);
digitalWrite(ledPinOne, LOW);
delay(i);
digitalWrite(ledPinTwo, LOW);
delay(i);
digitalWrite(ledPinThree, LOW);
delay(i);
}

if(buttonPin == LOW) {
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
}

}
delay(1);
}
*/

pseudo code

In this example, I am setting up 5 different integer variables, named ledPinOne with a value of 13, ledPinTwo with a value of 10, ledPinThree with a value of 7, buttonPin with a value of 3, and buttonState with a value of 0. Next is the void setup where we set the variables to iether Input or Output ( listening or doing something), where we set ledpinone,two, and three to output and buttonPin to input. Next is the void loop, in which everything is constantly being played over and over again. Now we set buttonState to read the variable buttonPin which is pin 3. If buttonState is high in value, for the integer i is equal to 0, i is less then 100, add one to i everytime. Do this for pin one two and three with a delay of i. So it will gradually get bigger and bigger until it hits 100. If buttonPin is low in value, then turn off pins one two and three. This is so the button only has to be pressed once and this will run through then turn off when it is done. Next i added a delay of 1 so that the prgram does not run through this code too fast.



No comments:

Post a Comment