-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_game_using_remote.ino
More file actions
164 lines (147 loc) · 3.19 KB
/
memory_game_using_remote.ino
File metadata and controls
164 lines (147 loc) · 3.19 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
#include <IRremote.h>
#include <LiquidCrystal.h>
#define DECODE_NEC
#define IR_RECEIVE_PIN 2
LiquidCrystal lcd(13, 12, 3, 4, 5, 6);
IRrecv irReceiver(IR_RECEIVE_PIN);
decode_results results;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.print("Welcome!");
delay(2000);
lcd.clear();
lcd.print("Game is Starting");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press 1 for");
lcd.setCursor(0, 1);
lcd.print("Green");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press 2 for");
lcd.setCursor(0, 1);
lcd.print("Yellow");
delay(2000);
lcd.clear();
lcd.print("Press 3 for");
lcd.setCursor(0, 1);
lcd.print("Red");
delay(2000);
lcd.clear();
lcd.print("Press 4 for");
lcd.setCursor(0, 1);
lcd.print("Blue");
delay(2000);
lcd.clear();
irReceiver.enableIRIn();
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
randomSeed(analogRead(A0));
}
int generatedSequence[10];
int currentLength = 0;
int lengths[] = {3,3,3};
int sequenceCount = 0;
void generateSequence() {
if (sequenceCount >= sizeof(lengths) / sizeof(lengths[0])) {
return;
}
currentLength = lengths[sequenceCount];
for (int j = 0; j < currentLength; j++) {
int pin = random(8, 12);
generatedSequence[j] = pin;
digitalWrite(pin, HIGH);
delay(350);
digitalWrite(pin, LOW);
delay(350);
}
delay(2000);
sequenceCount++;
}
int playerInput[10];
int inputCount = 0;
void loop() {
lcd.setCursor(0, 0);
lcd.print("Memorize");
lcd.setCursor(0, 1);
lcd.print("sequence!");
delay(1000);
lcd.clear();
lcd.print("3...");
delay(1000);
lcd.clear();
lcd.print("2...");
delay(1000);
lcd.clear();
lcd.print("1!");
delay(1000);
lcd.clear();
lcd.print("GO!!!");
delay(500);
lcd.clear();
generateSequence();
delay(1000);
lcd.clear();
lcd.print("Now, your turn!");
while (inputCount < currentLength) {
if (irReceiver.decode()) {
int buttonPressed = ButtonToPin(irReceiver.decodedIRData.command);
if (buttonPressed != -1) {
digitalWrite(buttonPressed, HIGH);
delay(500);
digitalWrite(buttonPressed, LOW);
playerInput[inputCount++] = buttonPressed;
}
irReceiver.resume();
}
}
bool correct = true;
for (int i = 0; i < currentLength; i++) {
if (playerInput[i] != generatedSequence[i]) {
correct = false;
break;
}
}
lcd.clear();
if (correct) {
lcd.print("Correct!");
delay(2000);
} else {
lcd.print("Wrong!");
lcd.setCursor(0, 1);
lcd.print("Try again!");
delay(2000);
lcd.clear();
sequenceCount = 0;
inputCount = 0;
for (int i = 0; i < currentLength; i++) {
digitalWrite(generatedSequence[i], LOW);
}
return;
}
inputCount = 0;
if (sequenceCount >= sizeof(lengths) / sizeof(lengths[0])) {
lcd.clear();
lcd.print("Well done!");
delay(2000);
lcd.clear();
lcd.print("Game is");
lcd.setCursor(0, 1);
lcd.print("Finished!!");
delay(10000);
}
}
int ButtonToPin(int c) {
switch (c) {
case 16: return 8;
case 17: return 9;
case 18: return 10;
case 20: return 11;
default: return -1;
}
}