Skip to content

Commit d90dbfb

Browse files
committed
Fixed disconnection and characteristic bug
1 parent 3437d9e commit d90dbfb

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

arduino/arduino.ino

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ int flag = 0;
2525
int start = 0;
2626
const unsigned long interval = 60000;
2727
unsigned long previousMillis = 0;
28+
uint8_t signal0[] = {0x48};
29+
uint8_t signal1[] = {0x70};
2830

2931
Adafruit_BluefruitLE_SPI Bluetooth(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
3032
Adafruit_BLEGatt gatt(Bluetooth);
@@ -42,7 +44,7 @@ void setup() {
4244
Serial.begin(115200);
4345

4446
/* Put the device into idle mode */
45-
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
47+
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_OFF, USART0_OFF, TWI_OFF);
4648

4749
/* Initialise the module */
4850
Serial.print(F("Initialising the Bluefruit LE module: "));
@@ -72,6 +74,9 @@ void setup() {
7274
int char1 = Bluetooth.atcommand("AT+GATTADDCHAR=UUID128=00-11-22-33-44-55-66-77-88-99-AB-BC-CD-DE-EF-FF,PROPERTIES=0x10,MIN_LEN=1,VALUE=HELLO");
7375
Serial.println(char1);
7476

77+
// Reset Bluefruit for custom characteristic
78+
Bluetooth.reset();
79+
7580
/* Wait for connection */
7681
Serial.println("Looking for Bluetooth Device...");
7782
while (!Bluetooth.isConnected()){
@@ -80,10 +85,11 @@ void setup() {
8085
if((unsigned long)(currentMillis - previousMillis) >= interval){
8186
Serial.println("Powering Down!");
8287
// Make the Bluetooth undiscoverable
83-
Bluetooth.sendCommandCheckOK("AT+GAPDEVNAME=Undiscoverable");
88+
Bluetooth.setAdvData(NULL, 0);
8489
// Turn off the Mode LED
8590
Bluetooth.sendCommandCheckOK("AT+HWModeLED=0");
8691
delay(500);
92+
// Power down the device
8793
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
8894
}
8995
delay(500);
@@ -102,10 +108,7 @@ void setup() {
102108
Serial.println(F("******************************"));
103109
start++;
104110

105-
Bluetooth.begin();
106-
107-
// Initialize the characteristic to '0'
108-
gatt.setChar(1, 'B', BUFSIZE);
111+
delay(500);
109112

110113
//accelerometer
111114
pinMode(9,INPUT); // Interrupt pin input
@@ -170,11 +173,13 @@ void loop() {
170173
unsigned long currentMillis = millis();
171174

172175
//put device into idle mode
173-
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
176+
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_OFF, USART0_OFF, TWI_OFF);
174177

175178
//reset sleep timer for first connection
176179
if(start == 1){
177180
previousMillis = currentMillis;
181+
//initialize characteristic value to '0'
182+
gatt.setChar(1, signal0, sizeof(signal0));
178183
start--;
179184
delay(500);
180185
}
@@ -185,12 +190,15 @@ void loop() {
185190
Serial.println(F("Bluetooth Device Connected!"));
186191
Serial.println(F("******************************"));
187192
previousMillis = currentMillis;
193+
//initialize characteristic value to '0'
194+
gatt.setChar(1, signal0, sizeof(signal0));
188195
flag--;
189196
delay(500);
190197
}
191198

192199
float maxG;
193200

201+
//print max g value
194202
if(xl.newXData() || xl.newYData() || xl.newZData()){
195203
float maxG = getMaxG();
196204
Serial.println(maxG);
@@ -201,11 +209,11 @@ void loop() {
201209
Serial.println("Interrupt: " + String(maxG) + "g");
202210
Serial.println("Changing Characteristic!");
203211
//change characteristic value to 'F'
204-
gatt.setChar(1, 'A', BUFSIZE);
205-
delay(1000);
212+
gatt.setChar(1, signal1, sizeof(signal1));
213+
delay(3000);
206214
Serial.println("Resetting Characteristic!");
207215
//reset characteristic value back to '0'
208-
gatt.setChar(1, 'B', BUFSIZE);
216+
gatt.setChar(1, signal0, sizeof(signal0));
209217
delay(1000);
210218
//reset the sleep timer
211219
previousMillis = currentMillis;
@@ -239,7 +247,7 @@ void loop() {
239247
unsigned long currentMillis = millis();
240248

241249
//put device into idle mode
242-
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
250+
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_OFF, USART0_OFF, TWI_OFF);
243251

244252
if(flag == 0){
245253
Serial.println(F("******************************"));
@@ -253,26 +261,52 @@ void loop() {
253261
if((unsigned long)(currentMillis - previousMillis) >= interval * 3){
254262
Serial.println("Powering Down!");
255263
// Make the Bluetooth undiscoverable
256-
Bluetooth.sendCommandCheckOK("AT+GAPDEVNAME=Undiscoverable");
264+
Bluetooth.setAdvData(NULL, 0);
257265
// Turn off the Mode LED
258266
Bluetooth.sendCommandCheckOK("AT+HWModeLED=0");
259267
delay(500);
268+
// Power down the device
260269
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
261270
}
262271
}
263272
}
264273

274+
/**
275+
* @brief Error handler for Arduino methods
276+
*
277+
* @param err error message string
278+
*
279+
* @return void returns nothing
280+
*/
281+
265282
void error(const __FlashStringHelper*err){
266283
Serial.println(err);
267284
while (1);
268285
}
269286

287+
/**
288+
* @brief Convert sensor reading into an integer value
289+
*
290+
* @param maxScale maximum scale for g-force
291+
*
292+
* @param g minimum g on individual axis
293+
*
294+
* @return integer value of sensor reading
295+
*/
296+
270297
int convertToReading(int maxScale, float g) {
271298
int reading = int((g * 2047) / maxScale);
272299
return reading;
273300
}
274301

275-
//calculates maximum g-force in any direction
302+
/**
303+
* @brief Calculates maximum g-force in any direction
304+
*
305+
* @param void takes nothing
306+
*
307+
* @return maximum g-force
308+
*/
309+
276310
float getMaxG() {
277311
int16_t x, y, z;
278312

@@ -282,8 +316,6 @@ float getMaxG() {
282316
float yg = xl.convertToG(scale,y);
283317
float zg = xl.convertToG(scale,z);
284318

285-
//Serial.println(String(xg) + " " + String(yg) + " " + String(zg));
286-
287319
float maxG = sqrt(pow(xg, 2) + pow(yg, 2) + pow(zg, 2)); //pythagoream theorem for three dimensions
288320

289321
return maxG;

0 commit comments

Comments
 (0)