#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#include "MHZ19.h"
#define RX_PIN 7
#define TX_PIN 8
#define BAUDRATE 9600
MHZ19 myMHZ19;
#include <SoftwareSerial.h> // Remove if using HardwareSerial or non-uno compatible device
SoftwareSerial mySerial(RX_PIN, TX_PIN); // (Uno example) create device to MH-Z19 serial
unsigned long timeElapse = 0;
unsigned long timeElapsePer = 0;//12e5; // 20 minutes in milliseconds
#define CLK 4
#define DT 5
#define SW 6
#include "GyverEncoder.h"
Encoder enc1(CLK, DT, SW); // для работы c кнопкой
bool ekran = 0;
#define FanRelay 10
bool izm = 1;
int CO2Value = 1000;
unsigned long TimeValue = 300000;
bool TimeBool = 0;
unsigned long TimePer = 0;
int par = 0;
bool call = 0;
char buffer[6];
#include <EEPROM.h>
byte prov = 15, provch = 0;
int filtCO2 = 0;
int gist = 2;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
myservo.attach(3); // attaches the servo on pin 9 to the servo object
myservo.write(0); // tell servo to go to position in variable 'pos'
Serial.begin(115200);
mySerial.begin(BAUDRATE); // sensor serial
myMHZ19.begin(mySerial); // pass to library
myMHZ19.autoCalibration(false); // make sure auto calibration is off
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF"); // now print it's status
// timeElapse =
enc1.setType(TYPE2);
EEPROM.get(1, provch);
if (provch != prov)
{
EEPROM.put(1, prov);
EEPROM.put(5, CO2Value);
EEPROM.put(10, TimeValue);
}
else
{
EEPROM.get(5, CO2Value);
EEPROM.get(10, TimeValue);
Serial.print("CO2 = ");
Serial.println(CO2Value);
Serial.print("Time = ");
Serial.println(TimeValue / 1000);
}
}
void loop() {
enc1.tick();
if (enc1.isRight())
{
Serial.println("Right"); // если был поворот
if (par == 0 && CO2Value < 3000) {
EEPROM.put(5, CO2Value);
CO2Value++;
Serial.print("CO2 = ");
Serial.println(CO2Value);
}
else if (par == 1 && TimeValue < 540000) {
EEPROM.put(10, TimeValue);
TimeValue = TimeValue + 1000;
Serial.print("Time = ");
Serial.println(TimeValue / 1000);
}
else if (par == 2)
call = 1;
}
if (enc1.isLeft())
{
Serial.println("Left");
if (par == 0 && CO2Value > 1) {
EEPROM.put(5, CO2Value);
CO2Value--;
Serial.print("CO2 = ");
Serial.println(CO2Value);
}
else if (par == 1 && TimeValue > 1000) {
EEPROM.put(10, TimeValue);
TimeValue = TimeValue - 1000;
Serial.print("Time = ");
Serial.println(TimeValue / 1000);
}
else if (par == 2)
call = 1;
}
if (enc1.isClick())
{
Serial.println("Click"); // отпускание кнопки (+ дебаунс)
par < 2 && ekran == 1 ? par++ : par = 0;
}
if (enc1.isHolded())
{
Serial.println("Holded"); // если была удержана и энк не поворачивался
ekran = !ekran;
lcd.clear();
izm = 1;
}
if (ekran == 0)
{
if (millis() - timeElapse >= 2000 || izm == 1) // Check if interval has elapsed (non-blocking delay() equivalent)
{
int CO2;
CO2 = myMHZ19.getCO2();
// Serial.print("CO2 (ppm): ");
// Serial.println(CO2);
filtCO2 += (CO2 - filtCO2) * 0.1;
// Serial.print("CO2Filtr (ppm): ");
// Serial.println(filtCO2);
lcd.setCursor(0, 0);
lcd.print("CO2f (ppm): ");
sprintf (buffer, "%4d", filtCO2);
lcd.print(buffer);
if (filtCO2 >= CO2Value && filtCO2 >= CO2Value + gist)
{
TimeBool = 1;
myservo.write(180); // tell servo to go to position in variable 'pos'
// Serial.println("180");
}
else if (filtCO2 < CO2Value && filtCO2 <= CO2Value - gist)
{
TimeBool = 0;
myservo.write(0); // tell servo to go to position in variable 'pos'
TimePer = millis(); // Update interval
// Serial.println("0");
}
if (millis() - TimePer >= TimeValue && TimeBool == 1)
digitalWrite(FanRelay, HIGH);
else
digitalWrite(FanRelay, LOW);
int8_t Temp; // Buffer for temperature
Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius)
// Serial.print("Temperature (C): ");
// Serial.println(Temp);
izm = 0;
timeElapse = millis(); // Update interval
}
}
else if (ekran == 1)
{
if (millis() - timeElapse >= 400 || izm == 1) // Check if interval has elapsed (non-blocking delay() equivalent)
{
for (int i = 0; i < 3; i++)
{
i < 2 ? lcd.setCursor(0, i) : lcd.setCursor(12, 1);
par == i ? lcd.print("*") : lcd.print(" ");
}
lcd.setCursor(1, 0);
lcd.print("CO2 (ppm): ");
sprintf (buffer, "%4d", CO2Value);
lcd.print(buffer);
lcd.setCursor(1, 1);
lcd.print("Time: ");
uint32_t sec = TimeValue / 1000ul; // полное количество секунд
// int timeHours = (sec / 3600ul); // часы
int timeMins = (sec % 3600ul) / 60ul; // минуты
int timeSecs = (sec % 3600ul) % 60ul; // секунды
sprintf (buffer, "%1d:%02d", timeMins, timeSecs);
lcd.print(buffer);
lcd.setCursor(13, 1);
lcd.print("Cal");
izm = 0;
digitalWrite(FanRelay, LOW);
timeElapse = millis(); // Update interval
}
if (call == 1) {
lcd.setCursor(13, 1);
if (millis() < timeElapsePer)
lcd.print("Err");
else
{
myMHZ19.calibrate(); // Take a reading which be used as the zero point for 400 ppm
lcd.print("OK ");
}
delay(1000);
call = 0;
timeElapse = millis(); // Update interval
}
}
}