Code error need help

Hi i know a little bit of code i wrote some of this i copied some form the internet here is my code i get some errors that i cant fix
Than you!!``Preformatted text`

[code]
// Temp/Humidity Sensor
#include "DHT.h"

// LCD
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>

// Temp/Humidity Sensor
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

// LCD
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// Relay
#define RELAYPIN 10

// setup the Tempt/Humidity Sensor
dht.begin();

// Setup relay
pinMode(RELAYPIN, OUTPUT);
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  // Read temperature as Fahrenheit
  float temp = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
   }

  if (temp < 100) {
    digitalWrite(RELAYPIN, HIGH);
   } else if (temp > 101) {
    digitalWrite(RELAYPIN, LOW);
  }
#include <Servo.h>

  SERVO SERVO ;

  int degrees ;
  boolean button_state_1 ;

  void setup () {
    SERVO . attach ( 9 );

    pinMode ( 2 , INPUT );
  }

  if ( millis () / 1000 < 1555200 ) {
    for ( degrees = 90 ; degrees <= 135 ; degrees += 9 ) {
      SERVO . write ( degrees );
      delay ( 2000 );
    }
    delay ( 8000 ); for ( degrees = 135 ; degrees >= 90 ; degrees -= 9


                        ) {
      SERVO . write ( degrees );
      delay ( 2000 );
    } for ( degrees = 90 ; degrees >= 45 ; degrees -= 9 ) {
      SERVO . write ( degrees );
      delay ( 2000 );
    }
    delay ( 8000 ); for ( degrees = 45 ; degrees <=



                          90 ; degrees += 9 ) {
      SERVO . write ( degrees );
      delay ( 2000 );
    }


  } else {
    SERVO . write ( 90 );

  }
  button_state_1 = digitalRead ( 2 ); if ( button_state_1 == HIGH ) {

  }

}

lcd.setCursor(0, 0);

[/code]
````Preformatted text`

THIS IS THE ERROR MESSAGE
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Uno"

sketch_mar02b:21:3: error: 'dht' does not name a type

dht.begin();

^~~

sketch_mar02b:24:10: error: expected constructor, destructor, or type conversion before '(' token

pinMode(RELAYPIN, OUTPUT);

      ^

sketch_mar02b:25:1: error: expected declaration before '}' token

}

^

exit status 1

'dht' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.Preformatted text

That code doesn't appear to be in a function.

Where did you copy it from?

First read:

then redo your topic putting code between tags </>,

after tell what is this?

This code is garbled. Looks like there are 2 loop() functions but there are multiple issues with this code.

Please post the code as indicated in post #3.

looks like two separate programs, the second simply copied into loop() of the first.

don't see a definition for setup() in the original, dht.begin() and pineMode (RELAYPIN, ... are outside any function.

believe the SERVO class is not all CAPS

many lines of text simply concatenated together or broken in mid-statement.

I coppied a part from github and deleted the lines that used components i dont need. I know that this is not the best way but i kept getting errors on lines for components i dont need

No, this is not a way at all.
Fixing this code is pointless, try to write a new code yourself

hi
i read it before making this post that is why i said i know a little bit of code i posted all my code as well as my error message like it states on the post. As for formating i tried to format in the ide before pasting here an i also tried to format here it seems that the part of code i wrote does not get formated.
Thank you!

hi
Could you be a bit more specific as to what lines are concatenated together or broken in mid-statement. As you can see i deleted the second loop() but other than that i dont know how to fix the rest of the things that you mention

Briefly: you have deleted essential lines of code

i dont know if you can tell but the part about the servo i 'wrote' it my self i used blocky arduino since i am in the process of learning code. My original thought was to have a very early staged code with that whould be the basic concept just to see how my hardware is working and then change the code little by little until i get to the desired result

the only thing i deleted was
#include <Adafruit_MCP23017.h>
because even though i had the library on my computer i whould still get error after error

No, you have also deleted

void setup() {

and who knows what else in the first code.

And you can't join 2 codes by just pasting one under the other like you have.

here's an edited version of your code that compiles using stub structures i wrote

// Temp/Humidity Sensor
#define MyHW
#ifdef MyHW
# include "sim.hh"
#else
# include <Servo.h>
# include "DHT.h"
# include <Wire.h>
# include <Adafruit_RGBLCDShield.h>
#endif

// LCD

// Temp/Humidity Sensor
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

// LCD
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// Relay
#define RELAYPIN 10

Servo RVO ;


int degrees ;
boolean button_state_1 ;

void setup () {
    RVO.attach (9);

    dht.begin();

    pinMode ( 2 , INPUT );
    pinMode(RELAYPIN, OUTPUT);
}


void loop() {
    // Wait a few seconds between measurements.
    delay(2000);

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float humidity = dht.readHumidity();
    // Read temperature as Fahrenheit
    float temp = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(humidity) || isnan(temp)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }

    if (temp < 100) {
        digitalWrite(RELAYPIN, HIGH);
    } else if (temp > 101) {
        digitalWrite(RELAYPIN, LOW);
    }

    if ( millis ()/ 1000 < 1555200 ) {
        for ( degrees = 90 ; degrees <= 135 ; degrees += 9 ) {
            RVO . write ( degrees );
            delay ( 2000 );
        }
        delay ( 8000 );
        for ( degrees = 135 ; degrees >= 90 ; degrees -= 9) {
            RVO . write ( degrees );
            delay ( 2000 );
        }
        for ( degrees = 90 ; degrees >= 45 ; degrees -= 9 ) {
            RVO . write ( degrees );
            delay ( 2000 );
        }
        delay ( 8000 ); for ( degrees = 45 ; degrees <= 90 ; degrees += 9 ) {
            RVO . write ( degrees );
            delay ( 2000 );
        }

    } else {
        RVO . write ( 90 );

    }
    button_state_1 = digitalRead ( 2 ); if ( button_state_1 == HIGH ) {

    }

    lcd.setCursor(0,0);
    lcd.print("H: ");
    lcd.print(humidity);
    lcd.print(" % ");
    lcd.setCursor(0,1);
    lcd.print("T: ");
    lcd.print(temp);
    lcd.print(" F ");
}

hi
thank you for fixing it and correctly formatting it
My question is what library is (# include "sim.hh") and is its function?
I tried the web but came up empty handed

i should have deleted the #ifdef stuff at top that contains stub-structs for the includes i don't have to compile the code

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