Hi I'm trying to get the DHT 11 sensor to work and wrote my own code but it failed so ive searched the internet and found a code sample so I can learn from it. When I go to compile the code I keep getting this error message Arduino: 1.8.6 (Windows 10), Board: "Arduino/Genuino Uno"
In file included from C:\Users\Rodney\AppData\Local\Temp\arduino_modified_sketch_859970\sketch_sep16a.ino:1:0:
C:\Users\Rodney\Documents\Arduino\libraries\Grove_Temperature_And_Humidity_Sensor/DHT.h:34:15: error: expected initializer before numeric constant
#define DHT11 11
^
C:\Users\Rodney\AppData\Local\Temp\arduino_modified_sketch_859970\sketch_sep16a.ino:3:13: note: in expansion of macro 'DHT11'
int DHTTYPE DHT11
^
C:\Users\Rodney\AppData\Local\Temp\arduino_modified_sketch_859970\sketch_sep16a.ino: In function 'void loop()':
sketch_sep16a:16:4: error: expected unqualified-id before '.' token
DHT.readHumidity = hum ;
^
sketch_sep16a:17:9: error: expected primary-expression before '.' token
temp=DHT.readTemperature()
^
exit status 1
expected unqualified-id before '.' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
as you can see I have tried writing it differently and added semi colons and removed semi colons but it still gives the same error .
here is the full code which since posting I have managed to get rid of original error message but now refuses to compile for uno
#include <DHT.h>
int DHTPIN = 8;
int DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
float hum;
float temp;
void setup() {
Serial.begin (9600);
int DHT,begin;
// put your setup code here, to run once:
}
void loop() {
int readHumidity = hum ;
int readTemperature = temp;
Serial.print ("humidity:");
Serial.print (hum);
Serial.print ("%,temp:");
Serial.print (temp);
delay (1000);
// put your main code here, to run repeatedly:
}
What is missing in this statement: int DHTTYPE DHT11?
There are two 'language elements' that are missing here. If you think that DHTTYPE is an identifier (variable), you should use a ______ operator to assign the value DHT11 to it and then terminate the statement using a ___.
Post#5 has detected your mistakes, and Post#6 has given you hints for correcting these mistakes. You are not lost; the Forum is here to bring you up, proceed, and progress.
If you are asked to write a single line C-code to assign a value (say, 0x2345) into a 16-bit variable named x, what line would you write? Please, show it in reply.
Hints: The variable x must be preceded by a keyword called 'data type' which is byte if the variable is to hold 8-bit data (range: 0x00 - 0xFF) or int if the variable is to hold 16-bit data (range: 0x0000 - 0xFFFF). Here, 0x has been used to mean that the data that follows it has the hexadecimal (16) base. The intended value is stored in the variable (x) using = operator (called assignment operator). The C-line is always ended (terminated) by a ; (semicolon). The items: byte int = 0x234 ; are called language elements/tokens/constructs.
Ripcrow:
Ok I’m lost. The code is copied and I’ve never coded before and never been shown or had a education. So what is missing.
Copy / Paste programming is likely to make you very frustrated and get you nowhere. You may need to take a step back from your project and spend some time learning basic coding techniques and the C / C++ language. The Arduino IDE contains dozens of example programs to learn from -- starting from very simple concepts and moving forward.
made some adjustments but now refuses to compile . I understand copy and paste programming is not the best way but usually if I can see how its written I can understand how to write my own and understand how it works. Googling these error codes does not really give a decent answer either . Here is the updated code
[code]
#include <DHT.h>
int DHTPIN = 8 ;
int (DHTTYPE DHT11);
DHT dht(DHTPIN,DHT11);
float hum ;
float temp;
void setup() {
Serial.begin (9600);
byte DHT, read ;
// put your setup code here, to run once:
}
void loop() {
int readHumidity = hum;
int readTemperature = temp;
int hum (0);
Serial.print ("humidity:");
Serial.print (hum);
Serial.print ("%,temp:");
Serial.print (temp);
delay (2000);
// put your main code here, to run repeatedly:
}
GolamMostafa pointed you to this post, which explains how to set up the device.
Scroll down to the bottom of the page he pointed you to. (just open this link in a new window)
There is a code box.
His code works.
Compare the first four lines of your code, to the first four lines of his code.
Those first four lines are what have been causing the majority of your problems.
DHT_11:12:6: error: expected primary-expression before '.' token
dht.begin();
^
C:\Users\Rodney\AppData\Local\Temp\arduino_modified_sketch_821257\DHT_11.ino: In function 'void loop()':
DHT_11:21:16: error: expected primary-expression before '.' token
int chk = DHT.read11(DHT11_PIN);
^
DHT_11:21:24: error: 'DHT11_PIN' was not declared in this scope
int chk = DHT.read11(DHT11_PIN);
^
DHT_11:23:21: error: expected primary-expression before '.' token
Serial.println(DHT.temperature);
^
DHT_11:25:22: error: expected primary-expression before '.' token
Serial.println (DHT.humidity);
^
Using library Adafruit_Unified_Sensor-1.0.2 at version 1.0.2 in folder: C:\Users\Rodney\Documents\Arduino\libraries\Adafruit_Unified_Sensor-1.0.2
Using library Grove_Temperature_And_Humidity_Sensor at version 1.0.0 in folder: C:\Users\Rodney\Documents\Arduino\libraries\Grove_Temperature_And_Humidity_Sensor
exit status 1
expected primary-expression before '.' tokenve tried a new code for the DHT 11 sensor. now I keep getting an expected primary expression error for a serial.println bit of code.
I stole that code and it still says there are errors in that code. Error I get is stray \240 in code. beginning to think I got dodgy sensors or computer. Compiler says one line of code is wrong and yet its the same as previous lines and no code works,
Ripcrow:
I stole that code and it still says there are errors in that code. Error I get is stray \240 in code. beginning to think I got dodgy sensors or computer. Compiler says one line of code is wrong and yet its the same as previous lines and no code works,
Hey Ripcrow.
Don't double post.
While dodgy sensors or computer might be a (slim) possibility, more like it's malevolent forces at work and an evil spirit has infected your computer.
or...
You've made changes to your code, but there are still errors.