-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBLE-Remote.ino
149 lines (134 loc) · 3.59 KB
/
BLE-Remote.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
/*
First HM-10 setup with USB-Serial converter
1. Flash new firmware (v700+)
2. AT (responds: OK)
3. AT+IMME1
4. AT+ROLE1
5. AT+NOTI1
6. AT+DISC? (with proto helmet running)
7. AT+CONN0 (0 means index of found proto BLE)
Now you can connect the HM-10 to the Arduino
For the first time hold 4,6,7 pin low when powering up and wait 20s. (resets button values to index 1)
*/
#include <ezButton.h>
#include <EEPROM.h>
#define BTNTIME 1500
#define BUTTONS 7
ezButton buttonArray[BUTTONS] = {
ezButton(2), //left
ezButton(8), //right
ezButton(3), //up
ezButton(6), //down
ezButton(5), //center
ezButton(7), //up right
ezButton(4) //up left
};
unsigned long btnPressTime[BUTTONS];
int btnVal[BUTTONS];
bool canSend = false, recon = false, requestBLEsave = false;
String dataIn;
int numOfAnims = 4;
unsigned long reconTime = 0,requestBLEsaveTime = 0;
void setup() {
Serial.begin(115200);
delay(200);
Serial1.begin(115200);
//while (!Serial) ;
for (int i = 0; i < BUTTONS; i++) {
buttonArray[i].setDebounceTime(50);
btnVal[i] = EEPROM.read(i);
btnPressTime[i] = 20000000;
}
sendS("AT+START");
}
void sendS(String temp) { //send string
Serial1.print(temp);
Serial.print("Sending->");
Serial.println(temp);
}
void sendI(int temp) { //send integer
Serial1.print(temp);
Serial.print("Sending->");
Serial.println(temp);
}
void changeBtn(int toChange) { //change button value
if(btnVal[toChange] == numOfAnims) {
btnVal[toChange] = 1;
} else {
btnVal[toChange]++;
}
sendI(btnVal[toChange]);
requestBLEsaveTime = millis();
requestBLEsave = true;
}
void loop() {
for (int i = 0; i < BUTTONS; i++) {
buttonArray[i].loop();
}
dataIn = "";
if(Serial1.available() > 0){ //serial read and process
dataIn = Serial1.readStringUntil('\n');
}
if (dataIn != "") { //try to connect
dataIn.trim();
Serial.println(dataIn);
if(dataIn == "OK+CONN") {
Serial.println("-CONN");
sendS("AT+NOTIFY_ON000C");
recon = false;
} else if(dataIn == "OK+DATA-OK") {
Serial.println("-DATA-OK");
sendS("AT+SET_WAYWR000C");
} else if(dataIn == "OK+SEND-OK") {
Serial.println("-SEND-OK");
sendS("g");
canSend = true;
} else if(dataIn == "OK+LOST") {
Serial.println("-LOST");
canSend = false;
sendS("AT+CONNL");
recon = true;
reconTime = millis();
} else if(dataIn.charAt(0) == 'i') {
dataIn.remove(0,1);
numOfAnims = dataIn.toInt();
Serial.println(numOfAnims);
}
}
if(recon && !canSend && reconTime+2000 < millis()) { //reconnect
sendS("AT+START");
reconTime = millis();
}
if(requestBLEsaveTime+20000 < millis() && requestBLEsave) { //save
for (int i = 0; i < BUTTONS; i++) {
EEPROM.write(i, btnVal[i]);
}
requestBLEsave = false;
Serial.println("saved");
}
for (int i = 0; i < BUTTONS; i++) {
if(buttonArray[i].isPressed() && canSend) { //detect press
btnPressTime[i] = millis();
}
if(buttonArray[i].isReleased() && canSend) { //short press
long pressDuration = millis() - btnPressTime[i];
if(pressDuration < BTNTIME) {
sendI(btnVal[i]);
btnPressTime[i] = 20000000;
}
}
if(btnPressTime[i]+BTNTIME < millis() && canSend) { //long press
btnPressTime[i] = 20000000;
changeBtn(i);
}
}
if(!canSend && !buttonArray[3].getState() && !buttonArray[5].getState() && !buttonArray[6].getState()) {
for (int i = 0; i < BUTTONS; i++) {
btnVal[i] = 1;
}
requestBLEsaveTime = millis();
requestBLEsave = true;
Serial.println("reset");
}
dataIn = "";
}