Keep getting this error message on wokwi simulator

Im using the Wokwi arduino simulator and was trying to join a 'char' variable and a 'float variable using sprintf() and strcat() but i keep getting this error message

sketch.ino:32:10: error: expected constructor, destructor, or type conversion before '(' token
sprintf(t_str, "%6.2f", t); // Format temperature value as a string with 2 decimal places
^
sketch.ino:33:10: error: expected constructor, destructor, or type conversion before '(' token
sprintf(h_str, "%6.2f", h); // Format humidity value as a string with 2 decimal places
^
sketch.ino:34:9: error: expected constructor, destructor, or type conversion before '(' token
strcat(Temp, t_str); // Concatenate the temperature string to the "Temp: " message
^
sketch.ino:35:9: error: expected constructor, destructor, or type conversion before '(' token
strcat(H, h_str);
^

here is my code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <stdio.h>

#define DHTPIN 8
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int buttonPin = 3;
const byte ldrPin = A0;

char printLL[12] = "Light Level";  // creates a character for display "Light Level" on the lcd screen
char LDR_Readings[16];             // create a character array to hold the message that differs depending on the output of the LDR

unsigned long previousMillis = 0;
const long interval = 3000;
unsigned long clearMillis = 0;

int delayTime = 25;


    float f = dht.readTemperature(true);
    float h = dht.readHumidity();
    float t = dht.readTemperature();
  char Temp[16] = "Temp: ";
  char H[16] = "Humi: ";
  char t_str[8];
  char h_str[8];
  sprintf(t_str, "%6.2f", t);  // Format temperature value as a string with 2 decimal places
  sprintf(h_str, "%6.2f", h);  // Format humidity value as a string with 2 decimal places
  strcat(Temp, t_str);  // Concatenate the temperature string to the "Temp: " message
  strcat(H, h_str);

void setup() {
  lcd.init();
  lcd.backlight();
  dht.begin();
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  pinMode(ldrPin, INPUT_PULLUP);
}

void displayLDR(LiquidCrystal_I2C lcd, char* LDR_Readings, int delayTime) {
  int len = strlen(LDR_Readings);
  for (byte i = 0; i < len; i++) {
    lcd.setCursor(i + 2, 1);
    delay(25);
    lcd.print(LDR_Readings[i]);
  }
}

void LightLevel(LiquidCrystal_I2C lcd, char* printLL, int delayTime) {
  int len = strlen(printLL);
  for (byte i = 0; i < len; i++) {
    lcd.setCursor(i + 2, 0);
    delay(25);
    lcd.print(printLL[i]);
  }
}


void TempHumi(LiquidCrystal_I2C lcd, char* Temp, char* H,  int delayTime) {
  int len = strlen(Temp);

  for (byte i = 0; i < len; i++) {
        delay(25);
    lcd.setCursor(i,0);
    lcd.print(Temp[i]);
    lcd.setCursor(i,1);
    lcd.print(H[i]);
  }
}



/*
The reason we need to put everything in the function parameters is so that we can pass in the required 
values when calling the function. This is a common programming technique called "passing arguments".

The LiquidCrystal_I2C lcd parameter is used to pass in the LCD object that you want to display the
message on. This allows the function to work with any LCD object, as long as it is of the 
LiquidCrystal_I2C type.

The char* message parameter is used to pass in the message that you want to display on the LCD.
The char* type is a pointer to an array of characters, which is commonly used to represent 
strings in C/C++.

The int delayTime parameter is used to pass in the delay time between each character display.
This allows the delay time to be easily adjusted without having to modify the function code.

By defining the function with these parameters, it becomes a reusable block of code that can 
be called from other parts of your program with different LCD objects, messages, and delay times.
*/


// displayMessage(lcd, message, delayTime); !!!!!!!

/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
displayMessage(lcd, message, delayTime) is a function call that invokes the displayMessage function
we defined earlier.

The first parameter lcd is the LCD object that we want to use for displaying the message. 
It must be of type LiquidCrystal_I2C as that is the type defined in the function parameter.

The second parameter message is the message that we want to display on the LCD. It is a 
character array (or string) that we defined earlier in the code.

