Defined names for bootmode and removed one unneeded variable

This commit is contained in:
Stefan Heinrichsen 2020-11-26 17:35:41 +01:00
parent 2955909a89
commit 93f0947a93
1 changed files with 39 additions and 45 deletions

View File

@ -15,6 +15,11 @@
// Dauer der Kalibrierungsphase in Milisekunden // Dauer der Kalibrierungsphase in Milisekunden
#define CALINTERVAL 180*1000 #define CALINTERVAL 180*1000
// Boot-Mode Konstanten
#define BOOT_NORMAL 42
#define BOOT_CALIBRATE 23
#define BOOT_UNKNOWN 63
// Pins für den MH-Z19b // Pins für den MH-Z19b
#define RX_PIN 16 #define RX_PIN 16
#define TX_PIN 17 #define TX_PIN 17
@ -30,33 +35,31 @@
#define NUMPIXELS 8 #define NUMPIXELS 8
Preferences preferences; Preferences preferences;
MHZ19 myMHZ19; MHZ19 myMHZ19;
HardwareSerial mySerial(1); HardwareSerial mySerial(1);
SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN); SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LED_PIN, NEO_RGB + NEO_KHZ800); Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LED_PIN, NEO_RGB + NEO_KHZ800);
String ampelversion = "0.12";
unsigned long getDataTimer = 0; unsigned long getDataTimer = 0;
unsigned long getDataTimer1 = 0; unsigned long getDataTimer1 = 0;
int countdown = 0; int countdown = 0;
int lastvals[120]; int lastvals[120];
int dheight; int dheight;
String ampelversion = "0.11"; int safezone = false;
int safezone = 1;
int tocalibrateornot; int tocalibrateornot;
int initloop = 1;
void switchBootMode(int bm){ void switchBootMode(int bm){
switch (bm){ switch (bm){
case 23: case BOOT_CALIBRATE:
preferences.putUInt("cal", 42); preferences.putUInt("cal", BOOT_NORMAL);
Serial.println("Startmodus nächster Reboot: Messmodus"); Serial.println("Startmodus nächster Reboot: Messmodus");
break; break;
case 42: case BOOT_NORMAL:
preferences.putUInt("cal", 23); preferences.putUInt("cal", BOOT_CALIBRATE);
Serial.println("Startmodus nächster Reboot: Kalibrierungsmodus"); Serial.println("Startmodus nächster Reboot: Kalibrierungsmodus");
break; break;
case 69: case BOOT_UNKNOWN:
Serial.println("EEPROM lesen war nicht möglich!"); Serial.println("EEPROM lesen war nicht möglich!");
break; break;
default: default:
@ -73,15 +76,15 @@ void setup() {
// Ab hier Bootmodus initialisieren und festlegen // Ab hier Bootmodus initialisieren und festlegen
preferences.begin("co2", false); preferences.begin("co2", false);
tocalibrateornot = preferences.getUInt("cal",69); // wir lesen unser flag ein, tocalibrateornot = preferences.getUInt("cal", BOOT_UNKNOWN); // wir lesen unser flag ein,
// 23 = reboot vor safezone, wir wollen kalibrieren, // 23 = reboot vor safezone, wir wollen kalibrieren,
// 42 = reboot nach safezone, wir tun nichts // 42 = reboot nach safezone, wir tun nichts
preferences.putUInt("cal", 23); // wir sind gerade gestartet preferences.putUInt("cal", BOOT_CALIBRATE); // wir sind gerade gestartet
switch(tocalibrateornot){ switch(tocalibrateornot){
case 23: case BOOT_CALIBRATE:
Serial.println("Startmodus Aktuell: Kalibrierungsmodus"); Serial.println("Startmodus Aktuell: Kalibrierungsmodus");
break; break;
case 42: case BOOT_NORMAL:
Serial.println("Startmodus Aktuell: Messmodus"); Serial.println("Startmodus Aktuell: Messmodus");
break; break;
} }
@ -166,7 +169,7 @@ void calibrateCO2() {
delay(500); delay(500);
display.clear(); display.drawString(64, 0, "Fertig!"); display.display(); display.clear(); display.drawString(64, 0, "Fertig!"); display.display();
preferences.putUInt("cal", 42); preferences.putUInt("cal", BOOT_NORMAL);
delay(2000); delay(2000);
display.clear(); display.setFont(Cousine_Regular_54); display.clear(); display.setFont(Cousine_Regular_54);
@ -193,7 +196,7 @@ void readCO2(){
} }
} }
// Farbe des LED-Rings setzen // Farbe des LED-Rings setzen
if (tocalibrateornot == 42) {set_led_color(CO2);} if (tocalibrateornot == BOOT_NORMAL) { set_led_color(CO2); }
//display.setLogBuffer(1, 30); //display.setLogBuffer(1, 30);
display.setFont(Cousine_Regular_54); display.setFont(Cousine_Regular_54);
display.setTextAlignment(TEXT_ALIGN_CENTER); display.setTextAlignment(TEXT_ALIGN_CENTER);
@ -213,39 +216,30 @@ void readCO2(){
} }
void loop() { void loop() {
if (initloop == 1) { if (millis() > 10000) {
if (millis() > 10000) { Serial.println("=== 10 Sekunden im Betrieb, nächster Boot im Normalmodus ===");
Serial.println("=== safe zone ==="); switchBootMode(BOOT_CALIBRATE);
switchBootMode(23); // preferences.putUInt("cal", NORMAL); // wir haben die safe zone erreicht, beim naechsten boot nicht kalibrieren!
// preferences.putUInt("cal", 42); // wir haben die safe zone erreicht, beim naechsten boot nicht kalibrieren! safezone = true;
safezone = 0; initloop = 0; }
}
}
if (safezone == 0){ if (safezone){
if (tocalibrateornot == 23){ if (tocalibrateornot == BOOT_CALIBRATE){
if (millis() - getDataTimer1 <= CALINTERVAL) { if (millis() - getDataTimer1 <= CALINTERVAL) {
rainbow(10); rainbow(10);
display.clear(); display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER); display.setTextAlignment(TEXT_ALIGN_CENTER);
//countdown = (CALINTERVAL - (getDataTimer1 + millis() * -1)) / 1000; //countdown = (CALINTERVAL - (getDataTimer1 + millis() * -1)) / 1000;
//countdown = (millis() + getDataTimer1 - CALINTERVAL) * -1 / 1000; //countdown = (millis() + getDataTimer1 - CALINTERVAL) * -1 / 1000;
countdown = ((getDataTimer1 + CALINTERVAL) - millis()) / 1000; countdown = ((getDataTimer1 + CALINTERVAL) - millis()) / 1000;
Serial.println("Countdown: " + String(countdown)); Serial.println("Countdown: " + String(countdown));
display.drawString(64, 0, String(countdown)); display.drawString(64, 0, String(countdown));
display.display(); display.display();
}
else if (millis() - getDataTimer1 >= CALINTERVAL) {
calibrateCO2();
getDataTimer1 = millis();
}
} }
else if (tocalibrateornot ==42) { else if (millis() - getDataTimer1 >= CALINTERVAL) {
if(initloop==1){ calibrateCO2();
Serial.println("fake news, nobody has the intention to do calibration...."); getDataTimer1 = millis();
initloop=0;
} }
} }
} }