001d_HeadsOrTails

Project Overview:

- 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.

Code:

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);

  }

}

 

Schematic:

Layout:

What I learned / Problems:

What I learned:

  • I learned how to use properly use input from a pushbutton

Problems:

  • I had problems when you let go of the push button
  • I had to figure out a way to find which LED was on last and to code it so it stays on