The third parameter delayTime is the amount of delay we want to have between each 
character being displayed on the LCD. It is an integer value that we defined earlier in the code.

When this function is called with these arguments, it will execute the code inside the function, 
which will display the message character by character on the specified LCD with the specified delay time.
*/

void loop() {

  /* LDR SENSOR */
  unsigned int AnalogValue;

  AnalogValue = analogRead(A0);

  strcpy(printLL, "Light Level");
  Serial.println(AnalogValue);

  lcd.setCursor(3, 1);

  // determining the message output for the ldr sensor readings.
  if (AnalogValue < 10) {
    strcpy(LDR_Readings, " - Dark - ");  // copy the message into the array
  } else if (AnalogValue < 200) {
    strcpy(LDR_Readings, "  - Dim - ");
  } else if (AnalogValue < 500) {
    strcpy(LDR_Readings, " - Light - ");
  } else if (AnalogValue < 800) {
    strcpy(LDR_Readings, " - Bright - ");
  } else {
    strcpy(LDR_Readings, " - Bright + - ");
  }

  /*
These lines are the modified if statements that set the appropriate message based on the AnalogValue reading.

If the AnalogValue is less than 10, the message array is assigned the string " - Dark - ".
 If the AnalogValue is between 10 and 200, the message array is assigned the string " - Dim - ", and so on.

Note that the strcpy function is used to copy the appropriate message into the message array.
 This function copies the contents of the second argument (the string) into the first argument (the message array).
*/

  unsigned long currentMillis = millis();

  // check if it's time to update the LED
  if (currentMillis - previousMillis >= interval - 2000) {
    // save the last time you updated the LED
    previousMillis = currentMillis;


    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
    }

    float hif = dht.computeHeatIndex(f, h);
    float hic = dht.computeHeatIndex(t, h, false);

    char Temp[16] = "Temp: ";



    Serial.print(F("Humidity: "));
    Serial.print(h);
    Serial.print(F("%  Temperature: "));
    Serial.print(t);
    Serial.print(F("°C "));
    Serial.print(f);
    Serial.print(F("°F  Heat index: "));
    Serial.print(hic);
    Serial.print(F("°C "));
    Serial.print(hif);
    Serial.println(F("°F"));

    /*lcd.setCursor(0, 0);
    lcd.print(" ");
    lcd.setCursor(0, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(1, 0);
    lcd.print(" ");
    lcd.setCursor(0, 0);
    lcd.print("H");
    lcd.setCursor(1, 1);
    lcd.print(" ");
    lcd.setCursor(0, 1);
    lcd.print("T");
    delay(25);
    lcd.setCursor(2, 0);
    lcd.print(" ");
    lcd.setCursor(1, 0);
    lcd.print("u");
    lcd.setCursor(2, 1);
    lcd.print(" ");
    lcd.setCursor(1, 1);
    lcd.print("e");
    delay(25);
    lcd.setCursor(3, 0);
    lcd.print(" ");
    lcd.setCursor(2, 0);
    lcd.print("m");
    lcd.setCursor(3, 1);
    lcd.print(" ");
    lcd.setCursor(2, 1);
    lcd.print("m");
    delay(25);
    lcd.setCursor(4, 0);
    lcd.print(" ");
    lcd.setCursor(3, 0);
    lcd.print("i");
    lcd.setCursor(4, 1);
    lcd.print(" ");
    lcd.setCursor(3, 1);
    lcd.print("p");
    delay(25);
    lcd.setCursor(5, 0);
    lcd.print(" ");
    lcd.setCursor(4, 0);
    lcd.print(":");
    lcd.setCursor(5, 1);
    lcd.print(" ");
    lcd.setCursor(4, 1);
    lcd.print(":");
    delay(25);
    lcd.setCursor(6, 0);
    lcd.print(" ");
    lcd.setCursor(5, 0);
    lcd.print(" ");
    lcd.setCursor(6, 1);
    lcd.print(" ");
    lcd.setCursor(5, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(8, 0);
    lcd.print(" ");
    lcd.setCursor(8, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(9, 0);
    lcd.print(" ");
    lcd.setCursor(9, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(7, 0);
    lcd.print(" ");
    lcd.setCursor(6, 0);
    lcd.print(h);
    lcd.setCursor(7, 1);
    lcd.print(" ");
    lcd.setCursor(6, 1);
    lcd.print(t);
    delay(25);
    lcd.setCursor(10, 0);
    lcd.print(" ");
    lcd.setCursor(10, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(11, 0);
    lcd.print(" ");
    lcd.setCursor(10, 0);
    lcd.print("%");
    lcd.setCursor(11, 1);
    lcd.print(" ");
    lcd.setCursor(10, 1);
    lcd.print("C");
    delay(25);
    lcd.setCursor(11, 1);
    lcd.print((char)223);
    lcd.setCursor(12, 0);
    lcd.print(" ");
    lcd.setCursor(12, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(13, 0);
    lcd.print(" ");
    lcd.setCursor(13, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(14, 0);
    lcd.print(" ");
    lcd.setCursor(14, 1);
    lcd.print(" ");
    delay(25);
    lcd.setCursor(15, 0);
    lcd.print(" ");
    lcd.setCursor(15, 1);
    lcd.print(" ");
*/
  TempHumi(lcd, Temp, H, delayTime);

    while (true) {
      // clear the LCD if it's time
      if (millis() - currentMillis >= interval) {
        currentMillis = millis();
        for (byte o = 0; o < 2; o++) {
          lcd.setCursor(o, 0);
          lcd.print(" ");
          delay(25);
        }
        LightLevel(lcd, printLL, delayTime);

        break;
      }
    }


    // see below to find more about what LightLevel(); does.

    /*
char message[] = " - Dark - ";
int len = strlen(message);
for (byte i = 0; i < len; i++) {
lcd.setCursor(i+2, 1);
delay(25);
lcd.print(message[i]);
}

The first line declares a character array named "message" and initializes it with the string "- Dark -". 
The string is enclosed in double quotes, which is how strings are represented in C++.

The second line declares an integer variable named "len" and assigns it the length of the "message" string using the strlen() function. 
This function calculates the length of a string by counting the number of characters in it, excluding the null character at the end ('\0').

The code then enters a for loop that will iterate over each character in the "message" string. 
The loop uses a byte variable "i" as the loop counter, starting from 0 and ending when "i" is no longer less than "len".

Inside the loop, the code sets the cursor position of the LCD display to i+2 on the second row (1), 
using the lcd.setCursor() function. The "+2" is added to position the text in the middle of the display, 
and the second parameter is the row number (0 for the first row and 1 for the second row).

The code then introduces a delay of 25 milliseconds using the delay() function. 
This is to create a pause between each character being printed to the LCD display.

The last line of the loop prints the current character of the "message" string to the LCD display using the lcd.print() function. 
The "i" variable is used as the index to access the current character in the "message" string, and that character is then sent to the LCD display.

The loop repeats until all the characters in the "message" string have been printed to the LCD display.

The i++ expression is a shorthand way of writing i = i + 1. It's called the increment operator,
 and it increments the value of the variable i by 1 each time the loop iterates. In this specific code, 
 i is used as the index to access each character of the message string inside the loop.
    */



    while (true)  // will rerun this function until the if statement is completed, then break; will end it {
      if (millis() - currentMillis >= interval - 2999) {
        currentMillis = millis();
        lcd.setCursor(0, 1);
        lcd.print(" ");
        lcd.print(" ");
        lcd.setCursor(3, 1);
        if (AnalogValue < 10) {
          displayLDR(lcd, LDR_Readings, delayTime);
        } else if (AnalogValue < 200) {
          displayLDR(lcd, LDR_Readings, delayTime);
        } else if (AnalogValue < 500) {
          displayLDR(lcd, LDR_Readings, delayTime);
        } else if (AnalogValue < 800) {
          displayLDR(lcd, LDR_Readings, delayTime);
        } else {
          displayLDR(lcd, LDR_Readings, delayTime);
        }

        break;
      }
  }

  /*
using the function displayLDR(...); it allows us to easily print a function into the if statements 
(see above for info on displayLDR() )
*/

  while (true) {
    if (millis() - currentMillis >= interval) {
      currentMillis = millis();

      break;
    }
  }
}

You can’t have code outside functions

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