Programming Question..?

Hello!
I'm new to programming, so my skills mainly include looking up commands and editing existing code. I have a code that should print the current temperature in F to the console, and if the temperature goes over 75 degrees it will activate a case fan. This all works well, but when the temperature goes below 75, the fan doesn't stop. Here is my code, as messy as it is.. :stuck_out_tongue: If anyone can help out by fixing the code and/or telling me how I majorly screwed up, that would be seriously appreciated.
Thanks! :smiley:

/*
Arduino thermistor example software
Tutorial: http://www.hacktronics.com/Tutorials/arduino-thermistor-tutorial
Copyright (c) 2010 Mark McComb, hacktronics LLC
License: The MIT License – Open Source Initiative (Go crazy)
*/

#include <LiquidCrystal.h>
#include <math.h>

/*
See tutorial for Thermistor connections

LCD Connections:
rs (LCD pin 4) to Arduino pin 12
rw (LCD pin 5) to Arduino pin 11
enable (LCD pin 6) to Arduino pin 10
LCD pin 15 to Arduino pin 13
LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
*/

int pin2 = 2;

void setup(void) {
Serial.begin(9600);
pinMode(pin2, OUTPUT);
}
double Thermister(int RawADC) {
double Temp;
// See Thermistor - Wikipedia for explanation of formula
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}

void printTemp(void) {
double fTemp;
double temp = Thermister(analogRead(0)); // Read sensor

fTemp = (temp * 1.8) + 32.0; // Convert to USA
Serial.print(fTemp);
Serial.println(" F");

if(fTemp > 75)
digitalWrite(pin2, HIGH);

}

void loop(void) {
printTemp();
delay(1000);
}

  1. When you post code, post using the CODE tags. You should have read that when you were reading the Read this before posting a programming question thread at the top of the forums.
  2. In order for the fan to turn off, you have to tell it to turn off. You never do, you only tell it to turn on if it goes over 75, but not to turn back off if it goes below it.

In order for the fan to turn off, you have to tell it to turn off.

As Arrch says, you are missing some logic.

Also, it is common in situations like this to add hysteresis (en.wikipedia.org/wiki/Hysteresis and make the "turn off" temp lower than the "turn on" temp to keep the fan from turning on and off too quickly.

Maybe something like:

if(fTemp <= 73)
 digitalWrite(pin2, LOW);

When you apply Patduino's fix though, consider putting it in the loop routine or changing the name of printTemp. It's a bit counterintuitive, given its name, to find the fan controller code in a routine called printTemp.

Wildbill is correct about readability. Along those lines, you should also use constants to declare the control limits, something like:

const int FAN_ON_TEMP = 75; // turn fan on at 75 degrees Fahrenheit
const int FAN_OFF_TEMP = 73; // turn fan off at 73 degrees Fahrenheit

And use those constants in place of the literal values in the code.

pressing CTRL-T in the IDE takes care of auto indent (makes code more readable too)