Skip to content

Instantly share code, notes, and snippets.

@fasmide
Created Aug 22, 2014
Embed
What would you like to do?
reaktion
#include <Bounce2.h>
#include <LiquidCrystal.h>
#define LARGEBTN 10
#define SMALLBTN 11
#define BUZZER 9
//Bounce largeBouncer = Bounce( LARGEBTN, 35 );
Bounce largeBouncer = Bounce();
Bounce smallBouncer = Bounce();
//Bounce smallBouncer = Bounce( SMALLBTN, 5);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7, 8);
char lastChar;
unsigned long lastCharTime = 0;
void setup() {
pinMode(LARGEBTN,INPUT);
pinMode(SMALLBTN, INPUT);
pinMode(BUZZER, OUTPUT);
Serial.begin(115200);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Hej Jakob");
lcd.setCursor(0,1);
lcd.print("Nu jeg klar!");
largeBouncer.attach(LARGEBTN);
smallBouncer.attach(SMALLBTN);
largeBouncer.interval(5);
smallBouncer.interval(5);
}
void loop() {
// Update the debouncer
largeBouncer.update();
smallBouncer.update();
if(smallBouncer.read()) {
startGame();
}
}
void startGame() {
int wait = random(5, 15);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Wait: ");
lcd.print(wait, 10);
lcd.write(" secs");
delay(wait*1000);
sound();
long starTime = millis();
while(largeBouncer.read() == LOW) {
largeBouncer.update();
smallBouncer.update();
}
Serial.write("STOP! hammer time\n");
long endTime = millis();
lcd.setCursor(0, 1);
lcd.print(endTime-starTime, 10);
lcd.write("ms");
Serial.print(endTime-starTime);
Serial.write("ms\n");
}
void handleLcd()
{
if (Serial.available()) {
if(millis() - lastCharTime > 100) {
lcd.clear();
}
lastChar = Serial.read();
if(lastChar == 10) // 10 == newline
{
lcd.setCursor(0,1);
} else {
lcd.write(lastChar);
}
lastCharTime = millis();
}
}
void sound()
{
digitalWrite(BUZZER, HIGH);
delay(100);
digitalWrite(BUZZER, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment