#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
// 0X3C+SA0 - 0x3C or 0x3D
void LcdP(char *fmt, ...)
+uint32_t my100millis = 0;
uint8_t state = 0; // 0 is power up, 1 is on, 2 is turning off
uint16_t lightOffSet = 1022;
-float offVoltage = SWITCHOFF;
-float onVoltage = SWITCHON;
+uint8_t updateTimeout = 0;
char *theState = "standby";
+uint8_t sKeypressed = 0;
+uint8_t uKeypressed = 0;
+uint8_t dKeypressed = 0;
// read the input on analog pin 0
void gotoXY(int x, int y)
//------------------------------------------------------------------------------
+ if (myVars.check != 0x52d2)
+ myVars.highV = SWITCHON;
+ myVars.lowV = SWITCHOFF;
oled.begin(&Adafruit128x64, I2C_ADDRESS);
- oled.setFont(Adafruit5x7);
+ oled.setFont(Adafruit5x7);
pinMode(WARNING, OUTPUT);
+ pinMode(SET, INPUT_PULLUP);
+ pinMode(UP, INPUT_PULLUP);
+ pinMode(DOWN, INPUT_PULLUP);
//oled.println("Hello world!");
- // oled.println("A long line");
+ // oled.println("A long line");
- // oled.println("2X demo");
+ // oled.println("2X demo");
//oled.print("\nmicros: ");
- // oled.print(micros() - m);
+ // oled.print(micros() - m);
+ my100millis = millis() + 1000;
//------------------------------------------------------------------------------
// put your main code here, to run repeatedly:
+ if (my100millis <= millis()) // 100ms for key check
+ if (digitalRead(SET) == 0)
+ if (digitalRead(UP) == 0)
+ if (digitalRead(DOWN) == 0)
+ LcdP("SET: High V=%d.%d ", int(myVars.highV), ((int(myVars.highV * 100)) % 100) / 10);
+ LcdP("SET: Low V=%d.%d ", int(myVars.lowV), ((int(myVars.lowV * 100)) % 100) / 10);
+ if (--updateTimeout == 0)
if (mymillis <= millis())
- average=average*16.0/17.0;
+ average += (vol / 16.0);
+ average = average * 16.0 / 17.0;
+ if (vol > myVars.highV)
- state = (WAITING*2) + 2;
+ state = (WAITING * 2) + 2;
- digitalWrite(RELAY, LOW); downSecs=0;
+ digitalWrite(RELAY, LOW);
//analogWrite(PIN_PWM,255); // only 3v3 intended so this will likely do
digitalWrite(WARNING, LOW);
+ if (vol > myVars.highV)
//analogWrite(PIN_PWM,128); // only 3v3 intended so this will likely do
- theState = "active "; upSecs=0;
- onDelay=0; theState = "standby "; state=0;
p(",\"status\":\"%s\"}\r\n", theState); // sadly printf here does not support floats
- gotoXY(0,0); LcdP("POWER SUPPLY CONTROL");
- gotoXY(0,2); LcdP("Current volts: %d.%d",int(vol),((int(vol*100))%100)/10);
- gotoXY(0,3); LcdP("State: %s",theState);
- gotoXY(0,4); LcdP("High: %d.%d Low: %d.%d ",int(onVoltage),((int(onVoltage*100))%100)/10,int(offVoltage),((int(offVoltage*100))%100)/10);
- gotoXY(0,5); LcdP("On: %05ld:%02ld:%02ld",upSecs/3600,(upSecs%3600)/60, upSecs%60);
- gotoXY(0,6); LcdP("Off: %05ld:%02ld:%02ld",downSecs/3600,(downSecs%3600)/60, downSecs%60);
- gotoXY(0,7); LcdP("Total: %05ld:%02ld:%02ld",secs/3600,(secs%3600)/60, secs%60);
+ LcdP("POWER SUPPLY CONTROL");
+ LcdP("Current volts: %d.%d", int(vol), ((int(vol * 100)) % 100) / 10);
+ LcdP("State: %s", theState);
+ LcdP("High: %d.%d Low: %d.%d ", int(myVars.highV), ((int(myVars.highV * 100)) % 100) / 10, int(myVars.lowV), ((int(myVars.lowV * 100)) % 100) / 10);
+ LcdP("On: %05ld:%02ld:%02ld", upSecs / 3600, (upSecs % 3600) / 60, upSecs % 60);
+ LcdP("Off: %05ld:%02ld:%02ld", downSecs / 3600, (downSecs % 3600) / 60, downSecs % 60);
+ LcdP("Total: %05ld:%02ld:%02ld", secs / 3600, (secs % 3600) / 60, secs % 60);
digitalWrite(LIGHT, HIGH);
mymillis = millis() + lightOnSet;
digitalWrite(LIGHT, LOW);
mymillis = millis() + lightOffSet;
- if (state==1) upSecs++; else downSecs++;