-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simon.ino
162 lines (127 loc) · 4.06 KB
/
Simon.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
/*
______ ________ ______ _ _
/ ____ |__ __| | \ / | / __ \ | \ | |
\ \___ || | \ / | | / \ | | \ | |
\___ \ || | \/ | | | | | | | | |
___ / / __ || __ | |\__/| | | \__/ | | | \ |
_____/ |________| |_| |_| \______/ |_| \_|
By @TheCaptain
*/
//This can probably run on any arduino, but I only tried it on the UNO
/**
NECESSITIES
-1 Arduino Uno
-1 SPDT OR SPST switch
-4 SPST push-button switches
-4 LED's preferably of different colors
-2 100 ohm resistors
-1 10K ohm resistor
-1 five volt power regulator
-2 4026B seven-segment drives (optional)
-2 seven-segment displays (optional)
-? hookup wire
-1 USB cable
while seven-segment displays for score keeping aren't necessary, I really suggest it
*/
//all power is routed through a SPDT switch and a 100 ohm resistor and than through a five volt power regulator (it doesn't matter what kind)
//leds are hooked up to arduino outputs 2, 3, 4 and 5 on long leg and short leg to ground
//buttons are hooked up to arduino inputs A0, A1, A2 and A3 and power through a 10K ohm resistor
//output 12 goes to pin 1 on a 4026B seven-segment drive chip, which is than carried to another 4026B and than to two seven-segment displays
//4026B's are powered through a 100 ohm resistor
//setup outputs
int outputs[] = {2,3,4,5};
int inputs[] = {0,1,2,3};
int inputPorts[] = {A0,A1,A2,A3};
int sevenSegmentPulseOutput = 12;
//make an array 100 long for storing the press sequence
int correct[100] = {};
//an integer for indexing the press sequence
int colorOn = 0;
//keep track of score
int score = 0;
// whether you are actively pressing buttons or watching the replay
boolean replay = true;
void ledOn(int num) {
for (int i = 0; i < 4; i += 1) {
if (outputs[i] == num) {
//turn led on if it matches the input
digitalWrite(outputs[i], HIGH);
}
else {
//turn led off if it doesn't match the input
digitalWrite(outputs[i], LOW);
}
}
}
void allOn() {
for (int i = 0; i < 4; i += 1) {
//turn all the leds on
digitalWrite(outputs[i], HIGH);
}
}
void allOff() {
for (int i = 0; i < 4; i += 1) {
//turn all the leds off
digitalWrite(outputs[i], LOW);
}
}
void setup() {
for (int i = 0; i < 4; i += 1) {
//initialize outputs
pinMode(outputs[i], OUTPUT);
}
//setup random based off of unconnected inputs
randomSeed(analogRead(A6)*analogRead(A7));
//choose first press
correct[0] = floor(random(0,4));
//game loop
while (true) {
for (int i = 0; i < 4; i+=1) {
//if button pressed or playing back sequence
if (analogRead(inputPorts[i]) > 1000 || replay) {
if (inputs[i] == correct[colorOn]) {
//if correct press, blink light and go to the next press
colorOn += 1;
ledOn(outputs[i]);
delay(500);
allOff();
delay(200);
if (colorOn > score) {
if (!replay) {
//if not just playing back the sequence, then increase score and add to sequence
score += 1;
correct[score] = floor(random(0,4));
//increase display counter
digitalWrite(sevenSegmentPulseOutput, HIGH);
delay(20);
digitalWrite(sevenSegmentPulseOutput, LOW);
}
//if done with sequence restart the sequence
colorOn = 0;
//add a delay before the replay
replay = !replay;
if (replay) {
delay(480);
}
}
break;
}
else if (!replay) {
//if incorrect button is pressed, blink the correct led until program is restarted
allOff();
while (!replay) {
ledOn(outputs[correct[colorOn]]);
delay(100);
allOff();
delay(100);
}
}
}
}
//wait a millisecond before going through loop again
delay(1);
}
}
void loop() {
// I didn't use this loop because breaking out of a while loop is easier
}