1 | #include <Tone.h>
|
2 | Tone speakerpin;
|
3 | int starttune[] = {NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_G4};
|
4 | int duration2[] = {100, 200, 100, 200, 100, 400, 100, 100, 100, 100, 200, 100, 500};
|
5 | int note[] = {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_C5, NOTE_G4, NOTE_C5};
|
6 | int duration[] = {100, 100, 100, 300, 100, 300};
|
7 | boolean button[] = {2, 3, 4, 5}; //The four button input pins
|
8 | boolean ledpin[] = {8, 9, 10, 11}; // LED pins
|
9 | int turn = 0; // turn counter
|
10 | int buttonstate = 0; // button state checker
|
11 | int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
|
12 | int inputArray[100];
|
13 |
|
14 |
|
15 | void setup()
|
16 | {
|
17 |
|
18 | speakerpin.begin(12); // Lautsprecher pin 12
|
19 |
|
20 | for(int x=0; x<4; x++) // LED pins are outputs
|
21 | {
|
22 | pinMode(ledpin[x], OUTPUT);
|
23 | }
|
24 |
|
25 | for(int x=0; x<4; x++)
|
26 | {
|
27 | pinMode(button[x], INPUT); // button pins are inputs
|
28 | digitalWrite(button[x], HIGH); // enable internal pullup; buttons start in high position; logic reversed
|
29 | }
|
30 |
|
31 | randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function
|
32 | for (int thisNote = 0; thisNote < 13; thisNote ++) {
|
33 | // play the next note:
|
34 | speakerpin.play(starttune[thisNote]);
|
35 | // hold the note:
|
36 | if (thisNote==0 || thisNote==2 || thisNote==4 || thisNote== 6)
|
37 | {
|
38 | digitalWrite(ledpin[0], HIGH);
|
39 | }
|
40 | if (thisNote==1 || thisNote==3 || thisNote==5 || thisNote== 7 || thisNote==9 || thisNote==11)
|
41 | {
|
42 | digitalWrite(ledpin[1], HIGH);
|
43 | }
|
44 | if (thisNote==8 || thisNote==12)
|
45 | {
|
46 | digitalWrite(ledpin[2], HIGH);
|
47 | }
|
48 | if (thisNote==10)
|
49 | {
|
50 | digitalWrite(ledpin[3], HIGH);
|
51 | }
|
52 | delay(duration2[thisNote]);
|
53 | // stop for the next note:
|
54 | speakerpin.stop();
|
55 | digitalWrite(ledpin[0], LOW);
|
56 | digitalWrite(ledpin[1], LOW);
|
57 | digitalWrite(ledpin[2], LOW);
|
58 | digitalWrite(ledpin[3], LOW);
|
59 | delay(25);
|
60 | }
|
61 | delay(1000);
|
62 | }
|
63 |
|
64 | void loop()
|
65 | {
|
66 |
|
67 |
|
68 |
|
69 | digitalWrite(ledpin[0], LOW);
|
70 | digitalWrite(ledpin[1], LOW);
|
71 | digitalWrite(ledpin[2], LOW);
|
72 | digitalWrite(ledpin[3], LOW);
|
73 | delay(1000);
|
74 |
|
75 | for (int y=turn; y <= turn; y++)
|
76 | { //Limited by the turn variable
|
77 |
|
78 | randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
|
79 | for (int x=0; x <= turn; x++)
|
80 | {
|
81 |
|
82 |
|
83 | for(int y=0; y<4; y++)
|
84 | {
|
85 |
|
86 | if (randomArray[x] == 1 && ledpin[y] == 8)
|
87 | { //if statements to display the stored values in the array
|
88 | digitalWrite(ledpin[y], HIGH);
|
89 | speakerpin.play(NOTE_G3, 100);
|
90 | delay(400);
|
91 | digitalWrite(ledpin[y], LOW);
|
92 | delay(100);
|
93 | }
|
94 |
|
95 | if (randomArray[x] == 2 && ledpin[y] == 9)
|
96 | {
|
97 | digitalWrite(ledpin[y], HIGH);
|
98 | speakerpin.play(NOTE_A3, 100);
|
99 | delay(400);
|
100 | digitalWrite(ledpin[y], LOW);
|
101 | delay(100);
|
102 | }
|
103 |
|
104 | if (randomArray[x] == 3 && ledpin[y] == 10)
|
105 | {
|
106 | digitalWrite(ledpin[y], HIGH);
|
107 | speakerpin.play(NOTE_B3, 100);
|
108 | delay(400);
|
109 | digitalWrite(ledpin[y], LOW);
|
110 | delay(100);
|
111 | }
|
112 |
|
113 | if (randomArray[x] == 4 && ledpin[y] == 11)
|
114 | {
|
115 | digitalWrite(ledpin[y], HIGH);
|
116 | speakerpin.play(NOTE_C4, 100);
|
117 | delay(400);
|
118 | digitalWrite(ledpin[y], LOW);
|
119 | delay(100);
|
120 | }
|
121 | }
|
122 | }
|
123 | }
|
124 | input();
|
125 | //}
|
126 | }
|
127 |
|
128 | void input() { //Function for allowing user input and checking input against the generated array
|
129 |
|
130 | for (int x=0; x <= turn;)
|
131 | { //Statement controlled by turn count
|
132 |
|
133 | for(int y=0; y<4; y++)
|
134 | {
|
135 |
|
136 | buttonstate = digitalRead(button[y]);
|
137 |
|
138 | if (buttonstate == LOW && button[y] == 2)
|
139 | { //Checking for button push
|
140 | digitalWrite(ledpin[0], HIGH);
|
141 | speakerpin.play(NOTE_G3, 100);
|
142 | delay(200);
|
143 | digitalWrite(ledpin[0], LOW);
|
144 | inputArray[x] = 1;
|
145 | delay(250);
|
146 |
|
147 | if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
|
148 | fail(); //the value in the same spot on the generated array
|
149 | } //The fail function is called if it does not match
|
150 | x++;
|
151 | }
|
152 | if (buttonstate == LOW && button[y] == 3)
|
153 | {
|
154 | digitalWrite(ledpin[1], HIGH);
|
155 | speakerpin.play(NOTE_A3, 100);
|
156 | delay(200);
|
157 | digitalWrite(ledpin[1], LOW);
|
158 | inputArray[x] = 2;
|
159 | delay(250);
|
160 |
|
161 | if (inputArray[x] != randomArray[x]) {
|
162 | fail();
|
163 | }
|
164 | x++;
|
165 | }
|
166 |
|
167 | if (buttonstate == LOW && button[y] == 4)
|
168 | {
|
169 | digitalWrite(ledpin[2], HIGH);
|
170 | speakerpin.play(NOTE_B3, 100);
|
171 | delay(200);
|
172 | digitalWrite(ledpin[2], LOW);
|
173 | inputArray[x] = 3;
|
174 | delay(250);
|
175 |
|
176 | if (inputArray[x] != randomArray[x]) {
|
177 | fail();
|
178 | }
|
179 | x++;
|
180 | }
|
181 |
|
182 | if (buttonstate == LOW && button[y] == 5)
|
183 | {
|
184 | digitalWrite(ledpin[3], HIGH);
|
185 | speakerpin.play(NOTE_C4, 100);
|
186 | delay(200);
|
187 | digitalWrite(ledpin[3], LOW);
|
188 | inputArray[x] = 4;
|
189 | delay(250);
|
190 |
|
191 | if (inputArray[x] != randomArray[x])
|
192 | {
|
193 | fail();
|
194 | }
|
195 | x++;
|
196 | }
|
197 | }
|
198 | }
|
199 | delay(500);
|
200 | turn++; //Increments the turn count, also the last action before starting the output function over again
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | void fail() { //Function used if the player fails to match the sequence
|
209 |
|
210 | for (int y=0; y<=2; y++)
|
211 | { //Flashes lights for failure
|
212 |
|
213 | digitalWrite(ledpin[0], HIGH);
|
214 | digitalWrite(ledpin[1], HIGH);
|
215 | digitalWrite(ledpin[2], HIGH);
|
216 | digitalWrite(ledpin[3], HIGH);
|
217 | speakerpin.play(NOTE_G3, 300);
|
218 | delay(200);
|
219 | digitalWrite(ledpin[0], LOW);
|
220 | digitalWrite(ledpin[1], LOW);
|
221 | digitalWrite(ledpin[2], LOW);
|
222 | digitalWrite(ledpin[3], LOW);
|
223 | speakerpin.play(NOTE_C3, 300);
|
224 | delay(200);
|
225 | }
|
226 | delay(500);
|
227 | turn = -1; //Resets turn value so the game starts over without need for a reset button
|
228 | }
|