How do i store the previous state of a boolean (true or false) using EEPROM

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);    
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Have you looked at the EEPROM examples in the IDE ?

A boolean variable is only a single byte so you can save it using EEPROM.write() and read it back using EEPROM.read()

Read the variable in setup() and write() it when it changes in your sketch. be aware that each EEPROM location has a limited number of writes that are guaranteed to work, so be careful how often you write the value

#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>
#include <EEPROM.h>
#define LOW_1 0
#define LOW_2 1
#define HIGH_1 11
#define HIGH_2 12

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);

//assign storei the value of address n
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);      // You can have 3 different precisions, higher precision takes longer
  sht4.setHeater(SHT4X_NO_HEATER);              // You can have 6 different heater settings


}

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)                           //can add minimum water level to be checked(add desiccant notify)
  {                                                             //can add over flow protection incase float becomes redundant or dehumidifier pump not working
   //tank 1 strong solution                                     //can add pressure sensor/flow sensor to check pressure build up/flow of pump discharge incase valve becomes redundant or pump not working
   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;
      
 
      EEPROM.update(0, false);
      EEPROM.update(1, 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; 

      /*EEPROM.write(LOW_1,lowTrig1 ? 1 : 0);
      EEPROM.write(LOW_2,lowTrig2 ? 1 : 0); */

      EEPROM.write(0, true);
      EEPROM.write(1, 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)                                    //Regeneration to be done between 9AM and 5PM only
{                                                               //Can add temp monitor to notify heater working condition if solution is cold for too long when regeneration begins or turn off regenerator if liquid is too hot for too long(check fan or regenerator)
  if(digitalRead(top1)==HIGH && digitalRead(bottom1)==LOW)      //overflow protection for regenerator tank
    {                                                           //flow sensor to protect pumps
      Serial.println("Regeneration - Tank1");                   //can add a bypass program to regenerate during night time if both tanks are dilute
      digitalWrite(t1r,HIGH);
      digitalWrite(rt1,HIGH);
      digitalWrite(h1,HIGH);
      highTrig1=true;
      EEPROM.write(10,true);


    }
  else if(digitalRead(top1)==LOW && digitalRead(bottom1)==HIGH)
    {
     Serial.println("Tank1 ready"); 
     digitalWrite(t1r,LOW);
     digitalWrite(rt1,LOW);
     digitalWrite(h1,LOW);  
     highTrig1=false; 
     EEPROM.write(10,false);   


    }
  if(digitalRead(top2)==HIGH && digitalRead(bottom2)==LOW)
    {
      Serial.println("Regeneration - Tank2"); 
      digitalWrite(t2r,HIGH);
      digitalWrite(rt2,HIGH);
      digitalWrite(h2,HIGH);
      highTrig2=true; 
      EEPROM.write(11,true);   

    }
   else if(digitalRead(top2)==LOW && digitalRead(bottom2)==HIGH)
    {
      Serial.println("Tank2 ready");
      digitalWrite(t2r,LOW);
      digitalWrite(rt2,LOW);
      digitalWrite(h2,LOW); 
      highTrig2=false;
      EEPROM.write(11,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);    
}

will this work?

You can store boolean values directly in EEPROM:

      lowTrig1=true;
      lowTrig2=false; 
      EEPROM.write(0, lowTrig1);
      EEPROM.write(1, lowTrig2);

Another note - you have always use update method rather than just a write()

All I can say is that the 4 boolean values will be read from EEPROM when the Arduino is powered up or reset. I have not looked in detail at how often you write the values to EEPROM

Using update() is acceptable as an alternative to write(). It will only actually write to EEPROM if the value at that location has changed

Thank you, i'll test it out once and update you guys soon.
replaced write with update

help! it's skipping all the if else if statements and directly executing the dsp and drp relays...

It works as you wrote it

It may not be your problem but, in setup() you EEPROM.read the previous values. However, from the factory all EEPROM locations are set to 0xFF, all bits on. This evaluates as logic true.

You need to establish some initial valid state for the values. You could make another EEPROM location called, I dunno, 'initialState'. In setup() if this location reads true (any non-zero value) then load all the triggers with some default startup state, presumably OFF/false and while you're at it, set initialState to false also. Then proceed as normal.

Next time the code starts up check initialState. It'll now be false so you know you've got previous values stored in EEPROM. Load the triggers with those stored values and proceed.

If you don't think you'll make 100,000 trigger changes your EEPROM should be OK. If you're still concerned there are 'eeprom leveling' algorithms to spread the changes over more area and extend the life.

Great idea! didn't know that EEPROM is default logic true. I'll do what you suggested and give an update soon. Thanks :raised_hands:

Also you can store strings like "ON" and "OFF" and then convert them into boolean values using strcmp() or strstr() function.

1 Like

you can store strings like "ON" and "OFF" and then convert them into boolean values

Aaagh!!! :frowning:

EEPROM.update prevents unnecessary excessive use of the EEPROM if you try many times writing the same value. It can happen due to a wrong code.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.