Go Down

Topic: Programming Question..? (Read 742 times) previous topic - next topic


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!  :D

    Arduino thermistor example software
    Tutorial:  http://www.hacktronics.com/Tutorials/arduino-thermistor-tutorial
    Copyright (c) 2010 Mark McComb, hacktronics LLC
    License: http://www.opensource.org/licenses/mit-license.php (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) {
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.8) + 32.0;    // Convert to USA
  Serial.println(" F"); 
  if(fTemp > 75)
digitalWrite(pin2, HIGH);



void loop(void) {
Is it a 270 or a 330 resistor for LEDs? Aw hell, I just blew another one.


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:

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


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:

Code: [Select]
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.
There are 10 types of people in the world, those that understand binary, and those that don't.


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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131