/*
Arduino Starter Kit example
Project 3 - Love-O-Meter
This sketch is written to accompany Project 3 in the Arduino Starter Kit
Parts required:
- one TMP36 temperature sensor
- three red LEDs
- three 220 ohm resistors
created 13 Sep 2012
by Scott Fitzgerald
http://www.arduino.cc/starterKit
This example code is part of the public domain.
*/
// named constant for the pin the sensor is connected to
const int sensorPin = A0;
// room temperature in Celsius
const float baselineTemp = 20.0;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const double R = 10000; //R=10KΩ reference resistor
const double RT0 = 10000;//resistence of thermistor at 298.15K
const double T0 = 298.15; //25 + 273.15
void setup() {
// open a serial connection to display values
Serial.begin(9600);
// set the LED pins as outputs
// the for() loop saves some extra coding
for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop() {
// read the value on AnalogIn pin 0 and store it in a variable
int sensorVal = analogRead(sensorPin);
// send the 10-bit sensor value out the serial port
Serial.print("sensor Value: ");
Serial.print(sensorVal);
// convert the ADC reading to voltage
float voltage = (sensorVal / 1023.0) * 5.0;
// Send the voltage level out the Serial port
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees C: ");
double VR = 5 - voltage; // voltage of reference resistor that connected to VCC
double RT = voltage / (VR/R); //current resistence of NTC thermistor
double ln = log(RT / RT0);
double TX = (1 / ((ln / BETA) + (1 / T0))); //Temperature from thermistor
float temperature = TX ;//Conversion to Celsius.Need modifying
float celsius = TX - 273.15;
float fahrenheit = (celsius * 9 / 5) + 32;
Serial.println(temperature);
Serial.print(celsius);
Serial.print("°C, ");
Serial.print(fahrenheit);
Serial.println("°F");
// if the current temperature is lower than the baseline turn off all LEDs
if (celsius < baselineTemp + 2) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} // if the temperature rises 2-4 degrees, turn an LED on
else if (celsius >= baselineTemp + 2 && celsius < baselineTemp + 4) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} // if the temperature rises 4-6 degrees, turn a second LED on
else if (celsius >= baselineTemp + 4 && celsius < baselineTemp + 6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} // if the temperature rises more than 6 degrees, turn all LEDs on
else if (celsius >= baselineTemp + 6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}