Stuck on fahrenheit

Hi everyone, I am new to programming, and I’m trying to build temperature control relay, everything is working except I can’t get readings in Celsius on the LCD…
I appreciate your help!



#include <OneWire.h>
#include <DallasTemperature.h>

// #define DEBUG 1
#define BLUETOOTH_ENABLED 1

float currentTemprature;  // Current Temprature store in farenheight

int RelayHeat = 6;
int RelayCool = 7;

const int RELAY_NONE = 0;
const int RELAY_HEAT = 1;
const int RELAY_COOL = 2;
const int RELAY_OVERRIDE_HEAT = 3;
const int RELAY_OVERRIDE_COOL = 4;
int RelayStatus = RELAY_NONE;

const int CONTROL_AUTO = 0;
const int CONTROL_HEAT = 1;
const int CONTROL_COOL = 2;
int controlMode = CONTROL_AUTO;

boolean controlCommandReceived = false;

const int SCREENMODE_DISPLAYTEMP = 1;
const int SCREENMODE_SETUP = 2;
const int SCREENMODE_SCREEN_SAVER = 3;
int screenMode = SCREENMODE_DISPLAYTEMP;

const int DISPLAYMODE_MAIN    = 0;
const int DISPLAYMODE_TARGET  = 1;
const int DISPLAYMODE_MINMAX  = 2;
int currentDisplayMode = DISPLAYMODE_MAIN;

unsigned long screenTime;

const int UNIT_FARENHEIGHT = 1;
const int UNIT_CELSIUS = 2;
int unit =  UNIT_CELSIUS;

float deviation = 1.0;
int targetTemp = 70;
unsigned long tempratureTimer;

//temp sensor defines

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 9
#define TEMPERATURE_PRECISION 9 // Lower resolution
#define TEMPERATURE_MINIMUM 0
#define TEMPERATURE_MAXIMUM 275

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int numberOfDevices; // Number of temperature devices found

float min = 1024;
float max = -100;

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

void setup(void)
{
  displayLogo();
  
  //setup temp sensor
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo.");

  // Start up the library
  sensors.begin();
  
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  
  // Loop through each device, print out address
  for(int i=0;i<numberOfDevices; i++)
  {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
    {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
    
      Serial.println("Setting resolution to ");
      Serial.println();
      Serial.println(TEMPERATURE_PRECISION, DEC);
      Serial.println();
      // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
      delay(500);
      Serial.print("Resolution actually set to: ");
      Serial.print(sensors.getResolution(tempDeviceAddress), DEC); 
      delay(500);
      Serial.println();
      //break;
    }else{
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
    }    
  }

  Serial.begin(9600);
  Serial.println();
  Serial.print( "Setting up Rotary Encoder" );
  //rotary encoder setup

  rotarySetup();
  Serial.begin (9600);

  //
  //  Releay Switch Setup
  //
  pinMode(RelayHeat, OUTPUT); //Set Pin12 as output 
  pinMode(RelayCool, OUTPUT); //Set Pin12 as output 
  digitalWrite(RelayHeat, HIGH); //Turn off relay 
  digitalWrite(RelayCool, HIGH); //Turn off relay 
  RelayStatus = RELAY_NONE;

  #ifdef BLUETOOTH_ENABLED  
  bluetoothSetup();
  #endif
  
  relaySetup();
  
  screenTime = millis();
  tempratureTimer = millis();

  delay( 4000 );
  lcdclear();
  setTempratureTemplate();  
}


void loop(void)
{ 
  if(screenMode != SCREENMODE_SETUP ){
    temperatureInterface();
    relayControl();
    #ifdef BLUETOOTH_ENABLED
      bluetoothInterface();
    #endif
  }
  rotary();
}



// function to print the temperature for a device
void printTemperature( )
{
    if(screenMode == SCREENMODE_DISPLAYTEMP ){
      lcdsetCursor (5,0);         // go to col 16 of the last row
      lcdprint( getDisplayTemperature( currentTemprature ) );           // update the display with a new number
  
      //
      //  Screensaver code
      //
      unsigned long delayTime = 60000 * 15;
      unsigned long currentTime = millis();
      if( screenTime + delayTime < currentTime ){
        screenMode = SCREENMODE_SCREEN_SAVER;
        lcdsetBacklightLOW();
      }
    }
}

