Snippets
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 | #include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <EEPROM.h>
struct
{
float highV;
float lowV;
uint16_t check;
} myVars;
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
SSD1306AsciiWire oled;
#define LIGHT 12
#define RELAY 11
#define WARNING 10
#define WAITING 60
#define ONDELAY 20
#define SWITCHON 3.7
#define SWITCHOFF 3.1
#define SET 5
#define UP 6
#define DOWN 7
uint32_t secs = 0;
uint32_t upSecs = 0;
uint32_t downSecs = 0;
void LcdP(char *fmt, ...)
{
char buf[128]; // 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
float vol;
uint8_t onDelay = 0;
uint16_t lightOnSet = 2;
uint16_t lightOffSet = 1022;
float average = 0;
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;
float voltage()
{
// read the input on analog pin 0
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float vols = sensorValue * (5.0 / 1023.0);
// print out the value you read:
return (vols);
}
void p(char *fmt, ...)
{
char buf[128]; // resulting string limited to 128 chars
va_list args;
va_start(args, fmt);
vsnprintf(buf, 128, fmt, args);
va_end(args);
Serial.print(buf);
}
void gotoXY(int x, int y)
{
oled.setCursor(x, y);
}
//------------------------------------------------------------------------------
void setup()
{
EEPROM.get(0, myVars);
if (myVars.check != 0x52d2)
{
myVars.check = 0x52d2;
myVars.highV = SWITCHON;
myVars.lowV = SWITCHOFF;
EEPROM.put(0, myVars);
}
Wire.begin();
oled.begin(&Adafruit128x64, I2C_ADDRESS);
oled.set400kHz();
oled.setFont(Adafruit5x7);
uint32_t m = micros();
Serial.begin(57600);
digitalWrite(LIGHT, LOW);
digitalWrite(RELAY, LOW);
digitalWrite(WARNING, LOW);
pinMode(LIGHT, OUTPUT);
pinMode(RELAY, OUTPUT);
pinMode(WARNING, OUTPUT);
pinMode(SET, INPUT_PULLUP);
pinMode(UP, INPUT_PULLUP);
pinMode(DOWN, INPUT_PULLUP);
average = voltage();
oled.clear();
LcdP("Init....\r\n");
//oled.println("Hello world!");
// oled.println("A long line");
//oled.println();
//oled.set2X();
// oled.println("2X demo");
// oled.set1X();
//oled.print("\nmicros: ");
// oled.print(micros() - m);
my100millis = millis() + 1000;
}
//------------------------------------------------------------------------------
void loop()
{
// put your main code here, to run repeatedly:
if (my100millis <= millis()) // 100ms for key check
{
my100millis += 100;
if (digitalRead(SET) == 0)
{
if (sKeypressed == 0)
{
sKeypressed = 1;
instate++;
updated = 0;
updateTimeout = 100;
}
}
else
sKeypressed = 0;
if (digitalRead(UP) == 0)
{
if (uKeypressed == 0)
{
uKeypressed = 1;
if (instate == 1)
myVars.highV += 0.1;
else if (instate == 2)
myVars.lowV += 0.1;
updated = 0;
updateTimeout = 100;
}
}
else
uKeypressed = 0;
if (digitalRead(DOWN) == 0)
{
if (dKeypressed == 0)
{
dKeypressed = 1;
if (instate == 1)
myVars.highV -= 0.1;
else if (instate == 2)
myVars.lowV -= 0.1;
updated = 0;
updateTimeout = 100;
}
}
else
dKeypressed = 0;
switch (instate)
{
case 0: if (updated == 0)
{
gotoXY(0, 1);
oled.clearToEOL();
updated = 1;
}
break;
case 1: if (updated == 0)
{
gotoXY(0, 1);
LcdP("SET: High V=%d.%d ", int(myVars.highV), ((int(myVars.highV * 100)) % 100) / 10); oled.clearToEOL();
updated = 1;
}
break;
case 2: if (updated == 0)
{
gotoXY(0, 1);
LcdP("SET: Low V=%d.%d ", int(myVars.lowV), ((int(myVars.lowV * 100)) % 100) / 10); oled.clearToEOL();
updated = 1;
}
break;
case 3: if (updated == 0)
{
gotoXY(0, 1);
LcdP("Version 1.01"); oled.clearToEOL();
updated = 1;
}
break;
case 4: updated=0; instate=0; break;
}
if (updateTimeout)
{
if (--updateTimeout == 0)
{
instate = 0;
updated = 0;
EEPROM.put(0, myVars);
}
}
}
if (mymillis <= millis())
{
vol = voltage();
average += (vol / 16.0);
average = average * 16.0 / 17.0;
vol = average;
if (state == 0)
{
if (vol > myVars.highV)
{
if (onDelay == 0)
{
onDelay = ONDELAY * 2;
theState = "starting";
}
}
}
else if (state == 1)
{
if (vol < myVars.lowV)
{
state = (WAITING * 2) + 2;
theState = "warning ";
}
}
else if (state ==2)
{
digitalWrite(WARNING, LOW);
}
else if (state > 2)
{
digitalWrite(WARNING, HIGH);
state--;
if (state == 2)
{
state = 0;
digitalWrite(RELAY, LOW);
digitalWrite(WARNING, LOW);
downSecs = 0;
//analogWrite(PIN_PWM,255); // only 3v3 intended so this will likely do
theState = "standby ";
}
}
else
digitalWrite(WARNING, LOW);
if (vol > myVars.highV)
{
if (onDelay)
{
if (--onDelay == 0)
{
state = 1;
//analogWrite(PIN_PWM,128); // only 3v3 intended so this will likely do
theState = "active ";
upSecs = 0;
}
}
}
else
{
if (onDelay)
{
onDelay = 0;
theState = "standby ";
state = 0;
}
}
if (lightState == 0)
{
p("{\"voltage\":");
Serial.print(vol);
p(",\"status\":\"%s\"}\r\n", theState); // sadly printf here does not support floats
oled.clearToEOL();
gotoXY(0, 0);
LcdP("POWER SUPPLY CONTROL"); oled.clearToEOL();
gotoXY(0, 2);
LcdP("Current volts: %d.%dv", int(vol), ((int(vol * 100)) % 100) / 10); oled.clearToEOL();
gotoXY(0, 3);
LcdP("State: %s", theState); oled.clearToEOL();
gotoXY(0, 4);
LcdP("High: %d.%dv Low: %d.%dv", int(myVars.highV), ((int(myVars.highV * 100)) % 100) / 10, int(myVars.lowV), ((int(myVars.lowV * 100)) % 100) / 10); oled.clearToEOL();
gotoXY(0, 5);
LcdP("On: %04ldd %02ld:%02ld:%02ld", secs/86400,(upSecs / 3600) % 24, (upSecs % 3600) / 60, upSecs % 60); oled.clearToEOL();
gotoXY(0, 6);
LcdP("Off: %04ldd %02ld:%02ld:%02ld", secs/86400,(downSecs / 3600) %24 , (downSecs % 3600) / 60, downSecs % 60); oled.clearToEOL();
gotoXY(0, 7);
LcdP("Tot: %04ldd %02ld:%02ld:%02ld", secs/86400,(secs / 3600) % 24, (secs % 3600) / 60, secs % 60); oled.clearToEOL();
lightState = 1;
digitalWrite(LIGHT, HIGH);
switch (state)
{
case 0:
if (onDelay)
{
lightOnSet = 80;
lightOffSet = 944;
}
else
{
lightOnSet = 1;
lightOffSet = 1023;
}
digitalWrite(RELAY, LOW);
break; // battery is low
case 1:
lightOnSet = 980;
lightOffSet = 44;
digitalWrite(RELAY, HIGH);
break; // battery is ok
default:
lightOnSet = 512;
lightOffSet = 512;
break; // intermediate state;
}
mymillis = millis() + lightOnSet;
}
else
{
lightState = 0;
digitalWrite(LIGHT, LOW);
mymillis = millis() + lightOffSet;
secs++;
if (state == 1)
upSecs++;
else
downSecs++;
}
}
}
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
I appreciate the detailed code structure, which makes it easier to understand how the different components interact. This type of project not only demonstrates real-world applications, but also showcases how microcontrollers can be effectively used in DIY electronics.