@@ -22,7 +22,6 @@ float threshold = 6.0;
2222
2323String data = " " ;
2424int flag = 0 ;
25- int sleep = 0 ;
2625int start = 0 ;
2726const unsigned long interval = 60000 ;
2827unsigned long previousMillis = 0 ;
@@ -41,6 +40,9 @@ Adafruit_BLEGatt gatt(Bluetooth);
4140void setup () {
4241 // put your setup code here, to run once:
4342 Serial.begin (115200 );
43+
44+ /* 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);
4446
4547 /* Initialise the module */
4648 Serial.print (F (" Initialising the Bluefruit LE module: " ));
@@ -73,7 +75,18 @@ void setup() {
7375 /* Wait for connection */
7476 Serial.println (" Looking for Bluetooth Device..." );
7577 while (!Bluetooth.isConnected ()){
76- delay (500 );
78+ unsigned long currentMillis = millis ();
79+ // Power down if 1 minute of no connection passes
80+ if ((unsigned long )(currentMillis - previousMillis) >= interval){
81+ Serial.println (" Powering Down!" );
82+ // Make the Bluetooth undiscoverable
83+ Bluetooth.sendCommandCheckOK (" AT+GAPDEVNAME=Undiscoverable" );
84+ // Turn off the Mode LED
85+ Bluetooth.sendCommandCheckOK (" AT+HWModeLED=0" );
86+ delay (500 );
87+ LowPower.powerDown (SLEEP_FOREVER, ADC_OFF, BOD_OFF);
88+ }
89+ delay (500 );
7790 }
7891
7992 // Change Mode LED Activity
@@ -91,6 +104,9 @@ void setup() {
91104
92105 Bluetooth.begin ();
93106
107+ // Initialize the characteristic to '0'
108+ gatt.setChar (1 , ' B' , BUFSIZE);
109+
94110 // accelerometer
95111 pinMode (9 ,INPUT); // Interrupt pin input
96112 pinMode (10 , OUTPUT); // CS for SPI
@@ -153,19 +169,24 @@ void loop() {
153169 while (Bluetooth.isConnected ()){
154170 unsigned long currentMillis = millis ();
155171
172+ // put device into idle mode
173+ LowPower.idle (SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
174+
156175 // reset sleep timer for first connection
157176 if (start == 1 ){
158177 previousMillis = currentMillis;
159178 start--;
179+ delay (500 );
160180 }
161181
162182 // reset sleep timer for reconnection
163183 if (flag == 1 ){
164184 Serial.println (F (" ******************************" ));
165185 Serial.println (F (" Bluetooth Device Connected!" ));
166186 Serial.println (F (" ******************************" ));
167- flag--;
168187 previousMillis = currentMillis;
188+ flag--;
189+ delay (500 );
169190 }
170191
171192 float maxG;
@@ -176,54 +197,36 @@ void loop() {
176197 }
177198
178199 // checks if threshold might have been exceeded, then verifies
179- gatt.setChar (1 , ' B' , BUFSIZE);
180200 if (digitalRead (9 ) == HIGH && (maxG = getMaxG ()) > threshold) {
181- if (sleep == 1 ){
182- Serial.println (" Woke Up!" );
183- sleep--;
184- }
185201 Serial.println (" Interrupt: " + String (maxG) + " g" );
186202 Serial.println (" Changing Characteristic!" );
187- // update characteristic value
203+ // change characteristic value to 'F'
188204 gatt.setChar (1 , ' A' , BUFSIZE);
189205 delay (1000 );
206+ Serial.println (" Resetting Characteristic!" );
207+ // reset characteristic value back to '0'
208+ gatt.setChar (1 , ' B' , BUFSIZE);
209+ delay (1000 );
210+ // reset the sleep timer
190211 previousMillis = currentMillis;
191212 }
192213
193- // sleep if a minute of inactivity passes
194- if ((unsigned long )(currentMillis - previousMillis) >= interval && (unsigned long )(currentMillis - previousMillis) < interval * 5 ){
195- if (sleep == 0 ){
196- Serial.println (" Going to Sleep!" );
197- sleep++;
198- delay (500 );
199- LowPower.idle (SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
200- }
201- }
202-
203- // power down if 5 minutes of inactivity passes
214+ // disconnect from bluetooth if 5 minutes of inactivity passes
204215 if ((unsigned long )(currentMillis - previousMillis) >= interval * 5 ){
205- Serial.println (" Powering Down!" );
216+ previousMillis = currentMillis;
217+ Bluetooth.disconnect ();
206218 delay (500 );
207- LowPower.powerDown (SLEEP_FOREVER, ADC_OFF, BOD_OFF);
208219 }
209220
210221 // check for incoming data from bluetooth device
211222 if (Bluetooth.available () > 0 ){
212- if (sleep == 1 ){
213- Serial.println (" Woke Up!" );
214- sleep--;
215- }
216223 data = Bluetooth.readString ();
217224 Serial.print (" Bluetooth: " + data);
218225 previousMillis = currentMillis;
219226 }
220227
221228 // check for incoming data from hardware
222229 if (Serial.available () > 0 ){
223- if (sleep == 1 ){
224- Serial.println (" Woke Up!" );
225- sleep--;
226- }
227230 data = Serial.readString ();
228231 Bluetooth.print (" Serial: " + data);
229232 Serial.println (" Message sent to bluetooth." );
@@ -233,13 +236,29 @@ void loop() {
233236
234237 // check if hardware disconnects from bluetooth device
235238 while (!Bluetooth.isConnected ()){
239+ unsigned long currentMillis = millis ();
240+
241+ // put device into idle mode
242+ LowPower.idle (SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
243+
236244 if (flag == 0 ){
237245 Serial.println (F (" ******************************" ));
238246 Serial.println (" Bluetooth Device Disconnected!" );
239247 Serial.println (F (" ******************************" ));
240248 flag++;
241249 }
242250 delay (500 );
251+
252+ // power down if 3 minutes of no connection passes
253+ if ((unsigned long )(currentMillis - previousMillis) >= interval * 3 ){
254+ Serial.println (" Powering Down!" );
255+ // Make the Bluetooth undiscoverable
256+ Bluetooth.sendCommandCheckOK (" AT+GAPDEVNAME=Undiscoverable" );
257+ // Turn off the Mode LED
258+ Bluetooth.sendCommandCheckOK (" AT+HWModeLED=0" );
259+ delay (500 );
260+ LowPower.powerDown (SLEEP_FOREVER, ADC_OFF, BOD_OFF);
261+ }
243262 }
244263}
245264
0 commit comments