@@ -46,15 +46,22 @@ const int inputCount = sizeof(inputs) / sizeof(inputs[0]);
4646byte outputs[] = {4 , 14 , 15 , 27 , 26 }; // row
4747const int outputCount = sizeof (outputs) / sizeof (outputs[0 ]);
4848
49- // Auto sleep after idle params
50- long previousMillis = 0 ;
51- unsigned long currentMillis = 0 ;
52- const long INTERVAL = 10 * 60 * 1000 ;
49+ // Auto sleep timer
50+ long sleepPreviousMillis = 0 ;
51+ unsigned long sleepCurrentMillis = 0 ;
52+ const long SLEEP_INTERVAL = 10 * 60 * 1000 ;
5353
54- // Check battery interval
54+ // Battery timer
5555long batteryPreviousMillis = 0 ;
5656unsigned long batteryCurrentMillis = 0 ;
57- const long BATTERY_INTERVAL = 60 * 1000 ;
57+ const long BATTERY_INTERVAL = 5 * 1000 ;
58+
59+ // Low battery LED blink timer
60+ long ledPreviousMillis = 0 ;
61+ unsigned long ledCurrentMillis = 0 ;
62+ const long LED_INTERVAL = 5 * 1000 ;
63+
64+ bool isLowBattery = false ;
5865
5966// Function declaration
6067void initKeys ();
@@ -126,6 +133,7 @@ void loop() {
126133 while (bleKeyboard.isConnected ()) {
127134 checkIdle ();
128135 checkBattery ();
136+ showLowBatteryWarning ();
129137 for (int r = 0 ; r < ROWS; r++) {
130138 digitalWrite (outputs[r], LOW); // Setting one row low
131139 for (int c = 0 ; c < COLS; c++) {
@@ -272,8 +280,10 @@ void showBatteryState() {
272280 Serial.println (result);
273281 u8g2.sendBuffer ();
274282
275- if (batteryPercentage < 20 ) {
276- showLowBatteryWarning ();
283+ if (batteryPercentage <= 20 ) {
284+ isLowBattery = true ;
285+ } else {
286+ isLowBattery = false ;
277287 }
278288
279289 delay (100 );
@@ -369,8 +379,8 @@ void goSleeping() {
369379 *
370380 */
371381void checkIdle () {
372- currentMillis = millis ();
373- if (currentMillis - previousMillis > INTERVAL ) {
382+ sleepCurrentMillis = millis ();
383+ if (sleepCurrentMillis - sleepPreviousMillis > SLEEP_INTERVAL ) {
374384 goSleeping ();
375385 }
376386}
@@ -392,13 +402,30 @@ void checkBattery() {
392402 *
393403 */
394404void showLowBatteryWarning () {
405+ if (!isLowBattery) {
406+ tp.DotStar_SetPower (false );
407+ return ;
408+ }
395409 tp.DotStar_SetPower (true );
396- tp.DotStar_SetBrightness (5 );
397- tp.DotStar_SetPixelColor (255 , 0 , 0 );
410+ ledCurrentMillis = millis ();
411+ if (ledCurrentMillis - ledPreviousMillis > 1000 &&
412+ ledCurrentMillis - ledPreviousMillis <= 1200 ) {
413+ tp.DotStar_SetBrightness (5 );
414+ tp.DotStar_SetPixelColor (255 , 0 , 0 );
415+ } else if (ledCurrentMillis - ledPreviousMillis > 1200 &&
416+ ledCurrentMillis - ledPreviousMillis <= 1300 ) {
417+ tp.DotStar_SetPixelColor (0 , 0 , 0 );
418+ } else if (ledCurrentMillis - ledPreviousMillis > 1300 &&
419+ ledCurrentMillis - ledPreviousMillis <= 1500 ) {
420+ tp.DotStar_SetPixelColor (255 , 0 , 0 );
421+ } else if (ledCurrentMillis - ledPreviousMillis > 1700 ) {
422+ tp.DotStar_SetPixelColor (0 , 0 , 0 );
423+ ledPreviousMillis = ledCurrentMillis;
424+ }
398425}
399426
400427/* *
401- * Update previousMillis ' value to reset idle timer
428+ * Update sleepPreviousMillis ' value to reset idle timer
402429 *
403430 */
404- void resetIdle () { previousMillis = currentMillis ; }
431+ void resetIdle () { sleepPreviousMillis = sleepCurrentMillis ; }
0 commit comments