Fix Button3 Longpress, MinMax Display

This commit is contained in:
Carsten Schmiemann 2018-09-28 02:47:37 +02:00
parent e12fe815b7
commit e01250db1d
5 changed files with 65 additions and 47 deletions

Binary file not shown.

View file

@ -17,7 +17,7 @@
<div class="row">
<div class="col-md-12">
<h3>
Power Analyzer - V0.29 Beta
Power Analyzer - V0.35 Beta
</h3>
<ul class="nav">
<li class="nav-item">

View file

@ -17,7 +17,7 @@
<div class="row">
<div class="col-md-12">
<h3>
Power Analyzer - V0.29 Beta
Power Analyzer - V0.35 Beta
</h3>
<ul class="nav">
<li class="nav-item">

View file

@ -17,7 +17,7 @@
<div class="row">
<div class="col-md-12">
<h3>
Power Analyzer - V0.29 Beta
Power Analyzer - V0.35 Beta
</h3>
<ul class="nav">
<li class="nav-item">

View file

@ -66,8 +66,8 @@ float Ah = 0, mAh = 0, Wh = 0, mWh = 0;
float battery_voltage, battery_average;
int i_header = 0;
bool button1, button2, button3, button3long;
bool lcd_light = true, lcd_minmax = false, wifi_enabled = true, test_mode = false;
bool measuring_run = false, measuring_init = false, reset_actual = false, reset_minmax = false, battery_low = false;
bool lcd_light = true, lcd_minmax = false, lcd_cleared = true, wifi_enabled = true, test_mode = false;
bool measuring_run = false, measuring_init = false, reset_actual = false, reset_minmax = false, battery_low = false, errorID = false;
unsigned long lastread = 0, tick, previousMillisReadData = 0, previousMillisDisplay = 0, data_timestamp = 0, button3timer = 0;
//Custom LCD Characters
@ -172,7 +172,7 @@ void setup() {
lcd.setCursor(4,0);
lcd.print("Power Analyzer");
lcd.setCursor(6,2);
lcd.print("V0.31 Beta");
lcd.print("V0.35 Beta");
lcd.setCursor(7,3);
lcd.print("CS,2018");
lcd.write(1);
@ -352,37 +352,44 @@ void loop() {
void lcd_print() {
if (lcd_minmax) {
if (lcd_cleared == false) {
lcd.clear();
lcd_cleared = true;
}
lcd.setCursor(2,0);
lcd.print("Min / Max values");
lcd.setCursor(1,1);
lcd.print(String(current_A_min) + String(" / ") + String(current_A_max) + String(" A"));
lcd.setCursor(1,2);
lcd.print(String(power_W_min) + String(" / ") + String(power_W_max) + String(" A"));
lcd.print(String(power_W_min) + String(" / ") + String(power_W_max) + String(" W"));
lcd.setCursor(1,3);
lcd.print(String(busvoltage_V_min) + String(" / ") + String(busvoltage_V_max) + String(" A"));
lcd.print(String(busvoltage_V_min) + String(" / ") + String(busvoltage_V_max) + String(" V"));
} else {
lcd.setCursor(0,1);
if (current_A < 100 && current_A >= 10) {lcd.print(" ");} else if (current_A < 10 && current_A >= 0) {lcd.print(" ");};
if (current_A > -100 && current_A <= -10) {lcd.print(" ");} else if (current_A > -10 && current_A < 0) {lcd.print(" ");};
lcd.print(current_A);
if (lcd_cleared) {lcd_init();}
lcd.setCursor(0,1);
if (current_A < 100 && current_A >= 10) {lcd.print(" ");} else if (current_A < 10 && current_A >= 0) {lcd.print(" ");};
if (current_A > -100 && current_A <= -10) {lcd.print(" ");} else if (current_A > -10 && current_A < 0) {lcd.print(" ");};
lcd.print(current_A);
lcd.setCursor(0,2);
if (power_W < 1000 && power_W > 100) {lcd.print(" ");} else if (power_W < 100 && power_W >= 10) {lcd.print(" ");} else if (power_W < 10) {lcd.print(" ");};
lcd.print(power_W);
lcd.setCursor(0,2);
if (power_W < 1000 && power_W > 100) {lcd.print(" ");} else if (power_W < 100 && power_W >= 10) {lcd.print(" ");} else if (power_W < 10) {lcd.print(" ");};
lcd.print(power_W);
lcd.setCursor(0,3);
if (busvoltage_V < 100 && busvoltage_V >= 10) {lcd.print(" ");} else if (busvoltage_V < 10) {lcd.print(" ");};
lcd.print(busvoltage_V);
lcd.setCursor(0,3);
if (busvoltage_V < 100 && busvoltage_V >= 10) {lcd.print(" ");} else if (busvoltage_V < 10) {lcd.print(" ");};
lcd.print(busvoltage_V);
lcd.setCursor(11,1);
if (Ah < 1000 && Ah > 100) {lcd.print(" ");} else if (Ah < 100 && Ah >= 10) {lcd.print(" ");} else if (Ah < 10) {lcd.print(" ");};
if (Ah > -100 && Ah <= -10) {lcd.print(" ");} else if (Ah > -10 && Ah < 0) {lcd.print(" ");};
lcd.print(Ah);
lcd.setCursor(11,1);
if (Ah < 1000 && Ah > 100) {lcd.print(" ");} else if (Ah < 100 && Ah >= 10) {lcd.print(" ");} else if (Ah < 10) {lcd.print(" ");};
if (Ah > -100 && Ah <= -10) {lcd.print(" ");} else if (Ah > -10 && Ah < 0) {lcd.print(" ");};
lcd.print(Ah);
lcd.setCursor(11,2);
if (Wh < 1000 && Wh > 100) {lcd.print(" ");} else if (Wh < 100 && Wh >= 10) {lcd.print(" ");} else if (Wh < 10) {lcd.print(" ");};
lcd.print(Wh);
lcd.setCursor(11,2);
if (Wh < 1000 && Wh > 100) {lcd.print(" ");} else if (Wh < 100 && Wh >= 10) {lcd.print(" ");} else if (Wh < 10) {lcd.print(" ");};
lcd.print(Wh);
lcd.setCursor(14,3);
if (battery_voltage > 3.4) {lcd.print(battery_voltage);} else {lcd.print("LOW ");}
lcd.setCursor(14,3);
if (battery_voltage > 3.4) {lcd.print(battery_voltage);} else {lcd.print("LOW ");}
}
}
@ -404,11 +411,11 @@ void lcd_init() {
lcd.write((uint8_t)2);
lcd.setCursor(18,3);
lcd.print("V");
delay(300);
lcd_cleared = false;
}
void lcd_header() {
if (measuring_run) {
if (measuring_run && lcd_minmax == false) {
i_header++;
if (i_header == 1) {
lcd.setCursor(0,0);
@ -437,9 +444,9 @@ void lcd_header() {
}
}
//Flash Battery Low LED
if (battery_low && digitalRead(led_error_pin)) {
if ((battery_low || errorID) && digitalRead(led_error_pin)) {
digitalWrite(led_error_pin, LOW);
} else if (battery_low && digitalRead(led_error_pin) == false) { digitalWrite(led_error_pin, HIGH); }
} else if ((battery_low || errorID) && digitalRead(led_error_pin) == false) { digitalWrite(led_error_pin, HIGH); }
}
void readCurrent() {
@ -513,14 +520,18 @@ void readCurrent() {
nvs.end();
measuring_init = false;
}
if (busvoltage_V > 26 || current_A > 62) {
errorID = true;
measuring_run = false;
} else {
errorID = false;
}
lastread = newtime;
ina219.recalibrate();
ina219.reconfig();
}
void read_bat() {
battery_average += (analogRead(adc_battery_pin) - battery_average) * 0.3;
battery_voltage = map(battery_average,0,3480,0,440)/100.0;
battery_voltage = map(battery_average,0,4096,0,440)/100.0;
if (battery_voltage < 3.3 && battery_low == false) {
battery_low = true;
lcd.noBacklight();
@ -548,22 +559,23 @@ void read_button() {
if (digitalRead(button3_pin)) {
button3 = false;
button3long = false;
button3timer = 0;
}
else {
button3 = true;
button3timer = millis();
if (millis() - button3timer > 900) {
if (button3timer == 0) {button3 = true;}
button3timer++;
if (button3timer == 4) {
button3long = true;
}
} else {button3long = false;}
}
if (button1 && test_mode == false) {
reset_actual = true;
}
if (button2 && measuring_run && test_mode == false) {
measuring_run = false;
if (errorID == false) {measuring_run = false;}
} else if (button2 && measuring_run == false) {
measuring_run = true;
if (errorID == false) {measuring_run = true;}
}
if (measuring_run) {
digitalWrite(led_run_pin,true);
@ -585,28 +597,34 @@ void read_button() {
}
void web_measuring_stop(AsyncWebServerRequest *request) {
measuring_run = false;
request->send(200);
if (errorID == false) {measuring_run = false;}
//request->send(200);
request->send(SPIFFS, "/index.html");
}
void web_measuring_run(AsyncWebServerRequest *request) {
measuring_run = true;
request->send(200);
if (errorID == false) {measuring_run = true;}
//request->send(200);
request->send(SPIFFS, "/index.html");
}
void web_measuring_reset(AsyncWebServerRequest *request) {
reset_actual = true;
request->send(200);
//request->send(200);
request->send(SPIFFS, "/index.html");
}
void web_get_values(AsyncWebServerRequest *request) {
String web_status;
if (measuring_run) {
if (measuring_run && errorID == false) {
web_status = "Running";
}
else {
else if (errorID == false) {
web_status = "Stopped";
}
else {
web_status = "Error";
}
request->send( 200, "application/json", "{\"voltage\":" + String(busvoltage_V) + ", \"current\":" + String(current_A) + ", \"power\":" + String(power_W) + ", \"Ah\":" + String(Ah) + ", \"Wh\":" + String(Wh) + ", \"battery\": " + String(battery_voltage) + ", \"run\":\"" + String(web_status) + "\", \"busvoltage_V_max\":" + String(busvoltage_V_max) + ", \"busvoltage_V_min\":" + String(busvoltage_V_min) + ", \"current_A_max\":" + String(current_A_max) + ", \"current_A_min\":" + String(current_A_min) + ", \"power_W_max\":" + String(power_W_max) + ", \"power_W_min\":" + String(power_W_min) + "}");
}