- Create a project that can make the Arduino play the game Heads or Tails by flashing two colored LEDs. For this assignment I will be using a tactile push button.
int btn2 = 2;
int led1 = 6;
int led2 = 7;
int time = 20;
int buttonState = 0;
int led1State = 1;
int led2State = 0;
int ledTemp = 0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(btn2, INPUT_PULLUP);
}
void loop(){
buttonState = digitalRead(btn2);
if (buttonState == LOW){
digitalWrite(led1, led1State);
digitalWrite(led2, led2State);
delay(time);
ledTemp = led1State;
led1State = led2State;
led2State = ledTemp;
}
else{
digitalWrite(led1, led1State);
digitalWrite(led2, led2State);
}
}
What I learned:
Problems:
You can do it, too! Sign up for free now at https://www.jimdo.com