Test of a code correction

#include <DHT.h>

#define DHTPIN A0
#define MOTORPIN 9
#define MIN_TEMP 20
#define MAX_TEMP 30
#define MIN_SPEED 0
#define MAX_SPEED 255

DHT dht(DHTPIN, DHT11);
void setup() {
pinMode(MOTORPIN, OUTPUT);
Serial.begin(9600);
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
int temp = map(temperature, 0, 1023, 0, 255);
if (temp >= MIN_TEMP && temp <= MAX_TEMP) {
int speed = map(temp, MIN_TEMP, MAX_TEMP, MIN_SPEED, MAX_SPEED);
analogWrite(MOTORPIN, speed);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("C, Motor Speed: ");
Serial.println(speed);
} else {
analogWrite(MOTORPIN, 0);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("C, Motor Speed: OFF");
}
delay(1000);
}

Getting stray '\302' in program when trying to compile. To no avail, I pasted the code in the code area as some previous posts have indicated I should. Any ideas?

  • When you see this, copy the code and paste it int a .txt file then save.

  • Open this text file, copy the code agin and paste it into a new .ino file.

1 Like

Copy it back out, paste it over your existing sketch and compile.

1 Like

That worked! Thanks very much. Can you tell me why the monitor would be reporting the temp and fan speed incorrectly, as such:

'18:13:50.253 -> Temperature: nanC, Motor Speed: -23592
18:13:51.271 -> Temperature: nanC, Motor Speed: -23592
18:13:52.320 -> Temperature: nanC, Motor Speed: -23592'

I did a schematic in TinkerCAD that looks like this, and have it wired exactly the same on a breadboard:

The fan spins at the same rate continuously, at present.

  • Print the value of temp
temp = map(temperature, 0, 1023, 0, 255);
Serial.println(temp);

What do you get ?

  • What is the range of temperature.

  • temperature is ’type’ float :thinking:

  • Never power motors from your Arduino !
    Use an external power supply.

1 Like

Your sketch uses a DHT11, but your wiring diagram appears to show a TMP36 or something similar.

The Elegoo kit it came out of says it's a DHT11 Temperature and Humidity Module. The assignment I'm working on is this:

I'm always so reassured when the diagram shows one thing and the sketch shows another... I get that warm fuzzy feeling sinking sensation in my stomach.

I do hope you didn't take that hookup diagram as gospel. Blowing the dust off a DHT11 module I have here, Gnd and Vcc are reversed to that diagram.

Hooked up as I showed it, the DHT11 will short out because the diagram is wrong?

On the one I looked at here, Vcc and Gnd were reversed from that diagram. Perhaps yours isn't. Perhaps it is. If I were you, I'd check it.

Topic moved !! Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Thanks for using code tags in your first post :+1:

Just make triple sure. Recently some largish amount of time was wasted and it turned out the inquirer was using a transistor instead of a DS18b20.

I don't know why they call a single component in a TO-92 package a module. And that one part can't be doing temperature and humidity.

a7