Programming Rele W/ CO2 sensor.


I am configuring a program with an MH-z19B CO2 sensor, arduino mega and a 2004 display. The program will then have temperature and humidity sensors, RTC, and ESP8266, but now I am locked the programming code to run a relay.

Yesterday I managed to make the sensor measure the ppm of CO2 after 2 days (I’m a beginner), today what I have to configure is the following:

If the ppm is less than 1200, I must turn on a relay 3 seconds (solenoid command) then it must remain off for another 15 seconds, and re-enter the same condition. If the ppm is greater than 1500 it should remain off. And if the ppm is greater than 2000, another relay must be turned on for 18 seconds.

I am a bit stuck with the millis functions and the conditional ones that I must implement, as well as how to keep the time count with two different time intervals.

I am attentive to any comments.

Thank you!

You forgot to post your code.
Must be the amnesia.

I leave the code, it doesn’t have much of what I ask because I don’t know how to really start. By the way, the ppm always gives me 0 and I can’t correct them, the ones that should be in the function are the ppm2

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

#define pwmPin 10

SoftwareSerial mySerial(15, 14); // RX, TX
LiquidCrystal_I2C lcd(0x27,20,4);

byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
unsigned char response[9]; 
unsigned long th, tl, ppm, ppm2, ppm3, tpwm, p1, p2 = 0;
unsigned int ppmm;
const int pinRELE = 40;
unsigned long time;
unsigned long interval = 3000;
unsigned long interval2 = 10000;
unsigned long PreviousMillis;

void setup() {
 pinMode(pwmPin, INPUT);
 th = pulseIn(pwmPin, HIGH, 3000000); // use microseconds
 tl = pulseIn(pwmPin, LOW, 3000000);
 tpwm = th + tl; // actual pulse width
 Serial.print("PWM-time: ");
 Serial.println(" us");
 p1 = tpwm/502; // start pulse width
 p2 = tpwm/251; // start and end pulse width combined
 pinMode(pinRELE, OUTPUT);
 previousMillis = millis();

void loop() {
   memset(response, 0, 9);
 mySerial.readBytes(response, 9);
 int i;
 byte crc = 0;
 for (i = 1; i < 8; i++) crc+=response[i];
 crc = 255 - crc;
  if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
   } else {
   unsigned int responseHighh = (unsigned int) response[2];
   unsigned int responseLoww = (unsigned int) response[3];
   ppmm = (256*responseHighh) + responseLoww;

 //CO2 via pwm
 do {
   th = pulseIn(pwmPin, HIGH, 1004000) / 1000;
   tl = 1004 - th;
   ppm2 = 2000 * (th-2)/(th+tl-4);
   ppm3 = 2000 * (th-p1)/(tpwm-p2);
 } while (th == 0);
 unsigned long currentMillis = millis();
 if (ppm2<1500)
 {if (unsigned long)((currentMillis - previousMillis)< interval){
 digitalWrite (pinRELE, HIGH)

 lcd.init();                      // initialize the lcd 
 lcd.print(" ");

Please post in code tags so the forum software doesn't mess with it - note that half your code above is in italics.

That aside, what you've posted doesn't compile.