Created
March 2, 2021 22:16
-
-
Save sk-t3ch/2de3d41e1a74bbd039292006c1cf4884 to your computer and use it in GitHub Desktop.
Smart Buoy - Control power with RTC module based on voltage from INA219 Power monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DS3232RTC.h> | |
#include <Adafruit_INA219.h> | |
#include <Wire.h> | |
int led = LED_BUILTIN; | |
// INA219 - Power Monitor | |
Adafruit_INA219 ina219; | |
void setup() { | |
Wire.begin(); | |
pinMode(led, OUTPUT); | |
// initialise INA219 - Power Monitor | |
ina219.begin(); | |
ina219.setCalibration_32V_1A(); | |
} | |
void loop() { | |
digitalWrite(led, HIGH); | |
delay(3000); | |
reset_alarm(); | |
} | |
void reset_alarm(){ | |
RTC.alarmInterrupt(ALARM_1, true); | |
RTC.squareWave(SQWAVE_NONE); | |
setTime(0, 0, 0, 0, 0, 1970); | |
RTC.set(now()); | |
int wait_time = get_wait_time_from_voltage(); | |
// set new alarm | |
RTC.setAlarm(ALM1_MATCH_MINUTES, 0, wait_time, 0, 0); | |
// clear old alarm flag - turning off system | |
RTC.alarm(ALARM_1); | |
} | |
int get_wait_time_from_voltage(){ | |
float shuntvoltage = ina219.getShuntVoltage_mV(); | |
float busvoltage = ina219.getBusVoltage_V(); | |
float loadvoltage = busvoltage + (shuntvoltage / 1000); | |
// Samsung 18650 % capacity at a given voltage | |
// batt_voltages 0.0, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.5 | |
// batt_percentages 0, 0, 9, 22, 52, 64, 75, 84, 93, 100, 100 | |
int wait_time; | |
if (loadvoltage < 3.6) wait_time = 55; | |
else if (loadvoltage < 3.8) wait_time = 25; | |
else if (loadvoltage < 4.0) wait_time = 15; | |
else if (loadvoltage < 4.1) wait_time = 10; | |
else wait_time = 3; | |
return wait_time; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment