Go Down

### Topic: Programming Question..? (Read 1 time)previous topic - next topic

#### Dawmasta

##### Nov 18, 2012, 05:22 am
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.. :p If anyone can help out by fixing the code and/or telling me how I majorly screwed up, that would be seriously appreciated.
Thanks!

/*
Arduino thermistor example software
Tutorial:  http://www.hacktronics.com/Tutorials/arduino-thermistor-tutorial
Copyright (c) 2010 Mark McComb, hacktronics LLC
*/

#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 http://en.wikipedia.org/wiki/Thermistor 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. + 32.0;    // Convert to USA
Serial.print(fTemp);
Serial.println(" F");

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

}

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

#### Arrch

#1
##### Nov 18, 2012, 05:48 am
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.

#### patduino

#2
##### Nov 18, 2012, 07:57 am
Quote
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:

Code: [Select]
`if(fTemp <= 73) digitalWrite(pin2, LOW); `
There are 10 types of people in the world, those who understand binary, and those that don't.

#### wildbill

#3
##### Nov 18, 2012, 12:46 pm
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.

#### patduino

#4
##### Nov 18, 2012, 04:58 pm
Wildbill is correct about readability. Along those lines, you should also use constants to declare the control limits, something like:

Code: [Select]
`const int FAN_ON_TEMP = 75; // turn fan on at 75 degrees Fahrenheitconst 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.
There are 10 types of people in the world, those who understand binary, and those that don't.

#### robtillaart

#5
##### Nov 18, 2012, 09:39 pm

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up