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.



video

Pseudo Code

We just basically state that we have constant variables - one, called buttonPin, for pin 2 and one, ledPin, for pin 13.

In the void setup() we use the function pinMode to assign roles to the pins. Pin 13 will be used as an output, and pin 2 will be used as an input.

In loop() we state the variable called buttonState and at the same we assign to it the value received from input pin 2(the switch, in this case). Then we call an if function in which the puttonState is read and if equal to HIGH we call function digitalWrite to illuminate the led. If first statement not true, it will keep the LED on LOW.

Original Code

This is the original code I used, written by DojoDave. The switch task was to focus on making a switch so I guess I didn't need to write anything else.

/*
created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Button
*/


const int buttonPin = 2;
const int ledPin = 13;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop(){
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}