-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancing_game.ino
151 lines (127 loc) · 4.17 KB
/
balancing_game.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
/*
This is a balancing game, made with an Arduino an accelerometer and some LEDs
Put all the circuitry on a plank and put the plank on a tube, have your victim
stand on the plank and make them balance, if they stay balanced for long enough
the LEDs play the "win" animation, if they lose balance they lose all their progress.
The score logic:
* "counter" is set to the pin of the first LED, and is used to turn on the LEDs
* state is set to 1, 1 = tilted, 0 = balanced
* If you are balanced "state" is set to 0
* While "state" is 0, set "counter" to HIGH and increment counter
there is a 1 second delay in the while loop.
* If you stay balanced till all the LEDs light up, do the "win" animation
set state to 1 and reset counter to the pin of the first LED
* If you lose your balance, the LEDs are turned off, state is set to 1
and counter reset to the pin of the firs LED.
This code only supports the MPU 6050 accelerometer/gyroscope sensor.
Though porting it to other sensors should be pretty easy.
This codes github repo: https://github.com/sit-on-cushions-we-must/balancing_game
Wiring diagrams and stuff is here:
This code is licensed under the MIT license: https://mit-license.org/
*/
#include<Wire.h>
// the LEDs
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int led5 = 6;
const int led6 = 7;
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcY; // used to store the accelerometer Y axis readings.
int state = 1; // used to store the state of the sensor, 1 = tilted, 0 = balanced.
int counter = led1; // used to light up the LEDs
void setup() {
// power on and wake up the sensor
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop() {
// request readings from the sensor
Wire.beginTransmission(MPU_addr);
Wire.write(0x3D); // starting with register 0x3D (ACCEL_YOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
// read the Y axis value
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
// for debugging
Serial.print("AcY = "); Serial.println(AcY);
/*
check if the sensor is balanced
if it is set state to 0 if it isn't
set state to 1 and turn off all the LEDs
*/
if (AcY <= 1500 && AcY >= -1500) {
state = 0;// set "state" to 0
Serial.println("balanced");
} else {
// set state to 1
Serial.println("tilted");
state = 1;
// reset counter
counter = led1;
// turn off all the LEDs
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
}
/*
while the sensor is balanced,
turn on the LEDs one after the other
*/
while(state == 0) {
digitalWrite(counter, HIGH);
delay(1000);
counter ++;
break; // break out of the loop so we can read the accelerometer
}
// if all the LEDs have been turned on
if (counter > (led6 + 1)) {
// reset counter
counter = led1;
// set state to 1
state = 1;
// play the "you win" animation
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
delay(300);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
delay(300);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
delay(300);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
delay(300);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
}
}