This repository was archived by the owner on Dec 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
73 lines (52 loc) · 1.54 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "game.h"
#include "button.h"
#include "screen.h"
#include "led.h"
#include "timer.h"
#include "constants.h"
#include "logger.h"
namespace game {
namespace {
const unsigned long GAME_TIME = 30000;
unsigned long startTime;
void countDown() {
screen::display("3");
delay(1000);
screen::display("2");
delay(1000);
screen::display("1");
delay(1000);
}
unsigned long runMain() {
uint8_t buttonsPressed = 0;
while (millis() < startTime + GAME_TIME) {
int buttonNumber = random(0, constants::NUMBER_OF_LEDS); //Generate random button
button::clearLast();
led::turnOn(buttonNumber); //Turn on led and wait for button press
led::shiftOut();
while(not button::isPressed(buttonNumber) and millis() < startTime + GAME_TIME){
timer::checkUpdateDisplay();
}
led::turnOff(buttonNumber);
if (millis() < startTime + GAME_TIME){
buttonsPressed ++; //Increment counter
} else {
return (millis() - startTime)/buttonsPressed;
}
}
}
}
unsigned long getRemainingTime() {
unsigned long remainingTime = startTime + GAME_TIME - millis();
if (remainingTime > 0) {
return remainingTime;
}
return 0;
}
void start() {
countDown();
startTime = millis();
unsigned long averageReactionSpeed = runMain();
screen::display(String(averageReactionSpeed) + " average speed in millis");
}
}