-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeniusduino.pde
More file actions
177 lines (150 loc) · 3.48 KB
/
geniusduino.pde
File metadata and controls
177 lines (150 loc) · 3.48 KB
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
//
// =============
// GeniusDuino
// =============
// # Leandro Nunes : leandron85[at]gmail[dot]com
// # Website: http://github.com/leandron/geniusduino
// # Blog: http://leandron.wordpress.com
//
// Instructions to assembly:
// - put buttons in digital pins, 4, 5, 6 and 7
// - put leds in digital pins 8, 9, 10 and 11
//
const int ST_BEGIN = 0;
const int ST_SHOW = 1;
const int ST_WAIT_INPUT = 2;
const int ST_INPUT_ERROR = 3;
const int ST_INPUT_OK = 4;
int error_pin = 13;
int ok_pin = 12;
int input_button[] = {4,5,6,7};
int output_led[] = {8,9,10,11};
int combination[100];
int comb_counter = 0;
int game_state;
int user_input_counter;
int pressed_button;
bool lock_buttons = false;
bool button_pressed_now = false;
void setup() {
// Set the button input mode
for (int i=0; i < 4; i++) {
pinMode(input_button[i], INPUT);
pinMode(output_led[i], OUTPUT);
}
pinMode(error_pin, OUTPUT);
pinMode(ok_pin, OUTPUT);
randomSeed(analogRead(0));
Serial.begin(9600);
change_game_state(ST_BEGIN);
}
//implements the states of the device
void loop() {
switch(game_state) {
case ST_BEGIN:
begin_action();
break;
case ST_SHOW:
show_and_generate_action();
clean_inputs();
change_game_state(ST_WAIT_INPUT);
break;
case ST_WAIT_INPUT:
read_buttons_action();
break;
case ST_INPUT_ERROR:
input_error_action();
break;
case ST_INPUT_OK:
input_ok_action();
break;
}
}
void input_error_action() {
all_lights_on(200);
delay(200);
all_lights_on(200);
change_game_state(ST_BEGIN);
}
void input_ok_action() {
digitalWrite(ok_pin, HIGH);
delay(1000);
digitalWrite(ok_pin, LOW);
change_game_state(ST_SHOW);
}
void change_game_state(int new_state) {
game_state = new_state;
}
void clean_inputs() {
user_input_counter = 0;
}
void read_buttons_action() {
button_pressed_now = false;
for (int i=0; i < 4; i++) {
if (digitalRead(input_button[i]) == HIGH) {
button_pressed_now = true;
pressed_button = i;
break;
}
}
if (!button_pressed_now) {
lock_buttons = false;
}
if (button_pressed_now && !lock_buttons) {
Serial.print("pressed button: ");
Serial.println(pressed_button);
verify_user_input(pressed_button);
lock_buttons = true;
}
}
void verify_user_input(int pressed) {
if (pressed != combination[user_input_counter]) {
change_game_state(ST_INPUT_ERROR);
return;
}
user_input_counter++;
if (user_input_counter == comb_counter) {
user_input_counter = 0;
all_lights_on(1000);
change_game_state(ST_INPUT_OK);
}
}
void show_and_generate_action() {
combination[comb_counter] = random(1000) % 4;
comb_counter++;
Serial.print("memory: ");
for (int i=0; i < comb_counter; i++) {
Serial.print(combination[i]);
Serial.print(" ");
blink_led(output_led[combination[i]], 500);
delay(50);
}
Serial.println(" ");
}
void begin_action() {
init_lights();
comb_counter = 0;
change_game_state(ST_SHOW);
}
void blink_led(int pin) {
blink_led(pin, 500);
}
void blink_led(int pin, int time) {
digitalWrite(pin, HIGH);
delay(time);
digitalWrite(pin, LOW);
}
void init_lights() {
for (int i; i < sizeof(output_led); i++) {
blink_led(output_led[i], 300);
}
}
void all_lights_on(int time) {
for (int i; i < sizeof(output_led); i++) {
digitalWrite(output_led[i], HIGH);
}
delay(time);
for (int i; i < sizeof(output_led); i++) {
digitalWrite(output_led[i], LOW);
}
}