String getDisplayTemperature( float tempF ){
    String strTemp = "";
    if(unit == UNIT_FARENHEIGHT)
    {
      strTemp = String(tempF);
    }
    else{
      float tempF = DallasTemperature::toCelsius( tempF );
      strTemp = String(tempF);
    }
    strTemp = strTemp.substring( 0, 4 );
    return strTemp;
}

float getDisplayTemperatureNum( float tempF ){
    float nTemp = tempF;
    if(unit == UNIT_CELSIUS)
    {
      nTemp = DallasTemperature::toCelsius(tempF);
      //nTemp = DallasTemperature::toFahrenheit(tempC);
    }
    return nTemp;
}

//
//  Get Temprature Evry 1 Sec and Display
//
void temperatureInterface()
{
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus

  unsigned long currentTime = millis();
  if( tempratureTimer + 1000 < currentTime ){

    sensors.requestTemperatures(); // Send the command to get temperatures
    // Loop through each device, print out temperature data
    for(int i=0;i<numberOfDevices; i++)
    {
      // Search the wire for address
      if(sensors.getAddress(tempDeviceAddress, i))
      {
        // Output the device ID
        //    Serial.print("Temperature for device: ");
        //    Serial.println(i,DEC);
        //float tempC = sensors.getTempC(tempDeviceAddress);
        //currentTemprature = DallasTemperature::toFahrenheit(tempC);
        currentTemprature = sensors.getTempF(tempDeviceAddress);
        if( currentTemprature >= TEMPERATURE_MINIMUM && currentTemprature <= TEMPERATURE_MAXIMUM )
        {
          if(currentTemprature >= max)
          {
            max = currentTemprature;
            if( currentDisplayMode == DISPLAYMODE_MINMAX ){
              lcdsetCursor ( 11, 1 );            // go to the 2nd row
              lcdprint( getDisplayTemperature( max ) ); // pad string with spaces for centering
            }
          }
          if(currentTemprature <= min)
          {
            min = currentTemprature;
            if( currentDisplayMode == DISPLAYMODE_MINMAX ){
              lcdsetCursor ( 3, 1 );            // go to the 2nd row
              lcdprint( getDisplayTemperature( min ) ); // pad string with spaces for centering
            }
          }
        }
    }
    // It responds almost immediately. Let's print out the data
    printTemperature( ); // Use a simple function to print out the data
   } 
    //else ghost device! Check your power requirements and cabling
  }
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}```

Are you converting F to C to display it?
I didn’t see C = (F-32)*5/9 anywhere to do that.

I thought “DallasTemperature.h” is doing that for me?

I haven’t used the library, I don’t know what that does.

If you send the variables in question to the serial monitor instead of the LCD, are you getting the expected results?

Are you seeing any expected results on the LCD?

How you debug depends on what is occurring. You haven’t described much of what is occurring so far with your code.

Look at this code.

String getDisplayTemperature( float tempF ){
    String strTemp = "";
    if(unit == UNIT_FARENHEIGHT)
    {
      strTemp = String(tempF);
    }
    else{
      float tempF = DallasTemperature::toCelsius( tempF );  // < -------- here
      strTemp = String(tempF);
    }
    strTemp = strTemp.substring( 0, 4 );
    return strTemp;
}

You are redefining tempF inside the function. Who knows what happens. Remove float.

I took a code from github, temperature control with LCD, there is a rotary knob and a small menu.
Inside menu you can choose F or C and set temperature. When in F everything works fine, on LCD and on serial. The problem is when I switch from F to C in the menu on the LCD it shows -17, and it doesn’t allow me to set the temperature.

I just changed this part:

strTemp = String(tempC) From tempF to tempC

It is finally working!

Thank you so much for your help and pointing me in the right direction!

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