I'm obviously new. Tinkering around but have no actual experience with this.
I want pin 2 (relay) high when the temp sensor is between 1 and 55 degrees, and pin 3 (heater) high when the temp is below 5. I finally got it to compile, but when I ice the sensor heater never comes on, and when I raise the temp of the sensor it got over 65 and pin2 never dropped out.
I started by editing an example code from adafruit so I don't REALLY understand the serial portion of the code. the bit at the end is mine.
Help?
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
Adafruit_Sensor *aht_humidity, *aht_temp;
const int Relay = A2;
const int Heater = A3;
int val = 0;
void setup(void) {
pinMode(Relay,OUTPUT);
pinMode(Heater,OUTPUT);
Serial.begin(9600);
while (!Serial)
delay(10);
Serial.println("Adafruit AHT10/AHT20 test!");
if (!aht.begin()) {
Serial.println("Failed to find AHT10/AHT20 chip");
while (1) {
delay(10);
}
}
Serial.println("AHT10/AHT20 Found!");
aht_temp = aht.getTemperatureSensor();
aht_temp->printSensorDetails();
aht_humidity = aht.getHumiditySensor();
aht_humidity->printSensorDetails();
}
void loop() {
sensors_event_t humidity;
sensors_event_t temp;
aht_humidity->getEvent(&humidity);
aht_temp->getEvent(&temp);
Serial.print("\t\tTemperature ");
Serial.print(temp.temperature);
Serial.println(" deg C");
delay(100);
Serial.print(temp.temperature);
Serial.print(",");
Serial.print(humidity.relative_humidity);
Serial.println();
delay(10);
val=aht_temp;
if (val<=5.0)
{
digitalWrite(Heater, HIGH);
}
else if (val>5)
{
digitalWrite(Heater, LOW);
}
if (1.00<=val<=55)
{
digitalWrite(Relay, HIGH);
}
else if (val<1.00)
{
digitalWrite(Relay, LOW);
}
else if (val>55)
{
digitalWrite(Relay, LOW);
}
}
Add comments in your code, you'll be thankful later.
Perhaps not in short code but others.
Probably a good idea to show the original code also to save others having to find and decipher and show the changes you made. "the bit at the end" doesn't mean anything.
Add some serial prints to see where you are going wrong.
Avoid this. While it is sintactically correct (meaning that the compiler does not consider it to be an error), it is not what you think. It is always true.
(1.00<=val<=55)
is equivalent to ((1.00<=val) < =55)
The expression in the inside parenthesis yields either 0 or 1, so the original expression is equivalent to (0 < =55) || (1 < =55)
which are both true. If you want to learn about this matter look for a c++ tutorial.
I added the pin definitions in the global function, the pinMode statements at the start of the loop function, and everything from "val=aht_temp" onwards.
Everything else is about how the sensor communicates with the controller, I didn't change any of that.
For some reason, although serial monitor continuously scrolls the temperature from the sensor, my if/else commands aren't toggling the outputs like they should based on that temperature. I put in a simple blink script to make sure the leds were wired properly, and it worked as expected so it has to be something in the code.
That's what I tried after your first comment and then the relay was stuck low instead of high.
Do you see anything wrong with the way I'm pointing the value to the temperature? I tried pasting every combination of the variables related to temp from the original code, and "aht_temp" was the only one I found that would compile. But that doesn't mean it's correct, just proper syntax. I thought making val a float and making it equal to temp.temperature would work, but that errored out.
I already tried that!!! but since you suggested it, i tried again and THIS time it worked!
The other times, it gave me an error saying it couldn't convert from an int to an int. I couldn't find anything on google explaining what that error even means so I moved on. But now since YOU said it, it works perfect! Thank you!
For logic testing purposes, the heater output was driving an LED in this code, but in reality is driving 4x 20W 12V heater pads. They like to draw WAY more current than they should so I have to keep my bench power supply current limiter dialed in. My next refinement is going to be moving the heater output to a PWM pin to use that as a current limiter, since it's a pure resistive load. Throttling it from 12v to about 7v should keep me in the ballpark.
everything in the base code that I started with is in int. but the displayed values are to two decimal places, that's why I tried to use a float. I was going to find the precise celsius equivalents to the fahrenheit temperatures I wanted, but ended up just using celsius integers because they were more than close enough. I gave up trying to figure out how it is displaying 24.73 degrees when everything in the code specifies int
This structure is filled by getEvent..
temperature is a float within this structure..
And you print temp.temperature, it prints the float.. Adafruit_Sensor.h Line 144