This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim-bologna.ino
201 lines (174 loc) · 3.7 KB
/
sim-bologna.ino
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#define BUTTON_1_PIN 2
#define BUTTON_2_PIN 3
#define BUTTON_3_PIN 4
#define BUTTON_4_PIN 5
#define BUTTON_5_PIN 6
#define BUTTON_6_PIN 7
#define BUTTON_COUNT 6
#define LEVEL_1_PIN 9
#define LEVEL_2_PIN 10
#define LEVEL_3_PIN 11
#define BUZZER_PIN 12
#define DEBOUNCE_DELAY 50
#define MOTOR_TURN_TIME 250
int buttonPins[BUTTON_COUNT] = {
BUTTON_1_PIN,
BUTTON_2_PIN,
BUTTON_3_PIN,
BUTTON_4_PIN,
BUTTON_5_PIN,
BUTTON_6_PIN,
};
/**
* Button state per pin. Structure is { { PREV, CURR }, ...}
*/
int buttonStates[BUTTON_COUNT][2] = {};
unsigned long lastDebounceTimes[BUTTON_COUNT] = {};
/**
* Level layout. Numbers correspond to buttons
*/
int levels[3][BUTTON_COUNT] = {
{1, 4, 6, 1, 0, 0},
{2, 5, 3, 1, 6, 0},
{4, 5, 3, 2, 3, 1},
};
int currentLevel = 0;
int currentStep = 0;
bool checkMove(int levelToCheck, int step, int pressedButton)
{
return levels[levelToCheck][step] == pressedButton;
}
void reset()
{
currentLevel = 0;
currentStep = 0;
}
/**
* Returns true if the current step in a level is 0
*/
bool isLevelFinished(int levelToCheck, int step)
{
return step == 6 || levels[levelToCheck][step] == 0;
}
void setupPins()
{
pinMode(13, OUTPUT);
pinMode(LEVEL_1_PIN, OUTPUT);
pinMode(LEVEL_2_PIN, OUTPUT);
pinMode(LEVEL_3_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(LEVEL_1_PIN, HIGH);
digitalWrite(LEVEL_2_PIN, HIGH);
digitalWrite(LEVEL_3_PIN, HIGH);
for (int i = 0; i < 6; i++)
{
pinMode(buttonPins[i], INPUT);
}
}
bool checkButtonPress(int buttonIndex)
{
int reading = digitalRead(buttonPins[buttonIndex]);
if (buttonStates[buttonIndex][0] != reading)
{
lastDebounceTimes[buttonIndex] = millis();
}
if ((millis() - lastDebounceTimes[buttonIndex]) > DEBOUNCE_DELAY)
{
if (reading != buttonStates[buttonIndex][1])
{
buttonStates[buttonIndex][1] = reading;
// return true when the state changed to high
if (reading == HIGH)
{
return true;
}
}
}
buttonStates[buttonIndex][0] = reading;
return false;
}
void setup()
{
Serial.begin(9600);
setupPins();
}
void nextLevel()
{
currentLevel++;
currentStep = 0;
Serial.println((String) "! Arrived in next level: " + currentLevel);
}
void buzz(int t = 1000)
{
tone(BUZZER_PIN, 500);
delay(t);
noTone(BUZZER_PIN);
}
void dropKralen(int level)
{
int enginePin;
switch (level)
{
case 0:
enginePin = LEVEL_1_PIN;
break;
case 1:
enginePin = LEVEL_2_PIN;
break;
case 2:
enginePin = LEVEL_3_PIN;
break;
}
Serial.println("Dropping the kralen");
digitalWrite(enginePin, LOW);
delay(MOTOR_TURN_TIME);
digitalWrite(enginePin, HIGH);
}
void loop()
{
for (int i = 0; i < BUTTON_COUNT; i++)
{
int button = i + 1;
// Check if the user pressed a button this turn, if true, do a thing
if (!checkButtonPress(i))
{
continue;
}
Serial.println((String) "User pressed button " + button);
if (checkMove(currentLevel, currentStep, button))
{
Serial.println(" Which was correct!");
// Was correct!
currentStep++;
if (!isLevelFinished(currentLevel, currentStep))
{
continue;
}
dropKralen(currentLevel);
// Go to next level and reset steps also start motor to drop the kralen
nextLevel();
if (currentLevel == 3)
{
Serial.println("Game finished!!");
// Finished!!!!!!!!
// Dunno what we should do :shrug:
buzz(150);
delay(100);
buzz(150);
delay(100);
buzz(150);
}
}
else
{
Serial.println(" Which was wrong!");
// Wrong!
buzz(200);
nextLevel();
}
}
if (currentLevel == 3)
{
reset();
}
}