Redifintion of int red error issue with the code

#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define RELAY1  4  // Relay heating
#define RELAY2  6  // Relay cooling


int red = 8 // red LED heating
int blue = 2 // blue LED cooling


#define BACKLIGHT_PIN 13
#define ONE_WIRE_BUS 7

LiquidCrystal_I2C lcd(0x27F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

{

  void setup()
  Serial.begin(9600);
  sensors.begin();
  lcd.begin(20, 4);
  lcd.backlight();


  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);

}

{
  void loop()


  lcd.setCursor(0, 0);
  lcd.print(" TEMP CONTROL");

}

{
  sensors.requestTemperatures();
  float temperature = sensors.getTempCByIndex(0);
  lcd.setCursor(0, 2);
  lcd.print("      ");
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("\337C");
}


if (temperature < 25) ////// Had a bogus ';' here
{
  digitalWrite(red, HIGH);
  digitalWrite(blue, LOW);
  digitalWrite(RELAY1, 0);
  digitalWrite(RELAY2, 1);

  lcd.setCursor(0, 3);
  lcd.print("      Heating");

}


else if (temperature > 25) ////// Had a bogus ';' here
{
  digitalWrite(RELAY1, 1);
  digitalWrite(RELAY2, 0);
  digitalWrite(red, LOW);
  digitalWrite(blue, HIGH);


  lcd.setCursor(0, 4);
  lcd.print("      Cooling ");
  lcd.setCursor(0, 3);
}

Hello Everyone,
I have shared the code above, it is for a temperature sensor and I am using if and else condition, but I am having an issue of redefinition of int red error, can anyone please suggest any input ?

The curly brackets for setup() and loop() are in the wrong places. Look at some example code to see the proper syntax.

Plus a handful of other errors...

int red = 8 // red LED heating
int blue = 2 // blue LED cooling

Missing semicolons.

LiquidCrystal_I2C lcd(0x27F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

Too many arguments, I think (!). And POSITIVE is not defined.

{    // Useless brace
  void setup()  // Missing brace

{    // Useless brace
  void loop()  // Missing brace

And some code that's not in a function:

{
 sensors.requestTemperatures();
 float temperature = sensors.getTempCByIndex(0);
 lcd.setCursor(0, 2);
 lcd.print("      ");
 lcd.print(sensors.getTempCByIndex(0));
 lcd.print("\337C");
}

if (temperature < 25) ////// Had a bogus ';' here
{
 digitalWrite(red, HIGH);

etc...

I fixed your code so that it compiles. I fixed the parts that Erik_Baas pointed out and others. See the comments in the code.

I think that you are using code from one LiquidCrystal library and including a different one. There are several libraries called LiquidCrystalI2C and they are different. The best current library for the hd44780 controller LCDs is the hd44780 library. For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.

To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.

The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.

In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.

Your code:

#include <Wire.h>
#include <DallasTemperature.h>
//#include <LiquidCrystal_I2C.h>  // wrong library for the code
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header


#define RELAY1  4  // Relay heating
#define RELAY2  6  // Relay cooling


int red = 8; //  ********* missing ; // red LED heating
int blue = 2; //  ********* missing ; // blue LED cooling


#define BACKLIGHT_PIN 13
#define ONE_WIRE_BUS 7

//LiquidCrystal_I2C lcd(0x27F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// {   ********* in wrong place

void setup()
{ // ********* added   
   Serial.begin(9600);
   sensors.begin();
   lcd.begin(20, 4);
   lcd.backlight();

   pinMode(red, OUTPUT);
   pinMode(blue, OUTPUT);
   pinMode(RELAY1, OUTPUT);
   pinMode(RELAY2, OUTPUT);
}

// {   ********* wrong place
void loop()
{   //  ********* added
   lcd.setCursor(0, 0);
   lcd.print(" TEMP CONTROL");

   // }  ********* wrong place

   // {   ********* wrong place
   sensors.requestTemperatures();
   float temperature = sensors.getTempCByIndex(0);
   lcd.setCursor(0, 2);
   lcd.print("      ");
   lcd.print(sensors.getTempCByIndex(0));
   lcd.print("\337C");
   //}  ********* wrong place


   if (temperature < 25) ////// Had a bogus ';' here
   {
      digitalWrite(red, HIGH);
      digitalWrite(blue, LOW);
      digitalWrite(RELAY1, 0);
      digitalWrite(RELAY2, 1);

      lcd.setCursor(0, 3);
      lcd.print("      Heating");

   }
   
   else if (temperature > 25) ////// Had a bogus ';' here
   {
      digitalWrite(RELAY1, 1);
      digitalWrite(RELAY2, 0);
      digitalWrite(red, LOW);
      digitalWrite(blue, HIGH);

      lcd.setCursor(0, 4);
      lcd.print("      Cooling ");
      lcd.setCursor(0, 3);
   }
} //  ********* added

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