I am new to the Arduino and programing. I am trying to teach myself. I got this sketch online and am trying to modify it. The problem I have is that when I upload it, the LED on pin 9 comes on and stays on no matter what the Temp is. I have been messing with this for what seems like forever and can't figure it out. Please Help!!!!!
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(soilTemp, 10);
pinMode (9, OUTPUT);
digitalWrite (9, LOW);
}
You define tempC both in printTemperature() and in loop(). They are not the same variable. Read up on the concept of "scope" to understand the difference. Also, you define tempC in loop() as an int, but then compare it to the floating point number 21.11. There's no guarantee what the value of tempC is in loop() because you never assign a value to it. However, my guess is that its value is 0, which always turns the LED on. Change this:
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
I tried your suggestion econjack but there was no change.
I also tried the loop like this but that didn't help either.
void loop(void)
{
float tempC;
if (tempC <= 21.11)
{
digitalWrite(9, HIGH);
}
else
{
digitalWrite(9, LOW);
}
delay(2000);
sensors.requestTemperatures();
You will have to forgive my lack of knowledge. The last thing I programed was a bouncing ball using a C64 when I was a kid. When you say I have to assign something to tempC I don't understand how to do that and an example would be of great help. Also I was wondering if someone could point me to some good reading about the concept of "scope"
Good Afternoon,
maybe before you can try to play a little bit with led
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
and then after add stuff to your sketch ... in this way you can understand the behavior of your code I hopo that can be helpfull for you !!
regards
Andrea