Snippets

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

Created by Peter Scargill last modified
  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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <EEPROM.h>
#include <dht.h>

// UPS (un-interruptible supply) software
// Peter Scargill 2017  - https://tech.scargill.net
//
// This Atmega 328 low cost solution powers up with an SSD1306 display, showing the
// battery voltage (needs dividers - I used 2k2 ground to input, 10k input to battery)
// with programmable offset to compensate for resistor variation.
// Averages voltage over 8 seconds to avoid spikes and save hardware filtering
//
// Starts up - if voltage above upper threshold for a while turns
// output bit on - that controls a P-channel MOSFET on output
// of voltage booster - power the board from booster BEFORE the MOSFET
// There is a warning bit- to warn micro of impending
// shutdown. Shutdown when LOW.
//
// Shuts down power after giving load time to respond and turn off
// also shuts down after this point EVEN if the power came back on after warning bit
// went low. While off uses minimal display area to save current (OLED)
// Then turns back on when battery is up to it. Big programmable hysteresis between
// shutdown voltage and wakeup voltage
//
// Additional functionality:
// - long-press UP to trigger orderly shutdown. In that state press DOWN to restore
// - long-press DOWN to trigger test voltage reset. Sets average voltage to ZERO
//     temporarily - this triggers warning and shutdown - and eventual reboot
// - Added optional, programmable warning beeper (Piezo)
// - Added serial JSON (optional) monitor
// - Watchdog timeout from 1 minute through 255 minutes - change of state pin WATCHDOG
//  In normal operation shows total on time and total off time
// = new logging graphics - long-press SET button to see 0-5v battery over time
//
// Use 328-based board similar regulator (pref low loss) and no USB chip. If possible
// run OLED off 3v3.
//
// 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


dht DHT;   // temperature sensing
SSD1306AsciiWire oled;

#define VER "v1.15"
#define I2C_ADDRESS 0x3C
#define OPTIMIZE_I2C 1

struct
{
  uint16_t check;
  float highV;
  float lowV;
  uint8_t startTimeout;
  uint8_t goodTimeout;
  uint8_t warningTimeout;
  uint8_t offTimeout;
  int16_t batteryOffset;
  uint16_t tone;
  uint8_t smon;
  uint8_t watchdog;
  uint16_t pause;
  uint8_t twarn;

} eeSave;


// 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 TIMEOUT_STATE 2
#define OFF_STATE 3
#define SHUTDOWN_STATE 4

#define WAITING 60      // wait before actually turning off "relay" after
#define ONDELAY 20      // delay before turning on

#define SWITCHON 3.7    // defaults for hysteresis voltage
#define SWITCHOFF 3.1

#define LONGPRESS_UP 20   // 10ths of second before long presson UP triggers shutdown
#define LONGPRESS_DOWN 20 // 10ths of second before battery fail test
#define LONGPRESS_SET 20 // 10ths of second before graphics display
#define SHOWGRAPH 10     // 10 seconds showing graphical info
#define SHUTDOWN_PERIOD 20

uint8_t showGraph = 0;

uint32_t secs = 0;
uint32_t upSecs = 0;
uint32_t downSecs = 0;

uint8_t triggerShutdown = 0;

uint32_t mySecs = 0;
uint8_t watchdogTrigger = 0;
uint8_t watchdogTimer = 0;
uint8_t watchdogState = 0;
uint8_t secondsCounter = 0;
uint8_t refresh = 0;

void LcdP(char *fmt, ...)
{
  char buf[22]; // resulting string limited to 128 chars
  va_list args;
  va_start(args, fmt);
  vsnprintf(buf, 128, fmt, args);
  va_end(args);
  oled.print(buf);
}

uint32_t mymillis = 0;
uint32_t my100millis = 0;
uint8_t lightState = 0;
uint8_t state = 0; // 0 is power up, 1 is on, 2 is turning off
uint8_t stateCounter = 0; // might want to stay in a state for some time
uint8_t startCounter = 0; // startup fallover into OFF state
float vol;
uint8_t onDelay = 0;

uint16_t average;
int temperatureReading;
int humidityReading;
uint8_t updateTimeout = 0;

char *theState = "standby";

uint8_t instate = 0;
uint8_t updated = 0;
uint8_t sKeypressed = 0;
uint8_t uKeypressed = 0;
uint8_t dKeypressed = 0;

