Christmas lights
There’s satisfaction to be had in watching lights blink especially when those lights turn on by your command.
The code is relatively simple enough now that I’ve had the weekend to digest the meaning of all these pins and ports.
//double lights green light and red light
void setup(){
pinMode(3,INPUT); //SWITCH
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}
void loop(){
// if (digitalRead(3) == true) {
digitalWrite(5,true);
digitalWrite(4,false);
delay(500);
//}
// else {
digitalWrite(5, false);
digitalWrite(4, true);
delay (500);
// }
}
Using the same setup but different program and allowing the “if”s and “else” commands, this tells the chip to perfrom an action that will respond differently. In this case, when the switch is pressed the light will either be switched between red and green.
Feeling a little bold, I added more lights.
I wanted to get the switch to cascade the lights using the code below but it seemed that it wasn’t reacting to the switch and thus acting on switching the lights on it’s own. I’ll work on it some more.
//christmas lights
void setup(){
pinMode(9,INPUT); //SWITCH
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
// pinMode(8,OUTPUT);
}
void loop(){
if (digitalRead(9) == true && digitalRead(2) ==true){
digitalWrite (2, true);
delay(1000);
digitalWrite (3, true);
delay(1000);
digitalWrite (4, true);
delay(1000);
digitalWrite (5, true);
delay(1000);
digitalWrite (6, true);
delay(1000);
digitalWrite (7, true);
delay(1000);
// digitalWrite (8, true);
// delay(1000);
//digitalWrite (8, false);
//delay(1000);
digitalWrite (7, false);
delay(1000);
digitalWrite (6, false);
delay(1000);
digitalWrite (5, false);
delay(1000);
digitalWrite (4, false);
delay(1000);
digitalWrite (3, false);
delay(1000);
digitalWrite (2, false);
}
}
No trackbacks yet.