Pages: [1]   Go Down
Author Topic: Programming Question..?  (Read 427 times)
0 Members and 1 Guest are viewing this topic.
Missouri
Offline Offline
Newbie
*
Karma: 0
Posts: 14
If you accidentally pull a wire, make sure it's the volts in. *shrugs*
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-grin

/* 
    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) {
  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.smiley-cool + 32.0;    // Convert to USA
  Serial.print(fTemp);
  Serial.println(" F"); 
 
  if(fTemp > 75)
 digitalWrite(pin2, HIGH);

   
 
}

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

Eeyup.

California
Online Online
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if(fTemp <= 73)
 digitalWrite(pin2, LOW);
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

New Jersey
Online Online
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: