i want my code to remember the states of lowTrig1, lowTrig2, highTrig1, highTrig2 (whether they were true or false previously) in case of power outage.
#include <Wire.h>
#include <math.h>
#include <TimeLib.h>
#define NTC0 A4
#define NTC1 A5
#define NTC2 A6
#define NTC3 A7
#define NUMSAMPLES 5
#define SERIESRESISTOR 10000
#include "Adafruit_SHT4x.h"
#define del 1000
#include <LiquidCrystal_I2C.h>
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
int samles[NUMSAMPLES];
int top1=4; //float 1
int bottom1=5;
int top2=2; //float 2
int bottom2=7;
int t1d=26; //tank 1 to Dehumidifier
int dt1=27; //Dehumidifier to tank 1
int t2d=29; //tank 2 to Dehumidifier
int dt2=28; //Dehumidifier to tank 2
int t1r=30; //tank 1 to Regenerator
int rt1=31; //Regenerator to tank 1
int t2r=32; //tank 2 to Regenerator
int rt2=33; //Regenerator to tank 2
int dsp=22; //Dehumidifier Supply pump
int drp=23; //Dehumidifier Return pump
int rsp=24; //Regenerator Supply pump
int rrp=25; //Regenerator Return pump
int h1=45; //Heater 1
int h2=46; //Heater 2
int rf=34; //Regenerator fan
int df=35; //Dehumidifier fan
//fan Interval set to 20 mins
unsigned long previousMillis=0;
const long interval = 60000;
bool lowTrig1=true;
bool lowTrig2=false;
bool highTrig1;
bool highTrig2;
void setup() {
Serial.begin(9600);
/*pin assign*/
//Float pins
pinMode(top1, INPUT_PULLUP);
pinMode(bottom1,INPUT_PULLUP);
pinMode(top2, INPUT_PULLUP);
pinMode(bottom2,INPUT_PULLUP);
//Valve relays
pinMode(t1d,OUTPUT);
pinMode(t2d,OUTPUT);
pinMode(t1r,OUTPUT);
pinMode(t2r,OUTPUT);
pinMode(dt1,OUTPUT);
pinMode(dt2,OUTPUT);
pinMode(rt1,OUTPUT);
pinMode(rt2,OUTPUT);
//12V Relays
pinMode(h1,OUTPUT);
pinMode(h2,OUTPUT);
//Pump Relays
pinMode(dsp,OUTPUT);
pinMode(drp,OUTPUT);
pinMode(rsp,OUTPUT);
pinMode(rrp,OUTPUT);
//Fan Relays
pinMode(rf,OUTPUT);
pinMode(df,OUTPUT);
lowTrig1=EEPROM.read(0);
lowTrig2=EEPROM.read(1);
highTrig1=EEPROM.read(10);
highTrig2=EEPROM.read(11);
// change to current hr,min,sec,day,month,yr during setup
setTime(11,38,30,11,1,2024);
if (! sht4.begin())
{
Serial.println("Couldn't find SHT4x");
delay(1000);
sht4.begin();
}
sht4.setPrecision(SHT4X_HIGH_PRECISION);
sht4.setHeater(SHT4X_NO_HEATER);
}
void loop() {
unsigned long currentMillis=millis();
/*SHT reading*/
sensors_event_t humidity, temp;
sht4.getEvent(&humidity, &temp);
float sht_temp = temp.temperature;
float sht_hum = humidity.relative_humidity;
Serial.print("Room Humidity: ");
Serial.print(sht_hum);
Serial.print("\n");
/*Dehumidification*/
if(sht_hum>65){
Serial.print("Dehumidifier ON\t\t");
if(lowTrig1==true && lowTrig2==false)
{
//tank 1 strong solution
if(digitalRead(top1)==LOW && digitalRead(bottom1)==HIGH)
{
//dehumidifier valves
digitalWrite(t1d,HIGH);//Supply - Tank1
digitalWrite(dt1,HIGH);
digitalWrite(t2d,LOW);
digitalWrite(dt2,LOW);
Serial.println("Desiccant supply - Tank1");
}
//tank 1 weak solution
else if(digitalRead(top1)==HIGH && digitalRead(bottom1)==LOW)
{
Serial.println("Weak solution, switching tanks. Standby..");
digitalWrite(dsp,LOW);
digitalWrite(drp,LOW);
//dehumidifier valves
digitalWrite(t1d,LOW);
digitalWrite(dt1,LOW);
digitalWrite(t2d,HIGH);//Supply - Tank2
digitalWrite(dt2,HIGH);
delay(3000);
lowTrig1=false;
lowTrig2=true;
}
else if(lowTrig1==true)
{
//dehumidifier valves
digitalWrite(t1d,HIGH);//Supply - Tank1
digitalWrite(dt1,HIGH);
digitalWrite(t2d,LOW);
digitalWrite(dt2,LOW);
Serial.println("Desiccant supply - Tank1");
}
else if(lowTrig1==false)
{
//dehumidifier valves
digitalWrite(t1d,LOW);
digitalWrite(dt1,LOW);
digitalWrite(t2d,HIGH);//Supply - Tank2
digitalWrite(dt2,HIGH);
Serial.println("Desiccant supply - Tank2");
}
}
else if(lowTrig1==false && lowTrig2==true)
{
if(digitalRead(top2)==LOW && digitalRead(bottom2)==HIGH)
{
//dehumidifier valves
digitalWrite(t1d,LOW);
digitalWrite(dt1,LOW);
digitalWrite(t2d,HIGH);//Supply -Tank2
digitalWrite(dt2,HIGH);
Serial.println("Desiccant supply - Tank2");
}
else if(digitalRead(top2)==HIGH && digitalRead(bottom2)==LOW)
{
Serial.println("Weak solution, switching tanks. Standby..");
digitalWrite(dsp,LOW);
digitalWrite(drp,LOW);
//dehumidifier valves
digitalWrite(t1d,HIGH);//Supply - Tank1
digitalWrite(dt1,HIGH);
digitalWrite(t2d,LOW);
digitalWrite(dt2,LOW);
delay(3000);
lowTrig1=true;
lowTrig2=false;
}
else if(lowTrig2==true)
{
//dehumidifier valves
digitalWrite(t1d,LOW);
digitalWrite(dt1,LOW);
digitalWrite(t2d,HIGH); //Supply - Tank2
digitalWrite(dt2,HIGH);
Serial.println("Desiccant supply - Tank2");
}
else if(lowTrig2==false)
{
//dehumidifier valves
digitalWrite(t1d,LOW); //Supply - Tank1
digitalWrite(dt1,LOW);
digitalWrite(t2d,HIGH);
digitalWrite(dt2,HIGH);
Serial.print(" Desiccant supply - Tank1");
}
}
digitalWrite(dsp,HIGH);//Dehumidifier pumps ON
digitalWrite(drp,HIGH);
digitalWrite(df,HIGH); //Dehumidifier Fan ON
}
else if (sht_hum<=61)
{
Serial.print("Dehumidifier OFF\t\t");
//All valves and pumps in dehumidifier circuit OFF
digitalWrite(t1d,LOW);
digitalWrite(dt1,LOW);
digitalWrite(t2d,LOW);
digitalWrite(dt2,LOW);
digitalWrite(dsp,LOW);
digitalWrite(drp,LOW);
//logic for fan loop
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
digitalWrite(df,HIGH); //Turn ON fan
delay(10000); //keep fan ON for 1 minute
digitalWrite(df,LOW); //Turn fan OFF
if(digitalRead(df)==HIGH)
{
Serial.println("FCU ON");
}
else
{
Serial.println("FCU OFF");
}
}
else
{
digitalWrite(df,LOW);
Serial.println("FCU OFF");
}
}
/*Regeneration*/
if(hour()>= 9 && hour() <17)
{
if(digitalRead(top1)==HIGH && digitalRead(bottom1)==LOW)
{
Serial.println("Regeneration - Tank1");
digitalWrite(t1r,HIGH);
digitalWrite(rt1,HIGH);
digitalWrite(h1,HIGH);
highTrig1=true;
}
else if(digitalRead(top1)==LOW && digitalRead(bottom1)==HIGH)
{
Serial.println("Tank1 ready");
digitalWrite(t1r,LOW);
digitalWrite(rt1,LOW);
digitalWrite(h1,LOW);
highTrig1=false;
}
if(digitalRead(top2)==HIGH && digitalRead(bottom2)==LOW)
{
Serial.println("Regeneration - Tank2");
digitalWrite(t2r,HIGH);
digitalWrite(rt2,HIGH);
digitalWrite(h2,HIGH);
highTrig2=true;
}
else if(digitalRead(top2)==LOW && digitalRead(bottom2)==HIGH)
{
Serial.println("Tank2 ready");
digitalWrite(t2r,LOW);
digitalWrite(rt2,LOW);
digitalWrite(h2,LOW);
highTrig2=false;
}
if(highTrig1==true || highTrig2==true)
{
Serial.println("Regenerator ON\n\n");
digitalWrite(rsp,HIGH);
digitalWrite(rrp,HIGH);
}
else if(highTrig1==false && highTrig2 == false)
{
Serial.println("Regenerator OFF\n\n");
digitalWrite(rsp,LOW);
digitalWrite(rrp,LOW);
}
}
else
{
digitalWrite(rsp,LOW);
digitalWrite(rrp,LOW);
digitalWrite(t1r,LOW);
digitalWrite(rt1,LOW);
digitalWrite(t2r,LOW);
digitalWrite(rt2,LOW);
}
delay(1000);
}