Having an error while executing the code

Hello Everyone !!
While I am new to learning Arduino, this code below is a part of my work. My work is to try to execute the code and while trying I am receiving the error -" the function definition is not allowed here before '{' token". I don't know whether this kind of error has been dealt with before. Kindly provide me any help and also if possible some suggestions on the best way to learn Arduino FYI: I am new to Arduino. I am looking forward to hearing from you all. The code is below:

Thank you

#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(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{ %%%%% here the error is coming

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);
{
digitalWrite(red,HIGH);
digitalWrite(blue,LOW);

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

digitalWrite(RELAY1,0);
digitalWrite(RELAY2,1);
}
else if(temperature > 25);
{
digitalWrite(RELAY1,1);
digitalWrite(RELAY2,0);
digitalWrite(red,LOW);
digitalWrite(blue,HIGH);

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

}

a function-definition is not allowed here before '{' token"
a function-definition is not allowed here before '{' token"
"

Please have a look at How to get the best out of this forum and alter your post accordingly. At the moment it's just impossible to read.

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

Post the full error message copied from the IDE with the convenient button

Is the start of your loop() function missing something vital ?

#include <Wire.h>[color=#222222][/color]
#include <DallasTemperature.h>[color=#222222][/color]
#include <LiquidCrystal_I2C.h>[color=#222222][/color]
#define RELAY1  4  // Relay heating[color=#222222][/color]
#define RELAY2  6  // Relay cooling[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
int red = 8; // red LED heating[color=#222222][/color]
int blue = 2; // blue LED cooling[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
#define BACKLIGHT_PIN 13[color=#222222][/color]
#define ONE_WIRE_BUS 7[color=#222222][/color]
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); [color=#222222][/color]
OneWire oneWire(ONE_WIRE_BUS);[color=#222222][/color]
DallasTemperature sensors(&oneWire);[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
void setup() [color=#222222][/color]
{  [color=#222222][/color]
 [color=#222222][/color]
  Serial.begin(9600);[color=#222222][/color]
  sensors.begin();[color=#222222][/color]
  lcd.begin(20,4);[color=#222222][/color]
  lcd.backlight();[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
  pinMode(red, OUTPUT);[color=#222222][/color]
  pinMode(blue, OUTPUT);[color=#222222][/color]
  pinMode(RELAY1, OUTPUT);[color=#222222][/color]
  pinMode(RELAY2, OUTPUT);[color=#222222][/color]
  [color=#222222][/color]
  }[color=#222222][/color]
 [color=#222222][/color]
 [color=#222222][/color]
 void loop()[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
  lcd.setCursor(0, 0);[color=#222222][/color]
  lcd.print("    TEMP CONTROL");[color=#222222][/color]
  [color=#222222][/color]
  sensors.requestTemperatures();[color=#222222][/color]
  float temperature = sensors.getTempCByIndex(0);[color=#222222][/color]
  lcd.setCursor(0, 2);[color=#222222][/color]
  lcd.print("      ");[color=#222222][/color]
  lcd.print(sensors.getTempCByIndex(0));[color=#222222][/color]
  lcd.print("\337C");[color=#222222][/color]
  [color=#222222][/color]
    if(temperature < 25);[color=#222222][/color]
    {[color=#222222][/color]
      digitalWrite(red,HIGH);[color=#222222][/color]
      digitalWrite(blue,LOW);[color=#222222][/color]
 [color=#222222][/color]
      lcd.setCursor(0, 3);[color=#222222][/color]
      lcd.print("      Heating");[color=#222222][/color]
 [color=#222222][/color]
      digitalWrite(RELAY1,0); [color=#222222][/color]
      digitalWrite(RELAY2,1);   [color=#222222][/color]
           }[color=#222222][/color]
    else if(temperature > 25);[color=#222222][/color]
    {[color=#222222][/color]
     digitalWrite(RELAY1,1);[color=#222222][/color]
     digitalWrite(RELAY2,0); [color=#222222][/color]
     digitalWrite(red,LOW);[color=#222222][/color]
     digitalWrite(blue,HIGH);[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
 lcd.setCursor(0, 4);[color=#222222][/color]
      lcd.print("      Cooling ");[color=#222222][/color]
      lcd.setCursor(0, 3);[color=#222222][/color]
      [color=#222222][/color]
}  [color=#222222][/color]
 [color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
[color=#ffffff]
[/color]
[color=#222222][/color]

I apologize for the inconvenience, here is the code above: It is a code for a sensor measuring temperature:
I am receiving the error : he function definition is not allowed here before '{' token"

Thank you for using code tags, but the forum software has added tags to it due to a bug, which is not your fault

If you have got the WYSIWYG editor option turned on in your forum profile then please turn it off and post the code again

“ . I don't know whether this kind of error has been dealt with before. ”

Oh, yes, most assuredly. It may help you to know that your code has not executed and issued this error, rather it has failed to compile, a step performed on the big machine you are using before the result is uploaded or downloaded, transferred in any case to the Arduino.

It is a syntax error, fancy talk for not having the basic grammar of a computer program exactly correct. It is suggested that you find a learning source that matches your style and get a handle on the structure of an Arduino program… where the braces, brackets, parentheses, colons, semicolons and keywords are allowed to appear and in what combinations.

Basic C/C++ programming, which I will always recommend you get familiar with using only the big machine. There are online interpreters that make experimenting with little steps easy, painless and without the added challenge of down (or is it up?) loading to an attached microprocessor, in this case your Arduino.

a7

I don't get an error there.

You are missing the '{' after "void loop()" and missing the '}' at the end of that function.

You have two 'if' statements with an extra ';' at the end of the line.

Everything else compiles OK.

#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(0x3F, 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()
{////// Was missing
  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);


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


    digitalWrite(RELAY1, 0);
    digitalWrite(RELAY2, 1);
  }
  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);
  }
}////// Was missing

ketankarkare:
While I am new to learning Arduino, this code below is a part of my work. My work is to try to execute the code and while trying I am receiving the error -" the function definition is not allowed here before '{' token".

It's a small point but you are at the stage where your program is being compiled, not being executed (or run). All programs must compile without errors before they will be uploaded to the Arduino and executed.

...R

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