Two Person Pushbutton Race
Last updated
Was this helpful?
Last updated
Was this helpful?
Pushbutton Race
Follow this guide to create a two player “button mashing” race.
Wiring
Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int p1 = 7;
const int p2 = 8;
int count1 = 0;
int count2 = 0;
bool p1Released;
bool p2Released;
int buttonPresses = 100;
void setup()
{
pinMode(p1,INPUT_PULLUP);
pinMode(p2,INPUT_PULLUP);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.clear();
lcd.print("3 ");
delay(400);
lcd.print("2 ");
delay(400);
lcd.print("1 ");
delay(400);
lcd.print("Go!");
delay(200);
}
void loop()
{
if(count1 >= 100) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Player 1 Wins!");
while(true)
{
//stay here until the reset button is pressed lcd.setCursor(0,1);
lcd.print("Reset to play again"); lcd.scrollDisplayLeft();
delay(400);
}
}
if(count2 >= 100) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Player 2 Wins!");
while(true)
{
//stay here until the reset button is pressed lcd.setCursor(0,1);
lcd.print("Reset to play again");
lcd.scrollDisplayLeft();
delay(400);
}
}
if(digitalRead(p1)==LOW && p1Released)
{
p1Released = false;
count1++;
}
if(digitalRead(p2)==LOW && p2Released)
{
p2Released = false;
count2++;
}
if(digitalRead(p1)==HIGH && !p1Released)
{
p1Released = true;
}
if(digitalRead(p2)==HIGH && !p2Released)
{
p2Released = true;
}
lcd.setCursor(0,0);
lcd.print("Player 1: ");
lcd.print((buttonPresses - count1));
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Player 2: ");
lcd.print((buttonPresses - count2));
lcd.print(" ");
}