Snippets

Peter Scargill Uninterruptible for Pi and other boards - using Atmega 328 processor

Updated by Peter Scargill

File uninterruptible.c Modified

  • Ignore whitespace
  • Hide word diff
 #include <EEPROM.h>
 #include <dht.h>
 
-
-
-dht DHT;   // just testing
-#define DHT22_PIN 11
-
-
 // UPS (un-interruptible supply) software
 // Peter Scargill 2017  - https://tech.scargill.net
 //
 // Blog has more - as does this video https://www.youtube.com/watch?v=44Lvdf7o4GQ
 // Now set up for the 32-pixel displays - got everything down to 4 lines
 
-#define VER "v1.14"
+
+dht DHT;   // temperature sensing
+SSD1306AsciiWire oled;
+
+#define VER "v1.15"
+#define I2C_ADDRESS 0x3C
+#define OPTIMIZE_I2C 1
+
 struct
 {
   uint16_t check;
 } eeSave;
 
 
-
-
-// Device address for the SSD1306 display
-#define I2C_ADDRESS 0x3C
-#define OPTIMIZE_I2C 1
-
-SSD1306AsciiWire oled;
-
-#define POWER 12  // inverted - ON - power available for my cheap version
-#define WARNING 5 // inverted
-#define TONE 13   // optional piezo beeper on warning
-#define WATCHDOG 11  //  watchdog in
+// pins
+#define WATCHDOG 3  //  watchdog in - looking for change of state - normally pulled high
+#define SET 4       // button
+#define WARNING 5   // inverted output - when it goes LOW, PC should shut down
+#define UP 6        // button
+#define DOWN 7      // button
+#define CHARGER_STDBY 8
+#define CHARGER_CHRG 9
+#define CHARGER_CE 10
+#define DHT22_PIN 11 // temperature sensor
+#define POWER 12    // inverted - ON - power to MOSFET
+#define TONE 13     // optional mini beeper for warning
+
+
+#define RPROG A0
+#define VSOLAR  A1
+#define VSUPPLY A2
+#define BOOST_ENABLE A3
+//  A4 and A5 are SDA and SCL for I2c
+#define ICHARGE A6
+#define VBAT A7  // voltage reading via 2k2 to ground and 10k to battery+ (maybe should be 22k and 100k)
 
 #define POWERUP_STATE 0
 #define GOOD_STATE 1
 #define SHOWGRAPH 10     // 10 seconds showing graphical info
 #define SHUTDOWN_PERIOD 20
 
-#define SET 4
-#define UP 6
-#define DOWN 7
-
-#define VOLTAGE A7
-
 uint8_t showGraph = 0;
 
 uint32_t secs = 0;
   pinMode(UP, INPUT_PULLUP);
   pinMode(DOWN, INPUT_PULLUP);
   pinMode(WATCHDOG, INPUT_PULLUP);
-  average = analogRead(VOLTAGE); // we start off with read value;
+  average = analogRead(VBAT); // we start off with read value;
   oled.clear();
   my100millis = millis() + 1000;
   state = 0;
           LcdP("Temp warning=%dc", eeSave.twarn); oled.clearToEOL();
           updated = 1;
         }