uint8_t logit[128];
uint16_t logPause = 0;

void gotoXY(int x, int y)
{
  oled.setCursor(x, y);
}

//------------------------------------------------------------------------------
void setup()
{
  EEPROM.get(0, eeSave);
  analogReference(INTERNAL);
  if (eeSave.check != 0x52d1)
  {
    eeSave.check = 0x52d1;

    eeSave.highV = SWITCHON;
    eeSave.lowV = SWITCHOFF;
    eeSave.startTimeout = 5;
    eeSave.goodTimeout = 5;
    eeSave.warningTimeout = 25;
    eeSave.offTimeout = 20;
    eeSave.batteryOffset = 400;
    eeSave.tone = 0;
    eeSave.smon = 0;
    eeSave.watchdog = 0;
    eeSave.pause = 0;
    eeSave.twarn = 40;
    EEPROM.put(0, eeSave);
  }

  Wire.begin();
  oled.begin(&Adafruit128x32, I2C_ADDRESS);
  oled.set400kHz();
  oled.setFont(Adafruit5x7);
  uint32_t m = micros();

  Serial.begin(115200); // serial JSON data out if set

#define POWERON LOW
#define POWEROFF HIGH
#define WARNINGON LOW
#define WARNINGOFF HIGH

  digitalWrite(POWER, POWEROFF);   pinMode(POWER, OUTPUT);
  digitalWrite(WARNING, WARNINGOFF); pinMode(WARNING, OUTPUT);
  digitalWrite(TONE, 0); pinMode(TONE, OUTPUT);
  pinMode(SET, INPUT_PULLUP);
  pinMode(UP, INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  pinMode(WATCHDOG, INPUT_PULLUP);
  average = analogRead(VBAT); // we start off with read value;
  oled.clear();
  my100millis = millis() + 1000;
  state = 0;
  stateCounter = eeSave.startTimeout;
  watchdogState = digitalRead(WATCHDOG);
}

/*
  // Float to ASCII conversion
  char *ftoa(char *a, double f, int precision)
  {
	long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};

	char *ret = a;
	long heiltal = (long)f;
	itoa(heiltal, a, 10);
	while (*a != '\0') a++;
	a++ = '.';
	long desimal = abs((long)((f - heiltal) * p[precision]));
	itoa(desimal, a, 10);
	return ret;

  }
*/


void showBattery(void)
{
  LcdP("%d.%02dv", int(vol), ((int(vol * 100)) % 100)); oled.clearToEOL();
}
//------------------------------------------------------------------------------
void loop()
{
  // main loop

  if (my100millis <= millis()) // 100ms for key check
  {
    my100millis += 100;
    if ((eeSave.watchdog) && (watchdogTrigger == 0))
    {
      if (watchdogState != digitalRead(WATCHDOG)) {
        watchdogTimer = eeSave.watchdog;
        watchdogTrigger = 0;
        watchdogState = digitalRead(WATCHDOG);
      }
    }
    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;
        }
        sKeypressed = 0;
      }
    }
    else
    {
      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)
        {
          switch (instate)
          {
            case 1 : if (eeSave.highV < 4.3) eeSave.highV += 0.1; break;
            case 2 : if (eeSave.lowV < 4.2) eeSave.lowV += 0.1; break;
            case 3 : eeSave.startTimeout++; break;
            case 4 : eeSave.goodTimeout++; break;
            case 5 : eeSave.warningTimeout++; break;
            case 6 : eeSave.offTimeout++; break;
            case 7 : eeSave.batteryOffset++; break;
            case 8 : if (eeSave.tone < 5000) eeSave.tone += 500; else eeSave.tone = 0; break;
            case 9 : eeSave.smon = 1; break;
            case 10 : eeSave.watchdog++;
              watchdogTimer = eeSave.watchdog; watchdogTrigger = 0; 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;
      }
    }
    else
    {
      uKeypressed++;
      if (uKeypressed == LONGPRESS_UP) triggerShutdown = 1;
      if (uKeypressed > LONGPRESS_UP) uKeypressed--;
    }

    if (digitalRead(DOWN) == 1)
    {
      if (dKeypressed)
      {
        if (dKeypressed < LONGPRESS_DOWN)
        {
          switch (instate)
          {
            case 1 : if (eeSave.highV > 2.3) eeSave.highV -= 0.1; break;
            case 2 : if (eeSave.lowV > 2.2) eeSave.lowV -= 0.1; break;
            case 3 : eeSave.startTimeout--; break;
            case 4 : eeSave.goodTimeout--; break;
            case 5 : eeSave.warningTimeout--; break;
            case 6 : eeSave.offTimeout--; break;
            case 7 : eeSave.batteryOffset--; break;
            case 8 : if (eeSave.tone >= 500) eeSave.tone -= 500; break;
            case 9 : eeSave.smon = 0; break;
            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;
      }
    }
    else
    {
      dKeypressed++;
      if (dKeypressed == LONGPRESS_DOWN) average = 0;
      if (dKeypressed > LONGPRESS_DOWN) dKeypressed--;
    }

    if (eeSave.highV <= eeSave.lowV) eeSave.highV = eeSave.lowV + 0.1;
    if (eeSave.lowV >= eeSave.highV) eeSave.lowV = eeSave.highV - 0.1;
    switch (instate)
    {
      case 0:  if (updated == 0)
        {
          gotoXY(0, 3);
          oled.clearToEOL();
          updated = 1;
        }
        break;
      case 1:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("SET: HiV=%d.%d   ", int(eeSave.highV), ((int(eeSave.highV * 100)) % 100) / 10); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 2:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("SET: LoV=%d.%d   ", int(eeSave.lowV), ((int(eeSave.lowV * 100)) % 100) / 10); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 3:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("SET: Start=%d secs", eeSave.startTimeout); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 4:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("SET: Good=%d secs", eeSave.goodTimeout); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 5:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("SET: Warn=%d secs", eeSave.warningTimeout); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 6:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("SET: Off=%d secs", eeSave.offTimeout); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 7:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("SET: Boffs=%d", eeSave.batteryOffset - 400); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 8: if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("Warning tone=%dHz", eeSave.tone); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 9: if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("Serial mon On/Off: %d", eeSave.smon); oled.clearToEOL();
          updated = 1;
        }

        break;
      case 10: if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("Watchdog=%d mins", eeSave.watchdog); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 11:  if (updated == 0)
        {
          gotoXY(0, 3);
          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("Temp warning=%dc", eeSave.twarn); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 13:  if (updated == 0)
        {
          gotoXY(0, 3);
          LcdP("Version=%s", VER); oled.clearToEOL();
          updated = 1;
        }
        break;
      case 14:  updated = 0; instate = 0; break;
    }

    if (updateTimeout)
    {
      if (--updateTimeout == 0)
      {
        instate = 0;
        updated = 0;
        EEPROM.put(0, eeSave);
      }
    }
  }

  // main 1 second loop here for determining where we are
  if (mymillis <= millis())
  {
    mymillis += 1000; // every second we do this

    int chk = DHT.read22(DHT22_PIN);

    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) {
      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
    {
      logPause = 0;
      for (int a = 0; a < 127; a++) logit[a] = logit[a + 1];
      logit[126] = ((int)(vol * 6.0));
    }

    if (triggerShutdown) {
      triggerShutdown = 0;
      state = SHUTDOWN_STATE;
      stateCounter = SHUTDOWN_PERIOD;
    }

    char bString[64];
    sprintf(bString, "{\"state\": %d,\"battery\": %d.%02d,\"upsecs\":%ld,\"downsecs\":%ld}", state, int(vol), (int)(vol * 100) % 100, upSecs, downSecs);

    if (eeSave.smon) Serial.println(bString);

    if (showGraph)
    {
      if (showGraph == SHOWGRAPH)
      {
        uint8_t val;
        oled.clear();
        oled.setCursor(0, 0);
        for (int a = 0; a < 128; a++)
        {
          //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));
        }
        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;
            }
          } 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;
            }
          }
          if (watchdogTrigger) {
            watchdogTrigger = 0;  // reboot
            stateCounter = eeSave.warningTimeout;
            state = TIMEOUT_STATE;
          }

          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 (mySecs & 2) LcdP("Tem=%dc ", temperatureReading); else LcdP("Hum=%d%% ", humidityReading);
              if (temperatureReading > eeSave.twarn) tone(TONE, eeSave.tone, 100);
            }
          }
          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;

        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();
            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();
          }
          ++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;
          }
      }
    }
  }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.