-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbomb_game_2.ino
More file actions
198 lines (150 loc) · 4.46 KB
/
bomb_game_2.ino
File metadata and controls
198 lines (150 loc) · 4.46 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
This game is a bomb game, A player should chose the correct wires to remove
Because you might cut off the wrong wire then pop the ballon!
The player recieve a clue based on the lights on in the bomb leds
It has also a timer which is a buzzer that the increase the sound if its comming to end!
If the player cut off the correct wires, Players Wins
*/
/*
Possible Combinations ;
(5, 6, 7), (5, 6, 8), (5, 6, 9), (5, 6, 10), (5, 7, 8)
(5, 7, 9), (5, 7, 10), (5, 8, 9), (5, 8, 10), (5, 9, 10)
(6, 7, 8), (6, 7, 9), (6, 7, 10), (6, 8, 9), (6, 8, 10)
(6, 9, 10), (7, 8, 9), (7, 8, 10), (7, 9, 10), (8, 9, 10)
*/
#include <Servo.h>
int wireSelections[20][3] = {
{5, 6, 7}, {5, 6, 8}, {5, 6, 9}, {5, 6, 10}, {5, 7, 8},
{5, 7, 9}, {5, 7, 10}, {5, 8, 9}, {5, 8, 10}, {5, 9, 10},
{6, 7, 8}, {6, 7, 9}, {6, 7, 10}, {6, 8, 9}, {6, 8, 10},
{6, 9, 10}, {7, 8, 9}, {7, 8, 10}, {7, 9, 10}, {8, 9, 10}
};
void delayServoMovement(Servo servo, int startPos , int endPos , int delayNum = 3){
for ( startPos ; startPos <= endPos ; startPos++ ){
servo.write(startPos);
delay(delayNum);
}
}
void buzzerSound( int pin , int volume){
tone(pin , volume);
}
bool isCutOff(int pin){
if ( digitalRead(pin) == HIGH){
return true;
}
return false;
}
class CustomSensor {
public:
int echo = 0;
int trig = 0;
int range = 0;
bool detected = false;
CustomSensor(int pin1, int pin2, int val) {
echo = pin1;
trig = pin2;
range = val;
}
void detect() {
// check if the an object enter the range
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
int duration = pulseIn(echo, HIGH);
int distance = duration * 0.034 / 2;
if (distance <= range) {
detected = true;
Serial.println("Game Starting ! " + (String)distance + " cm");
}
}
};
class CustomTimer{
public:
int timer_pin = 0;
int timer_time = 0;
int buzzer_sound = 0 , incresed_volume = 5;
CustomTimer(int pin , int timer){
timer_pin = pin;
timer_time = timer;
}
void decreaseTimer(){
buzzer_sound = buzzer_sound + incresed_volume;
timer_time = timer_time - 1;
}
void sound(){
::buzzerSound(timer_pin, buzzer_sound);
}
};
class GameWires{
public:
int wires[6];
int correct_wires[3], wrong_wires[3];
GameWires(int pins[6]){
for (int i = 0 ; i < 6 ; i++ ){
wires[i] = pins[i];
}
}
void setTheCorrectWires(){
int selected = random(0, 19);
for ( size_t i = 0 ; i < 3 ; i++){
correct_wires[i] = ::wireSelections[selected][i];
}
size_t wireUsed = 0; // check how many used wires to handle distributing wrong wires
for (size_t i = 0; i < 6 ; i++ ){
int correct = false; // check if in the correct_wires
for (size_t j = 0; j < 3 ; j++ ){
if (correct_wires[j] == wires[i]){
correct = true;
}
}
if (!correct && wireUsed < 3){
wrong_wires[wireUsed] = wires[i];
wireUsed = wireUsed + 1;
}
}
}
};
int ECHO = 2; // Use for sensor
int TRIGGER = 3; // Use for sensor
int POPPER_PIN = 4; // Use for servo
int WIRES[] = {5,6,7,8,9,10}; // Wires available
int BUZZER_PIN = 11; // Buzzer used for timer ( lower to higher sound)
int WIN_LEDS[] = {A0 , A1, A2}; // Leds for the winning strikes
int LOSE_LEDS[] = {A3 , A4, A5}; // Leds for the lossing strikes
int TIME = 6000; // The minimum time for playing
CustomSensor sensor(::ECHO, ::TRIGGER, 10); // sensor object
GameWires gWires(::WIRES); // wires game logic object
CustomTimer cTimer(::BUZZER_PIN, 20); // buzzer timer object
Servo popper;
int POPPER_START_POS = 0 , POPPER_END_POS = 90; // The angle of servo when popping a ballon
void setup() {
// A0 is not input so i can
Serial.begin(9600);
randomSeed(analogRead(A0));
int range = random(0, 1000);
for (int i = 0; i < range; i++) {
randomSeed(analogRead(A0));
}
for ( int pin : ::WIN_LEDS){
pinMode(pin , OUTPUT);
}
for ( int pin : ::LOSE_LEDS){
pinMode(pin , OUTPUT);
}
for ( int pin : ::WIRES){
pinMode(pin , INPUT);
}
pinMode(::BUZZER_PIN , OUTPUT);
pinMode(::TRIGGER, OUTPUT);
pinMode(::ECHO , INPUT);
::popper.attach(POPPER_PIN);
}
void loop() {
// put your main code here, to run repeatedly:
::popper.write(POPPER_START_POS); // Put the servo back to its position
::sensor.detect(); // Check if there is a player hand , then start the timer
if (::sensor.detected){
}
}