Christmas Lights
Author:
Joshua Mac
Difficulty level (1-5)
3
Time to complete
20 mins
Reference
Aron Graca
Kyle Chan
Challenge Description
While pushing a button, the red lights go on.
when not pushing any buttons, the green lights go on.
Pictures:
Answer
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin1 = 10;
const int ledPin2 = 9;
const int ledPin4 = 11;
const int ledPin3 = 8;
int buttonState1 = 0;
int buttonState2 = 0;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == HIGH)
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin4, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin4, LOW);
}
else if (buttonState2 == HIGH)
{
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
}


No comments:
Post a Comment