Make timers more better, add uncrustify config
This commit is contained in:
parent
34f37b9dda
commit
91c21a02d5
3593
.uncrustify.cfg
Normal file
3593
.uncrustify.cfg
Normal file
File diff suppressed because it is too large
Load diff
184
src/main.cpp
184
src/main.cpp
|
@ -7,121 +7,129 @@ MHZ19 myMHZ19;
|
|||
Adafruit_AHTX0 aht;
|
||||
LiquidCrystal_I2C LCD(0x27,2,1,0,4,5,6,7,3,POSITIVE);
|
||||
|
||||
int greenpin = 25;
|
||||
int yellownpin = 33;
|
||||
int redpin = 32;
|
||||
#define PIN_LED_GREEN 25
|
||||
#define PIN_LED_YELLOW 33
|
||||
#define PIN_LED_RED 32
|
||||
|
||||
int backlightState = HIGH;
|
||||
int backlightUpdate = 0;
|
||||
|
||||
int update_ival = 0;
|
||||
int override = 0;
|
||||
volatile bool interruptFired = false;
|
||||
unsigned long lastInterrupt = 0;
|
||||
unsigned long lastUpdated = 0;
|
||||
|
||||
unsigned const int updateTimer = 5000;
|
||||
unsigned const int interruptTimer = 500;
|
||||
|
||||
int ready = 0;
|
||||
int cal_time = 5; //minutes
|
||||
|
||||
void led(int red, int green, int yellow)
|
||||
{
|
||||
analogWrite(redpin, red);
|
||||
analogWrite(greenpin, green);
|
||||
analogWrite(yellownpin, yellow);
|
||||
analogWrite(PIN_LED_RED, red);
|
||||
analogWrite(PIN_LED_GREEN, green);
|
||||
analogWrite(PIN_LED_YELLOW, yellow);
|
||||
}
|
||||
|
||||
void IRAM_ATTR BacklightToggle() {
|
||||
backlightUpdate = true;
|
||||
if (!interruptFired)
|
||||
interruptFired = true;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial2.begin(9600, SERIAL_8N1, 16, 17);
|
||||
Serial.begin(9600);
|
||||
Serial2.begin(9600, SERIAL_8N1, 16, 17);
|
||||
|
||||
myMHZ19.begin(Serial2);
|
||||
myMHZ19.autoCalibration();
|
||||
myMHZ19.setRange(2000);
|
||||
myMHZ19.begin(Serial2);
|
||||
myMHZ19.autoCalibration();
|
||||
myMHZ19.setRange(2000);
|
||||
|
||||
Serial.println("MHZ19 init:");
|
||||
char myVersion[4];
|
||||
myMHZ19.getVersion(myVersion);
|
||||
Serial.print("\nFirmware Version: ");
|
||||
for(byte i = 0; i < 4; i++)
|
||||
{
|
||||
Serial.print(myVersion[i]);
|
||||
if(i == 1)
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("MHZ19 init:");
|
||||
char myVersion[4];
|
||||
myMHZ19.getVersion(myVersion);
|
||||
Serial.print("\nFirmware Version: ");
|
||||
for(byte i = 0; i < 4; i++)
|
||||
{
|
||||
Serial.print(myVersion[i]);
|
||||
if(i == 1)
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
|
||||
Serial.print("Range: ");
|
||||
Serial.println(myMHZ19.getRange());
|
||||
Serial.print("Background CO2: ");
|
||||
Serial.println(myMHZ19.getBackgroundCO2());
|
||||
Serial.print("Temperature Cal: ");
|
||||
Serial.println(myMHZ19.getTempAdjustment());
|
||||
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");
|
||||
Serial.print("Range: ");
|
||||
Serial.println(myMHZ19.getRange());
|
||||
Serial.print("Background CO2: ");
|
||||
Serial.println(myMHZ19.getBackgroundCO2());
|
||||
Serial.print("Temperature Cal: ");
|
||||
Serial.println(myMHZ19.getTempAdjustment());
|
||||
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");
|
||||
|
||||
if (!aht.begin()) {
|
||||
Serial.println("Could not find AHT? Check wiring");
|
||||
while (true);
|
||||
}
|
||||
if (!aht.begin()) {
|
||||
Serial.println("Could not find AHT? Check wiring");
|
||||
while (true);
|
||||
}
|
||||
|
||||
LCD.begin(16, 2);
|
||||
LCD.clear();
|
||||
LCD.setBacklight(backlightState);
|
||||
pinMode(23, INPUT_PULLUP);
|
||||
attachInterrupt(23, BacklightToggle, FALLING);
|
||||
LCD.begin(16, 2);
|
||||
LCD.clear();
|
||||
LCD.setBacklight(backlightState);
|
||||
pinMode(23, INPUT_PULLUP);
|
||||
attachInterrupt(23, BacklightToggle, FALLING);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (backlightUpdate) {
|
||||
backlightState = !backlightState;
|
||||
backlightUpdate = false;
|
||||
Serial.printf("Setting backlight to %i \n", backlightState);
|
||||
LCD.setBacklight(backlightState);
|
||||
}
|
||||
if (interruptFired) {
|
||||
if (millis() - lastInterrupt > interruptTimer || lastInterrupt == 0) {
|
||||
backlightState = !backlightState;
|
||||
Serial.printf("Setting backlight to %i \n", backlightState);
|
||||
LCD.setBacklight(backlightState);
|
||||
lastInterrupt = millis();
|
||||
}
|
||||
|
||||
if (update_ival-- > 0) {
|
||||
delay(1000);
|
||||
return;
|
||||
}
|
||||
interruptFired = false;
|
||||
}
|
||||
|
||||
update_ival = 5; //5sec
|
||||
if (millis() - lastUpdated > updateTimer || lastUpdated == 0) {
|
||||
lastUpdated = millis();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
sensors_event_t humidity, temp;
|
||||
aht.getEvent(&humidity, &temp);
|
||||
LCD.setCursor(0, 0);
|
||||
LCD.print(temp.temperature, 1);
|
||||
LCD.print(" C ");
|
||||
LCD.setCursor(9, 0);
|
||||
LCD.print(humidity.relative_humidity, 1);
|
||||
LCD.print("%rH");
|
||||
sensors_event_t humidity, temp;
|
||||
aht.getEvent(&humidity, &temp);
|
||||
LCD.setCursor(0, 0);
|
||||
LCD.print(temp.temperature, 1);
|
||||
LCD.print(" C ");
|
||||
LCD.setCursor(9, 0);
|
||||
LCD.print(humidity.relative_humidity, 1);
|
||||
LCD.print("%rH");
|
||||
|
||||
if (millis() >= (cal_time * 60000) && !override) {
|
||||
LCD.setCursor(0, 1);
|
||||
LCD.print(" Ready.");
|
||||
override = true;
|
||||
}
|
||||
if (millis() >= (cal_time * 60000) && !ready) {
|
||||
LCD.setCursor(0, 1);
|
||||
LCD.print(" Ready.");
|
||||
ready = true;
|
||||
}
|
||||
|
||||
int co2 = myMHZ19.getCO2(false);
|
||||
if (myMHZ19.errorCode != RESULT_OK) {
|
||||
Serial.print("Error. Response Code: ");
|
||||
Serial.println(myMHZ19.errorCode);
|
||||
return;
|
||||
}
|
||||
LCD.setCursor(0, 1);
|
||||
LCD.print(co2);
|
||||
LCD.print("ppm ");
|
||||
int co2 = myMHZ19.getCO2(false);
|
||||
if (myMHZ19.errorCode != RESULT_OK) {
|
||||
Serial.print("Error. Response Code: ");
|
||||
Serial.println(myMHZ19.errorCode);
|
||||
return;
|
||||
}
|
||||
LCD.setCursor(0, 1);
|
||||
LCD.print(co2);
|
||||
LCD.print("ppm ");
|
||||
|
||||
if (co2 < 1000)
|
||||
led(0, 1, 0);
|
||||
else if (co2 < 1500)
|
||||
led(0, 0, 5);
|
||||
else
|
||||
led(5, 0, 0);
|
||||
if (co2 < 1000)
|
||||
led(0, 1, 0);
|
||||
else if (co2 < 1500)
|
||||
led(0, 0, 5);
|
||||
else
|
||||
led(5, 0, 0);
|
||||
|
||||
if (millis() < (cal_time * 60000)) {
|
||||
{
|
||||
LCD.setCursor(8, 1);
|
||||
LCD.printf("Cal..%2lum", ((cal_time * 60000) - millis()) / 60000);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (millis() < (cal_time * 60000)) {
|
||||
{
|
||||
LCD.setCursor(8, 1);
|
||||
LCD.printf("Cal..%2lum", ((cal_time * 60000) - millis()) / 60000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue