Can't get sketch to compile

Hi, I found a sketch online to try to build a indoor air quality monitoring unit. When I put it in a Arduino sketch form I was unable to get it to compile. I am not very good at understanding C++ computer language so I am limited on understanding whats wrong.
I am sending a copy of the sketch. thank you for yourhelp.Use code tags to format code for the forum`

#include <TFT_HX8357.h>
#include <User_Setup.h>
#include <Arduino_DataBus.h>
#include <Arduino_G.h>
#include <Arduino_GFX.h>
#include <Arduino_GFX_Library.h>
#include <Arduino_TFT.h>
#include <Arduino_TFT_18bit.h>
#include <gfxfont.h>
#include <YCbCr2RGB.h>
#include <MQ135.h>
#include <DHT11.h>
#include <Adafruit_HX8357.h>
#include <Wire.h>
#include <SPI.h>
#include <TFT.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(6, 7, 2, 3, 4, 5);


#define ALTITUDE 896.6
#define DHTTYPE DHT11 



int MQ135 (A0);
int DHT11 (A6);
int gLed = 6;
int yLed = 7;
int rLed = 8;
const int pinPM25 = 2;
const int pinPM1 = 3;
const unsigned long sampleTime = 5000;  // mSec   -> 5..30 sec

float calc_low_ratio(float lowPulse) {
  return lowPulse / sampleTime * 100.0;  // low ratio in %
}

float calc_c_mgm3(float lowPulse) {
  float r = calc_low_ratio(lowPulse);
  float c_mgm3 = 0.00258425 * pow(r, 2) + 0.0858521 * r - 0.01345549;
  return max(0, c_mgm3);
}

float calc_c_pcs283ml(float lowPulse) {
  float r = calc_low_ratio(lowPulse);
  float c_pcs283ml =  625 * r;
  return min(c_pcs283ml, 12500);
}




void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  Serial.println("Warming up...");
  delay(60000);
}

  pinMode(gLed, OUTPUT);
  pinMode(yLed, OUTPUT);
  pinMode(rLed, OUTPUT);
  }
  
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  tft.setTextFont(1);        // Select font 1 which is the Adafruit GLCD font
  delay(100);  
  bme.begin();  
  DHT11.begin();
  delay(3000);
  }
  

void loop() {
 
  duration1 = pulseIn(pin1, LOW);
  duration2 = pulseIn(pin2, LOW);
  lowpulseoccupancy1 = lowpulseoccupancy1+duration1;
  lowpulseoccupancy2 = lowpulseoccupancy2+duration2;


  if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
  {
    ratio1 = lowpulseoccupancy1/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration1 = 1.1*pow(ratio1,3)-3.8*pow(ratio1,2)+520*ratio1+0.62; // using spec sheet curve

    ratio2 = lowpulseoccupancy2/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration2 = 1.1*pow(ratio2,3)-3.8*pow(ratio2,2)+520*ratio2+0.62; // 
 
    lcd.setCursor(0, 0);
    lcd.print("PM10 ");
    lcd.setCursor(6, 0);
    lcd.print(concentration1,3);
       
    Serial.print("concentration1 = ");
    Serial.print(concentration1);
    Serial.print(" pcs/0.01cf  -  ");

    Serial.print("concentration2 = ");
    Serial.print(concentration2);
    Serial.print(" pcs/0.01cf  -  ");

    
    if (concentration1 < 1000) {
     lcd.setCursor (0, 1);
     for (int i = 0; i < 16; ++i)
   {
     lcd.write(' ');
   }
 
     lcd.setCursor (0, 1);
     for (int i = 0; i < 16; ++i)
  {
     lcd.write(' ');
  }
      
     lcd.setCursor(4, 1);
     lcd.print("GOOD");
     
    
     digitalWrite(gLed, HIGH);
     digitalWrite(yLed, LOW);
     digitalWrite(rLed, LOW);
   
    }
    
    if (concentration1 > 10000 && concentration1 < 20000) {

    lcd.setCursor (0, 1);
    for (int i = 0; i < 16; ++i)
  {
    lcd.write(' ');
  } 
  
      
      lcd.setCursor(4, 1);
     lcd.print("ACCEPTABLE");
    
     digitalWrite(gLed, LOW);
     digitalWrite(yLed, HIGH);
     digitalWrite(rLed, LOW);
     
    }
      if (concentration1 > 20000 && concentration1 < 50000) {
     lcd.setCursor (0, 1);
     for (int i = 0; i < 16; ++i)
  {
     lcd.write(' ');
  }   
     lcd.setCursor(4, 1);
     lcd.print("HEAVY");
     
     digitalWrite(gLed, LOW);
     digitalWrite(yLed, LOW);
     digitalWrite(rLed, HIGH);
  
  }
   
    if (concentration1 > 50000 ) {
lcd.setCursor (0, 1);
for (int i = 0; i < 16; ++i)
{
  lcd.write(' ');
}
      
     lcd.setCursor(4, 1);
     lcd.print("HAZARD");
    
     digitalWrite(gLed, LOW);
     digitalWrite(yLed, LOW);
     digitalWrite(rLed, HIGH);
    
     
    } 
      
    lowpulseoccupancy1 = 0;
    lowpulseoccupancy2 = 0;
    starttime = millis();
  }
}

  

 
   printIndoorTemperature();
   printIndoorHumidity();
}


void readSensor()
{
  previousIndoorTemperature = indoorTemperature;
  previousIndoorHumidity = indoorHumidity;
  
  indoorHumidity = dht.readHumidity();
  indoorTemperature = Celcius2Fahrenheit(dht.readTemperature());
  Serial.println(indoorTemperature);
  Serial.println(indoorHumidity);
}
 
void printIndoorTemperature()
{
  String temperature;
  if(indoorTemperature != previousIndoorTemperature)
  {

    if(indoorTemperature>=100)
    {
      temperature = String(indoorTemperature,0);
    }else
    {
      temperature = String(indoorTemperature,2);
    }

void printIndoorTemperature()
{
  String temperature;
  if(indoorTemperature != previousIndoorTemperature)
  {

    if(indoorTemperature>=100)
    {
      

void printIndoorTemperature()
{
  String temperature;
  if(indoorTemperature != previousIndoorTemperature)
  {

    if(indoorTemperature>=100)
    {
      previousIndoorTemperature = indoorTemperature;
    }
  }
float Celcius2Fahrenheit(float celcius)
{
  return 1.8 * celcius + 32;
}
  
  
  
       

   




   

 

Hi, @jcann9960

Can you tell us the model Arduino you are using?
What compile errors did you get?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Your attempt to use code tags did not quite work out. [code] needs to be on it's own line.

I suggest that you use tools → auto format to indent your code properly. If you understand the result of that operation, you can see that you have some missing and/or misplaced }

I am using a older mega 2560. The error I am getting is " expected constructor, destructor, or type conversion before '{' token. It also highlights "pinMode(rLed, OUTPUT);

That's what I am saying, I don't know c++ language enough to understand what the error is.

Functions start with a { and end with a }. After auto format

  1. Functions definitions need to start at the beginning of a line. Example where it is wrong:
    void printIndoorTemperature()

You're missing two } before that.
2. Statements should not start at the beginning of a line. Example where it is wrong:

}

pinMode(gLed, OUTPUT);
pinMode(yLed, OUTPUT);
pinMode(rLed, OUTPUT);

Remove that }, auto format again and go to the next one

And lastly you are missing a number of } at the end of the code (last function).

PS
I did fix your code tags earlier for you.

Thank you for the fix. I didn't understand what you meant around the statement
"Void printIndoorTemperature()"

Do you understand it now?

Hi, @jcann9960

Where and can you post a link to the code?

Tom.... :smiley: :+1: :coffee: :australia:

Note here that case matters in the C/C++ language. So this line should be

void printIndoorTemperature() {
// now the stuff in that function, and end the function definition with a:-
}

Hi, that's the way I had "void" in my sketch. I didn't notice it when I posted it in my question. These "curly braces" are the most confusing part of this c++ language, if you have them in the wrong place it messes up the whole sketch.

Yes it will. But it is a simple rule.
They must be matched. That is for every { that starts a function there must be a matching } to end that function.

You can also use { and } within a function, for example around an if statement to group things together. These also must be matched. If not the compiler can make no sense as to what you are trying to do.

You think this is complex? Take Python which has the exactly the same concept but it takes the form of an indentation of one or more spaces. Try working with that, it is even easier to make mistakes and you can't even see them.

Thanks! In my sketch can you see if any of my braces are in the wrong places? Thanks so much for your help.

Yes. And I have showed you how to approach it in post #6.

Thanks Tom, I still can't get the sketch to compile. Let me see if I can figure it out. At least I know the braces are in the right places.

Show your updated code in a new reply.

Hint.
To find the closing brace of any opening brace just click your cursor on the opening brace and the corresponding closing brace will be highlighted by having a square aground it.

This is a screen dump from your original code, unfortunate the cursor doesn't show on this dump but it was clicked on the opening brace of the first line of the setup function.

Notice that the matching brace is on line 61. This leaves all the code after this line orphaned, and the closing brace on line 66 useless. When the compiler sees this closing brace on line 66 it doesn't know what to do with it and generates an error.

You have situations like that all over your code, you have to remove them all before you can get anything to compile.

Thanks, I will give this a try. I Didn't know this.

I know Python much better than C, and yet I still make those mistakes sometimes :rofl:
At least it is much easier to tell what the problem is.
I do see that printIndoorTemperature() is missing a closing bracket: