-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathcontrol.ino
177 lines (154 loc) · 7.54 KB
/
control.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "M5Atom.h"
// `20200608a`
#define M0 22
#define M1 19
#define STP 23
#define DIR 33
float accX = 0, accY = 0, accZ = 0;
float accX_avg = 0, accY_avg = 0, accZ_avg = 0;
int n_average = 1;
bool IMU_ready = false;
int usdelay = 10;
int M0_state = 1;
int M1_state = 1;
int DIR_state = 1;
uint8_t numberImageData[750 + 2] = {
50, // width
5, // height
0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69,
0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3,
0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00,
0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69,
0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3,
0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00,
0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3,
0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00,
0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69,
0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3,
0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00,
0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69,
0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3,
0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69,
0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3,
0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00,
0x69, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69,
0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x69, 0xA3, 0x00, 0x00, 0x00
};
void setup()
{
M5.begin(true, false, true);
Serial.println("");
Serial.println("INIT IMU");
// check if IMU is ready
if (M5.IMU.Init() == 0) {
IMU_ready = true;
Serial.println("[OK] INIT ready");
} else {
IMU_ready = false;
Serial.println("[ERR] IMU INIT failed!");
}
delay(1000);
pinMode(M0, OUTPUT);
digitalWrite(M0, M0_state);
pinMode(M1, OUTPUT);
digitalWrite(M1, M1_state);
pinMode(DIR, OUTPUT);
digitalWrite(DIR, LOW);
ledcAttachPin(STP, 0); // broche 18, canal 0.
ledcSetup(0, 32*60/4, 12); // canal = 0, frequence = 5000 Hz, resolution = 12 bits
ledcWrite(0, 2048); // canal = 0, rapport cyclique = 2048 (half at 12bits)
//#pinMode(STP, OUTPUT);
//#digitalWrite(STP, LOW);
M5.dis.displaybuff(numberImageData);
}
int numberis = 0;
int CaseIs = 0;
void loop()
{
M5.IMU.getAccelData(&accX, &accY, &accZ);
if ((accY < -0.7) && (abs(accZ) < (0.4))) {
if (abs(accX) < 0.4) {
Serial.printf("Tilt avant\n");
numberis = 4 + M0_state * 1;
M5.dis.displaybuff(numberImageData, (9 - numberis) * 5, 0);
}
} else if ((accY > 0.7) && (abs(accZ) < (0.4))) {
if (abs(accX) < 0.4) {
Serial.printf("Tilt arriere\n");
numberis = 6 + M1_state * 1;
M5.dis.displaybuff(numberImageData, (9 - numberis) * 5, 0);
}
} else if ((accX < -0.7) && (abs(accZ) < (0.4))) {
if (abs(accY) < 0.4) {
Serial.printf("Tilt gauche\n");
numberis = 8 + DIR_state * 1;
M5.dis.displaybuff(numberImageData, (9 - numberis) * 5, 0);
}
} else {
numberis = 2;
M5.dis.displaybuff(numberImageData, (9 - numberis) * 5, 0);
}
if (M5.Btn.wasPressed())
{
if (numberis > 7) {
DIR_state = ((DIR_state + 1) % 2) ;
digitalWrite(DIR, !digitalRead(DIR));
} else if (numberis > 5) {
M1_state = ((M1_state + 1) % 2) ;
digitalWrite(M1, !digitalRead(M1));
} else if (numberis > 3) {
M0_state = ((M0_state + 1) % 2) ;
digitalWrite(M0, !digitalRead(M0));
} else if (numberis > 1) {
usdelay = ((usdelay + 10) % 100);
}
//numberis = (++numberis) % 6;
M5.dis.displaybuff(numberImageData, (9 - numberis) * 5, 0);
Serial.printf("numberis:%d\n", numberis);
Serial.printf("M0:%d\n", M0_state);
Serial.printf("M1:%d\n", M1_state);
Serial.printf("DIR:%d\n", DIR_state);
Serial.printf("usdelay:%d\n", usdelay);
Serial.printf("accX:%f\n", accX);
Serial.printf("accY:%f\n", accY);
Serial.printf("accZ:%f\n", accZ);
do
{
M5.update();
} while (M5.Btn.isPressed());
}else{
M5.dis.displaybuff(numberImageData, (9 - usdelay/10) * 5, 0);
}
M5.update();
delay(usdelay-10);
//digitalWrite(STP, !digitalRead(STP));
}