SparkFun Forums 

Where electronics enthusiasts find answers.

#202106
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 temp sensor value and init_temp value are not the same somehow.
It looks as though it should work as it is now since the temp module is returning a 2 digit integer and it looks as though init_pass is returning a 2 digit integer but is only seeing the first digit when comparing in the if statement: if (temp >= init_temp){

I guess the best question I could ask is if the serial monitor is returning a 2 digit integer for Serial.print(init_pass) , one after another after another ie. 252525252525252525 - how different is that from having if(temp >= 25) vs if(temp >= init_pass) ? or is that really an integer that is displaying in the serial monitor? I'm confused.
Code: Select all
#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_temp = 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);
  //Serial.print(temp);
  lcd.write(0xDF);              //Display custom characters '°'
  lcd.print("C");
  delay(200);                
  initialtemperature();
     
   for(byte j=0;j<2;j++){   
  byte init_temp = (initial_temperature[j] - '0');

   if (temp >= init_temp){   
       relayState = HIGH;
       digitalWrite(relay,  relayState);     
       
   }
   Serial.print(init_temp);
   }   
  
  key_pressed = customKeypad.getKey();
  if(key_pressed=='A')
  change(); 
}


void change(){
      j=0;
  int i=0;
  lcd.clear();
  lcd.print("FanOn Temp=");
  lcd.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);
      Serial.print(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);
    

    }