-        break;        
+        break;
       case 13:  if (updated == 0)
         {
           gotoXY(0, 3);
 
     int chk = DHT.read22(DHT22_PIN);
 
-    average = ((average * 7) + analogRead(VOLTAGE)) / 8; //i.e. average over 8 seconds 0-1023 = 0-5v - might make testing longer in the end
+    average = ((average * 7) + analogRead(VBAT)) / 8; //i.e. average over 8 seconds 0-1023 = 0-5v - might make testing longer in the end
     vol = (((float)average) * 5.0 / 824.0) * (float)eeSave.batteryOffset / 400.0; //10k/2k2
 
     if (eeSave.pause) {
             state = TIMEOUT_STATE;
           }
 
-          gotoXY(0, 1); 
+          gotoXY(0, 1);
           if (watchdogTimer) LcdP("WDT: %d:%02d ", watchdogTimer, 59 - secondsCounter);
           else
           {
             temperatureReading = (int)DHT.temperature;
             humidityReading = (int)DHT.humidity;
-            if (chk==DHTLIB_OK)
+            if (chk == DHTLIB_OK)
             {
               if (mySecs & 2) LcdP("Tem=%dc ", temperatureReading); else LcdP("Hum=%d%% ", humidityReading);
-              if (temperatureReading>eeSave.twarn) tone(TONE, eeSave.tone, 100);
+              if (temperatureReading > eeSave.twarn) tone(TONE, eeSave.tone, 100);
             }
           }
           LcdP("Bat="); showBattery();
Updated by Peter Scargill

File uninterruptible.c Modified

  • Ignore whitespace
  • Hide word diff
       case 12: if (updated == 0)
         {
           gotoXY(0, 3);
-          LcdP("Twarning=%d%%", eeSave.twarn); oled.clearToEOL();
+          LcdP("Temp warning=%dc", eeSave.twarn); oled.clearToEOL();
           updated = 1;
         }
         break;        
Updated by Peter Scargill

File uninterruptible.c Modified

  • Ignore whitespace
  • Hide word diff
 #include "SSD1306Ascii.h"
 #include "SSD1306AsciiWire.h"
 #include <EEPROM.h>
+#include <dht.h>
+
+
+
+dht DHT;   // just testing
+#define DHT22_PIN 11
+
 
 // UPS (un-interruptible supply) software
 // Peter Scargill 2017  - https://tech.scargill.net
 // Blog has more - as does this video https://www.youtube.com/watch?v=44Lvdf7o4GQ
 // Now set up for the 32-pixel displays - got everything down to 4 lines
 
-#define VER "v1.13"
+#define VER "v1.14"
 struct
 {
   uint16_t check;
   uint8_t smon;
   uint8_t watchdog;
   uint16_t pause;
-  
+  uint8_t twarn;
+
 } eeSave;
 
+
+
+
 // Device address for the SSD1306 display
 #define I2C_ADDRESS 0x3C
 #define OPTIMIZE_I2C 1
 
 #define VOLTAGE A7
 
-uint8_t showGraph=0;
+uint8_t showGraph = 0;
 
 uint32_t secs = 0;
 uint32_t upSecs = 0;
 uint8_t watchdogTimer = 0;
 uint8_t watchdogState = 0;
 uint8_t secondsCounter = 0;
-uint8_t refresh=0;
+uint8_t refresh = 0;
 
 void LcdP(char *fmt, ...)
 {
 uint8_t onDelay = 0;
 
 uint16_t average;
-
+int temperatureReading;
+int humidityReading;
 uint8_t updateTimeout = 0;
 
 char *theState = "standby";
 uint8_t dKeypressed = 0;
 
 uint8_t logit[128];
-uint16_t logPause=0;
+uint16_t logPause = 0;
 
 void gotoXY(int x, int y)
 {
 {
   EEPROM.get(0, eeSave);
   analogReference(INTERNAL);
-  if (eeSave.check != 0x52d3)
+  if (eeSave.check != 0x52d1)
   {
-    eeSave.check = 0x52d3;
+    eeSave.check = 0x52d1;
 
     eeSave.highV = SWITCHON;
     eeSave.lowV = SWITCHOFF;
     eeSave.tone = 0;
     eeSave.smon = 0;
     eeSave.watchdog = 0;
-    eeSave.pause=0;
+    eeSave.pause = 0;
+    eeSave.twarn = 40;
     EEPROM.put(0, eeSave);
   }
 
         watchdogState = digitalRead(WATCHDOG);
       }
     }
-     if (digitalRead(SET) == 1)
+    if (digitalRead(SET) == 1)
     {
       if (sKeypressed)
-      {  if (sKeypressed<LONGPRESS_SET) 
-          {
-            if (showGraph) { showGraph=0; oled.clear(); instate=255              ; sKeypressed=0;  refresh=1; }
-            instate++;
-            updated = 0;
-            updateTimeout = 100;
+      { if (sKeypressed < LONGPRESS_SET)
+        {
+          if (showGraph) {
+            showGraph = 0;
+            oled.clear();
+            instate = 255              ;
+            sKeypressed = 0;
+            refresh = 1;
           }
-       sKeypressed=0;
+          instate++;
+          updated = 0;
+          updateTimeout = 100;
+        }
+        sKeypressed = 0;
       }
     }
     else
-      {
-        sKeypressed++; 
-        if (sKeypressed==LONGPRESS_SET) showGraph = SHOWGRAPH;
-        if (sKeypressed>>LONGPRESS_SET) sKeypressed--;
-      }
+    {
+      sKeypressed++;
+      if (sKeypressed == LONGPRESS_SET) showGraph = SHOWGRAPH;
+      if (sKeypressed >> LONGPRESS_SET) sKeypressed--;
+    }
 
 
     if (digitalRead(UP) == 1) // special case long press is reset...
     {
       if (uKeypressed)
       {
-       if (uKeypressed<LONGPRESS_UP) 
-       {
+        if (uKeypressed < LONGPRESS_UP)
+        {
           switch (instate)
           {
             case 1 : if (eeSave.highV < 4.3) eeSave.highV += 0.1; break;
             case 9 : eeSave.smon = 1; break;
             case 10 : eeSave.watchdog++;
               watchdogTimer = eeSave.watchdog; watchdogTrigger = 0; break;
-            case 11: eeSave.pause++; break;
+            case 11: if (eeSave.pause < 5) eeSave.pause++; break;
+            case 12: if (eeSave.twarn < 99) eeSave.twarn++; break;
           }
           updated = 0;
           updateTimeout = 100;
         }
-        uKeypressed=0;
+        uKeypressed = 0;
       }
     }
     else
     {
       uKeypressed++;
-      if (uKeypressed==LONGPRESS_UP) triggerShutdown = 1;
-      if (uKeypressed>LONGPRESS_UP) uKeypressed--;                                                                         
+      if (uKeypressed == LONGPRESS_UP) triggerShutdown = 1;
+      if (uKeypressed > LONGPRESS_UP) uKeypressed--;
     }
 
     if (digitalRead(DOWN) == 1)
     {
       if (dKeypressed)
       {
-        if (dKeypressed<LONGPRESS_DOWN)
+        if (dKeypressed < LONGPRESS_DOWN)
         {
           switch (instate)
           {
             case 10 : if (eeSave.watchdog) eeSave.watchdog--;
               watchdogTimer = eeSave.watchdog; watchdogTrigger = 0; break;
             case 11 : if (eeSave.pause) eeSave.pause--; break;
+            case 12 : if (eeSave.twarn) eeSave.twarn--; break;
           }
           updated = 0;
           updateTimeout = 100;
         }
-       dKeypressed = 0;
+        dKeypressed = 0;
       }
     }
     else
     {
       dKeypressed++;
-      if (dKeypressed==LONGPRESS_DOWN) average=0;
-      if (dKeypressed>LONGPRESS_DOWN) dKeypressed--;
+      if (dKeypressed == LONGPRESS_DOWN) average = 0;
+      if (dKeypressed > LONGPRESS_DOWN) dKeypressed--;
     }
 
     if (eeSave.highV <= eeSave.lowV) eeSave.highV = eeSave.lowV + 0.1;
           LcdP("Serial mon On/Off: %d", eeSave.smon); oled.clearToEOL();
           updated = 1;
         }
+
         break;
       case 10: if (updated == 0)
         {
       case 11:  if (updated == 0)
         {
           gotoXY(0, 3);
-          LcdP("Log period=%d mins",eeSave.pause); oled.clearToEOL();
+          switch (eeSave.pause)
+          {
+            case 0 : LcdP("Log period=NONE"); break;
+            case 1 : LcdP("Log period=1 sec"); break;
+            case 2 : LcdP("Log period=10 secs"); break;
+            case 3 : LcdP("Log period=1 min"); break;
+            case 4 : LcdP("Log period=10 mins"); break;
+            case 5 : LcdP("Log period=1 hr"); break;
+            default : LcdP("Log period=??"); break;
+          }
+          oled.clearToEOL();
+          updated = 1;
+        }
+        break;
+      case 12: if (updated == 0)
+        {
+          gotoXY(0, 3);
+          LcdP("Twarning=%d%%", eeSave.twarn); oled.clearToEOL();
           updated = 1;
         }
-        break;  
-      case 12:  if (updated == 0)
+        break;        
+      case 13:  if (updated == 0)
         {
           gotoXY(0, 3);
           LcdP("Version=%s", VER); oled.clearToEOL();
           updated = 1;
         }
-        break;     
-      case 13:  updated = 0; instate = 0; break;
+        break;
+      case 14:  updated = 0; instate = 0; break;
     }
 
     if (updateTimeout)
   if (mymillis <= millis())
   {
     mymillis += 1000; // every second we do this
+
+    int chk = DHT.read22(DHT22_PIN);
+
     average = ((average * 7) + analogRead(VOLTAGE)) / 8; //i.e. average over 8 seconds 0-1023 = 0-5v - might make testing longer in the end
     vol = (((float)average) * 5.0 / 824.0) * (float)eeSave.batteryOffset / 400.0; //10k/2k2
-    if (eeSave.pause) { if ((mySecs%60)==0) logPause++; } else logPause++; 
+
+    if (eeSave.pause) {
+      if ((mySecs % 60) == 0) logPause++;
+    } else logPause++;
+
+    if (eeSave.pause == 1) {
+      logPause++;  // 1 second
+    }
+    else if (eeSave.pause == 2) {
+      if ((mySecs % 10) == 0) logPause++;  // 10 seconds
+    }
+    else if (eeSave.pause == 3) {
+      if ((mySecs % 60) == 0) logPause++;  // 1 minute
+    }
+    else if (eeSave.pause == 4) {
+      if ((mySecs % 600) == 0) logPause++;  // 10 minutes
+    }
+    else if (eeSave.pause == 5) {
+      if ((mySecs % 3600) == 0) logPause++;  // 1 hour
+    }
+
     mySecs++;
-    if (logPause>eeSave.pause) // log 128 values 0-30 = 0-5v  -  2 secs updating for testing
+    if (logPause > eeSave.pause) // log 128 values 0-30 = 0-5v  -  2 secs updating for testing
     {
-      logPause=0; 
-      for (int a=0;a<127;a++) logit[a]=logit[a+1];
-      logit[126]=((int)(vol*6.0));
+      logPause = 0;
+      for (int a = 0; a < 127; a++) logit[a] = logit[a + 1];
+      logit[126] = ((int)(vol * 6.0));
     }
 
     if (triggerShutdown) {
 
     if (showGraph)
     {
-      if (showGraph==SHOWGRAPH) 
+      if (showGraph == SHOWGRAPH)
+      {
+        uint8_t val;
+        oled.clear();
+        oled.setCursor(0, 0);
+        for (int a = 0; a < 128; a++)
         {
-          uint8_t val;
-          oled.clear();
-          oled.setCursor(0,0);
-          for (int a=0;a<128;a++)
-            {
-              uint32_t point=(0x80000000>>logit[a])|0x80000001;
-              if ((a==0)||(a==127)) point=0xffffffff;
-              oled.setCursor(a,0); oled.ssd1306WriteRamBuf((uint8_t)((point)&255));
-              oled.setCursor(a,1); oled.ssd1306WriteRamBuf((uint8_t)((point>>8)&255));;
-              oled.setCursor(a,2); oled.ssd1306WriteRamBuf((uint8_t)((point>>16)&255));
-              oled.setCursor(a,3); oled.ssd1306WriteRamBuf((uint8_t)((point>>24)&255));              
-            } 
-          oled.setCursor(0,0);    
+          //uint32_t point=(0x80000000>>logit[a])|0x80000001;
+
+          uint32_t point = (((0x80000000 >> logit[a]) - 1) ^ 0xffffffff) | 0x80000001;
+
+          if ((a == 0) || (a == 127)) point = 0xffffffff;
+          oled.setCursor(a, 0); oled.ssd1306WriteRamBuf((uint8_t)((point) & 255));
+          oled.setCursor(a, 1); oled.ssd1306WriteRamBuf((uint8_t)((point >> 8) & 255));;
+          oled.setCursor(a, 2); oled.ssd1306WriteRamBuf((uint8_t)((point >> 16) & 255));
+          oled.setCursor(a, 3); oled.ssd1306WriteRamBuf((uint8_t)((point >> 24) & 255));
         }
-      showGraph--; if (showGraph==0) refresh=1;
+        oled.setCursor(0, 0);
+      }
+      showGraph--; if (showGraph == 0) refresh = 1;
     }
     else
     {
-    switch (state)
-    {
-      case POWERUP_STATE : // powered up everything off
-        secondsCounter = 0;
-        if ((stateCounter == eeSave.startTimeout) || (refresh)) {
-          refresh=0; 
-          gotoXY(0, 0);
-          LcdP("Starting up..");
-          oled.clearToEOL(); 
-        }
-        if (stateCounter) --stateCounter;
-        gotoXY(0, 1); LcdP("Bat="); showBattery();
-        gotoXY(0, 2); LcdP("Wait=%03d secs", stateCounter); oled.clearToEOL();
-        digitalWrite(WARNING, WARNINGON); // no warning
-        digitalWrite(POWER, POWEROFF);
-        if (vol > eeSave.highV)
-        {
-          if (stateCounter == 0) {
-            stateCounter = eeSave.goodTimeout;
-            state = GOOD_STATE;
+      switch (state)
+      {
+        case POWERUP_STATE : // powered up everything off
+          secondsCounter = 0;
+          if ((stateCounter == eeSave.startTimeout) || (refresh)) {
+            refresh = 0;
+            gotoXY(0, 0);
+            LcdP("Starting up..");
+            oled.clearToEOL();
           }
-        } else
-        {
-          if (stateCounter == 0) {
-            stateCounter = eeSave.offTimeout;
-            state = OFF_STATE;
+          if (stateCounter) --stateCounter;
+          gotoXY(0, 1); LcdP("Bat="); showBattery();
+          gotoXY(0, 2); LcdP("Wait=%03d secs", stateCounter); oled.clearToEOL();
+          digitalWrite(WARNING, WARNINGON); // no warning
+          digitalWrite(POWER, POWEROFF);
+          if (vol > eeSave.highV)
+          {
+            if (stateCounter == 0) {
+              stateCounter = eeSave.goodTimeout;
+              state = GOOD_STATE;
+            }
+          } else
+          {
+            if (stateCounter == 0) {
+              stateCounter = eeSave.offTimeout;
+              state = OFF_STATE;
+            }
           }
-        }
-        break;
-
-      case GOOD_STATE :  // battery is above upper threshold
-        if ((stateCounter == eeSave.goodTimeout) || (refresh)) {
-          refresh=0;
-          gotoXY(0, 0);
-          LcdP("** Pi Power %s **", VER);
-          oled.clearToEOL();
-          watchdogTimer = eeSave.watchdog;
-        }
-        if (stateCounter) --stateCounter;
-        if (++secondsCounter == 60)
-        {
-          secondsCounter = 0;
-          if (watchdogTimer) {
-            watchdogTimer--;  // only do this when battery is ok
-            if (watchdogTimer == 0) watchdogTrigger = 1;
+          break;
+
+        case GOOD_STATE :  // battery is above upper threshold
+          if ((stateCounter == eeSave.goodTimeout) || (refresh)) {
+            refresh = 0;
+            gotoXY(0, 0);
+            LcdP("** Pi Power %s **", VER);
+            oled.clearToEOL();
+            watchdogTimer = eeSave.watchdog;
           }
-        }
-        if (watchdogTrigger) {
-          watchdogTrigger = 0;  // reboot
-          stateCounter = eeSave.warningTimeout;
-          state = TIMEOUT_STATE;
-        }
- 
-        gotoXY(0, 1); if (watchdogTimer) LcdP("WDT: %d:%02d ",watchdogTimer,59-secondsCounter); 
-                      LcdP("Bat: "); showBattery();
-        gotoXY(0, 2); LcdP("HiV=%d.%dv LoV=%d.%dv", int(eeSave.highV), ((int(eeSave.highV * 100)) % 100) / 10, int(eeSave.lowV), ((int(eeSave.lowV * 100)) % 100) / 10); oled.clearToEOL();
-        digitalWrite(WARNING, WARNINGOFF); // no warning
-        digitalWrite(POWER, POWERON);
-        if (vol < eeSave.lowV)
-        {
-          gotoXY(0, 3); LcdP("Wait=%03d secs", stateCounter); oled.clearToEOL();
-          if (stateCounter == 0) {
+          if (stateCounter) --stateCounter;
+          if (++secondsCounter == 60)
+          {
+            secondsCounter = 0;
+            if (watchdogTimer) {
+              watchdogTimer--;  // only do this when battery is ok
+              if (watchdogTimer == 0) watchdogTrigger = 1;
+            }
+          }
+          if (watchdogTrigger) {
+            watchdogTrigger = 0;  // reboot
             stateCounter = eeSave.warningTimeout;
             state = TIMEOUT_STATE;
           }
-        } else
-        {
-          stateCounter = eeSave.goodTimeout - 1;
-          if ((instate == 0) && (vol >= eeSave.lowV))
+
+          gotoXY(0, 1); 
+          if (watchdogTimer) LcdP("WDT: %d:%02d ", watchdogTimer, 59 - secondsCounter);
+          else
           {
-            gotoXY(0, 3);
-            if (mySecs & 2) {
-              LcdP("On= %04ldd %02ld:%02ld:%02ld", upSecs / 86400, (upSecs / 3600) % 24, (upSecs % 3600) / 60, upSecs % 60);
-              oled.clearToEOL();
+            temperatureReading = (int)DHT.temperature;
+            humidityReading = (int)DHT.humidity;
+            if (chk==DHTLIB_OK)
+            {
+              if (mySecs & 2) LcdP("Tem=%dc ", temperatureReading); else LcdP("Hum=%d%% ", humidityReading);
+              if (temperatureReading>eeSave.twarn) tone(TONE, eeSave.tone, 100);
             }
-            else          {
-              LcdP("Off=%04ldd %02ld:%02ld:%02ld", downSecs / 86400, (downSecs / 3600) % 24 , (downSecs % 3600) / 60, downSecs % 60);
-              oled.clearToEOL();
+          }
+          LcdP("Bat="); showBattery();
+          gotoXY(0, 2); LcdP("HiV=%d.%dv LoV=%d.%dv", int(eeSave.highV), ((int(eeSave.highV * 100)) % 100) / 10, int(eeSave.lowV), ((int(eeSave.lowV * 100)) % 100) / 10); oled.clearToEOL();
+          digitalWrite(WARNING, WARNINGOFF); // no warning
+          digitalWrite(POWER, POWERON);
+          if (vol < eeSave.lowV)
+          {
+            gotoXY(0, 3); LcdP("Wait=%03d secs", stateCounter); oled.clearToEOL();
+            if (stateCounter == 0) {
+              stateCounter = eeSave.warningTimeout;
+              state = TIMEOUT_STATE;
+            }
+          } else
+          {
+            stateCounter = eeSave.goodTimeout - 1;
+            if ((instate == 0) && (vol >= eeSave.lowV))
+            {
+              gotoXY(0, 3);
+              if (mySecs & 2) {
+                LcdP("On= %04ldd %02ld:%02ld:%02ld", upSecs / 86400, (upSecs / 3600) % 24, (upSecs % 3600) / 60, upSecs % 60);
+                oled.clearToEOL();
+              }
+              else          {
+                LcdP("Off=%04ldd %02ld:%02ld:%02ld", downSecs / 86400, (downSecs / 3600) % 24 , (downSecs % 3600) / 60, downSecs % 60);
+                oled.clearToEOL();
+              }
             }
           }
-        }
-        ++upSecs;
-        break;
+          ++upSecs;
+          break;
 
-      case TIMEOUT_STATE : // battery has dropped below upper threshold - ALWAYS timeout to OFF_STATE
-        secondsCounter = 0;
-        if ((stateCounter == eeSave.warningTimeout) || (refresh)) { refresh=0; oled.clear(); }
-        gotoXY(0, 0); LcdP("Timoutout. Bat="); showBattery();
-        gotoXY(0, 1); LcdP("Wait=%03d secs", stateCounter); oled.clearToEOL();
-        digitalWrite(WARNING, WARNINGON); // warning
-        digitalWrite(POWER, POWERON);
-        if (eeSave.tone) tone(TONE, eeSave.tone, 100);
-        if (stateCounter) --stateCounter;
-        if (stateCounter == 0 ) {
-          stateCounter = eeSave.offTimeout;
-          state = OFF_STATE;
-        }
-        ++upSecs;
-        break;
-
-      case OFF_STATE : // everything off
-        secondsCounter = 0;
-        if ((stateCounter == eeSave.offTimeout) || (refresh)) { refresh=0; oled.clear(); }
-        if (stateCounter) --stateCounter;
-        if (vol <= eeSave.highV)  {
-          gotoXY(0, 0);
-          LcdP("Stdby. Bat=");
-          showBattery();
-        }
-        digitalWrite(WARNING, WARNINGON); // LEAVE ON ie LOW
-        digitalWrite(POWER, POWEROFF);
-        if (vol > eeSave.highV)
-        {
-          gotoXY(0, 0); LcdP("Warming up. Bat=");  showBattery();
+        case TIMEOUT_STATE : // battery has dropped below upper threshold - ALWAYS timeout to OFF_STATE
+          secondsCounter = 0;
+          if ((stateCounter == eeSave.warningTimeout) || (refresh)) {
+            refresh = 0;
+            oled.clear();
+          }
+          gotoXY(0, 0); LcdP("Timoutout. Bat="); showBattery();
           gotoXY(0, 1); LcdP("Wait=%03d secs", stateCounter); oled.clearToEOL();
-          if (stateCounter == 0) {
-            stateCounter = eeSave.goodTimeout;
-            state = GOOD_STATE;
+          digitalWrite(WARNING, WARNINGON); // warning
+          digitalWrite(POWER, POWERON);
+          if (eeSave.tone) tone(TONE, eeSave.tone, 100);
+          if (stateCounter) --stateCounter;
+          if (stateCounter == 0 ) {
+            stateCounter = eeSave.offTimeout;
+            state = OFF_STATE;
           }
-        } else
-        {
-          stateCounter = eeSave.offTimeout - 1; // avoid clearscreen
-        //  gotoXY(0, 3); oled.clearToEOL();
-        }
-        ++downSecs;
-        break;
+          ++upSecs;
+          break;
 
-      case SHUTDOWN_STATE: // everything off and stays off... hold the SET button.... set state to SHUTDOWN_STATE and stateCounter to SHUTDOWN_PERIOD
-        secondsCounter = 0;
-        if ((stateCounter == SHUTDOWN_PERIOD) || (refresh))
-        {
-          refresh=0;
-          oled.clear();
-          gotoXY(0, 2); LcdP("Shutdown commencing.."); oled.clearToEOL();
-          digitalWrite(WARNING, WARNINGON); // warning
-        }
-        if (stateCounter)
-        { --stateCounter;
-          if (stateCounter == 0)
+        case OFF_STATE : // everything off
+          secondsCounter = 0;
+          if ((stateCounter == eeSave.offTimeout) || (refresh)) {
+            refresh = 0;
+            oled.clear();
+          }
+          if (stateCounter) --stateCounter;
+          if (vol <= eeSave.highV)  {
+            gotoXY(0, 0);
+            LcdP("Stdby. Bat=");
+            showBattery();
+          }
+          digitalWrite(WARNING, WARNINGON); // LEAVE ON ie LOW
+          digitalWrite(POWER, POWEROFF);
+          if (vol > eeSave.highV)
           {
-            gotoXY(0, 2); LcdP("Shutdown COMPLETE"); oled.clearToEOL();
-            digitalWrite(WARNING, WARNINGON); // LEAVE ON ie LOW
-            digitalWrite(POWER, POWEROFF);
+            gotoXY(0, 0); LcdP("Warming up. Bat=");  showBattery();
+            gotoXY(0, 1); LcdP("Wait=%03d secs", stateCounter); oled.clearToEOL();
+            if (stateCounter == 0) {
+              stateCounter = eeSave.goodTimeout;
+              state = GOOD_STATE;
+            }
+          } else
+          {
+            stateCounter = eeSave.offTimeout - 1; // avoid clearscreen
+            //  gotoXY(0, 3); oled.clearToEOL();
           }
-        }
-        else if (digitalRead(DOWN) == 0)   {
-          oled.clear();
-          state = POWERUP_STATE;
-          stateCounter = eeSave.startTimeout;
-        }
+          ++downSecs;
+          break;
+
+        case SHUTDOWN_STATE: // everything off and stays off... hold the SET button.... set state to SHUTDOWN_STATE and stateCounter to SHUTDOWN_PERIOD
+          secondsCounter = 0;
+          if ((stateCounter == SHUTDOWN_PERIOD) || (refresh))
+          {
+            refresh = 0;
+            oled.clear();
+            gotoXY(0, 2); LcdP("Shutdown commencing.."); oled.clearToEOL();
+            digitalWrite(WARNING, WARNINGON); // warning
+          }
+          if (stateCounter)
+          { --stateCounter;
+            if (stateCounter == 0)
+            {
+              gotoXY(0, 2); LcdP("Shutdown COMPLETE"); oled.clearToEOL();
+              digitalWrite(WARNING, WARNINGON); // LEAVE ON ie LOW
+              digitalWrite(POWER, POWEROFF);
+            }
+          }
+          else if (digitalRead(DOWN) == 0)   {
+            oled.clear();
+            state = POWERUP_STATE;
+            stateCounter = eeSave.startTimeout;
+          }
+      }
     }
-   }
   }
 }
Updated by Peter Scargill

File uninterruptible.c Modified

  • Ignore whitespace
  • Hide word diff
     eeSave.tone = 0;
     eeSave.smon = 0;
     eeSave.watchdog = 0;
-    eeSave.pause=1;
+    eeSave.pause=0;
     EEPROM.put(0, eeSave);
   }
 
Updated by Peter Scargill

File uninterruptible.c Modified

  • Ignore whitespace
  • Hide word diff
 // Blog has more - as does this video https://www.youtube.com/watch?v=44Lvdf7o4GQ
 // Now set up for the 32-pixel displays - got everything down to 4 lines
 
-#define VER "v1.12"
+#define VER "v1.13"
 struct
 {
   uint16_t check;
 {
   EEPROM.get(0, eeSave);
   analogReference(INTERNAL);
-  if (eeSave.check != 0x52d1)
+  if (eeSave.check != 0x52d3)
   {
-    eeSave.check = 0x52d1;
+    eeSave.check = 0x52d3;
 
     eeSave.highV = SWITCHON;
     eeSave.lowV = SWITCHOFF;
             case 9 : eeSave.smon = 0; break;
             case 10 : if (eeSave.watchdog) eeSave.watchdog--;
               watchdogTimer = eeSave.watchdog; watchdogTrigger = 0; break;
-            case 11 : if (eeSave.pause>1) eeSave.pause--; break;
+            case 11 : if (eeSave.pause) eeSave.pause--; break;
           }
           updated = 0;
           updateTimeout = 100;
     mymillis += 1000; // every second we do this
     average = ((average * 7) + analogRead(VOLTAGE)) / 8; //i.e. average over 8 seconds 0-1023 = 0-5v - might make testing longer in the end
     vol = (((float)average) * 5.0 / 824.0) * (float)eeSave.batteryOffset / 400.0; //10k/2k2
-    if ((mySecs%60)==0) logPause++;
+    if (eeSave.pause) { if ((mySecs%60)==0) logPause++; } else logPause++; 
     mySecs++;
     if (logPause>eeSave.pause) // log 128 values 0-30 = 0-5v  -  2 secs updating for testing
     {
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5