1

I'm using a DHT11 to read temperature to control a relay that will control a fan. When 'A' is pressed it allows the user to update the temp value that the relay will go HIGH at. The value is being stored and read to and from the EEPROM but they are not being compared properly in the if statement. Please help!

#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <dht11.h>                 
dht11 DHT;                 

char temperature[2];
char initial_temperature[2];
char new_temperature[2];
char key_pressed=0;

const int relay = 8; 
const int dht11_data = 6;     
int temp=0;
int relayState = LOW;         
int i=0;
int j=0;
int init_pass = 0;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {38, 40, 42, 44};
byte colPins[COLS] = {46, 48, 50, 52};

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 //initialize an instance of class NewKeypad
 Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

 void setup() {
 Serial.begin(9600);
 pinMode(relay, OUTPUT);
 digitalWrite(relay, relayState);
 lcd.begin(16,2);
 lcd.print("  Welcome to ");
 lcd.setCursor(0,1);
 lcd.print("     Test 2");
 delay(2000);
 lcd.clear();


   }

void loop() {

DHT.read(dht11_data);
temp=DHT.temperature;

lcd.clear();                   
lcd.setCursor(5,0) ;         
lcd.print("Temp");            //display"Temp="
lcd.setCursor(5,1);
lcd.print(temp);
lcd.write(0xDF);              //Display custom characters '°'
lcd.print("C");

delay(200);               
initialtemperature();


       // here is the problem - comparing temp>=init_pass
 // 'temp' will not compare with value of 'init_pass' 
 // like it does with a number value in it's place.

for(int j=0;j<2;j++){   
init_pass = (initial_temperature[j] - '0');

if (temp >= (init_pass)){

 if(init_pass==2)
   relayState = HIGH;
   digitalWrite(relay,  relayState);

   Serial.print(init_pass);
  }
  }   

 key_pressed = customKeypad.getKey();
 if(key_pressed=='A')
 change();
}


void change(){
  j=0;
int i=0;
Serial.begin(9600);
lcd.clear();
lcd.print("FanOn Temp=");
lcd.print(initial_temperature);
// Serial.print(initial_temperature); 
lcd.write(0xDF);
lcd.write("C");   
lcd.setCursor(0,1);
lcd.print("New Temp=");
delay(200);
while(i<2)

{

char pressed=customKeypad.getKey();
if(pressed){
  new_temperature[i++]=pressed;
  lcd.print(pressed);
  EEPROM.write(j,pressed);
  j++;
}

pressed=0;

}
if(i==2){
lcd.write(0xDF);
lcd.write("C");         
delay(1000);
lcd.clear();
lcd.print("Saved");
delay(1000);
  }
  }

 void initialtemperature(){


for (int j = 0; j < 2; j++)
initial_temperature[j] = EEPROM.read(j);


}
8
  • temp is not comparing with init_pass ... if (temp >= (init_pass)){ if(init_pass==2) relayState = HIGH; digitalWrite(relay, relayState); Serial.print(init_pass); }
    – TEEJ
    Feb 16, 2019 at 21:37
  • btw it was formatted until I posted it... not sure what I did
    – TEEJ
    Feb 16, 2019 at 21:42
  • print as in Serial.print? I was using that for testing. If you mean something else, can you provide an example?
    – TEEJ
    Feb 16, 2019 at 22:26
  • Don't post >100 lines of code and say something vague like "... they are not being compared properly in the if statement." Delete code that isn't germane to your question, and point out the specific line that is giving you trouble (along with a description of any setup or context we need in order to understand the line in question.)
    – Duncan C
    Feb 16, 2019 at 23:48
  • the problem starts at: if (temp >= (init_pass)){ comparing temp>=init_pass 'temp' will not compare with value of 'init_pass' like it does with a number value in it's place.
    – TEEJ
    Feb 17, 2019 at 0:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.