Project 3: Love-o-meter in Celcius?

I have the circuit all hooked up and loaded the project sketch. Worked perfectly the first time. The sketch and example in the book use centigrade. Wondering why a Fahrenheit conversion annotation wasn't included. Now I'm off to learn how to convert to Fahrenheit. :)

Well I figured it out without much issue. It didn’t like some of the formatting I was throwing at it. Having verify there is really handy and works great for these simple projects.

Here’s the Celcius to Fahrenheit conversion code for my American Love-O-Meter. Output’s both Celcius and Fahrenheit to the serial monitor.

Example Output in serial monitor:

sensor Value: 149, Volts: 0.73, degrees C: 22.75, degrees F: 72.96

/*
Arduino Starter Kit example
Project 3 - Love-O-Meter

This sketch is written to accompany Project 3 in the
Arduino Starter Kit

Parts required:
1 TMP36 temperature sensor
3 red LEDs
3 220 ohm resistors

Created 13 September 2012
by Scott Fitzgerald

http://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 Celcius
const float baselineTemp = 20.0;

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/1024.0) * 5.0;

// Send the voltage level out the Serial port
Serial.print(", Volts: ");
Serial.print(voltage);

// convert the voltage to temperature in degrees C
// the sensor changes 10 mV per degree
// the datasheet says there’s a 500 mV offset
// ((volatge - 500mV) times 100)
Serial.print(", degrees C: “);
float temperatureC = (voltage - .5) * 100;
Serial.print(temperatureC);
// convert the celcius value to fahrenheit to display for Americans
Serial.print(”, degrees F: ");
float temperatureF = (temperatureC * 9 / 5 + 32);
Serial.println(temperatureF);

// if the current temperature is lower than the baseline
// turn off all LEDs
if(temperatureC < baselineTemp){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} // if the temperature rises 2-4 degrees, turn an LED on
else if(temperatureC >= baselineTemp+2 && temperatureC < 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(temperatureC >= baselineTemp+4 && temperatureC < 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(temperatureC >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}

My first successful code modification from scratch. I think this one should be included in the example sketch instead because it includes both Celcius and Fahrenheit conversions. The code still only uses the Celcius value to calculate when to turn on the LED’s. Fahrenheit is only for displaying in the serial monitor data. Neat? :slight_smile:

I immediately had the same question, but ended up figuring it out. I also added a fourth LED and the addition of a buzzer so that when the fourth LED lights up the buzzer goes off. The kids really get a kick out of this because the buzzer adds and extra element of excitement. I told them if they are really love struck then the buzzer will go off. I was surprised how easy it was to set up the buzzer. I set it up on pin 6 in this variation:

//Addition of 4th LED and a buzzer to go off with 4th LED
 */

// named constant for the pin the sensor is connected to
const int sensorPin = A0;
  // room temperature in Celcius
const float baselineTemp = 78;


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<6; 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);
  //define alarm pitch

  // 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/1024.0) * 5.0;

  // Send the voltage level out the Serial port
  Serial.print(", Volts: ");
  Serial.print(voltage);

  // convert the voltage to temperature in degrees C
  // the sensor changes 10 mV per degree
  // the datasheet says there's a 500 mV offset
  // ((volatge - 500mV) times 100)
  Serial.print(", degrees C: "); 
  float temperature = (voltage - .5) * 100;
  Serial.print(temperature);
  float degF = (temperature*9/5+32);
  Serial.print(", degrees F: ");
  Serial.println(degF);

  // if the current temperature is lower than the baseline
  // turn off all LEDs
  if(degF < baselineTemp){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  } // if the temperature rises 2-4 degrees, turn an LED on 
  else if(degF >= baselineTemp +2 && degF < baselineTemp+4){
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  } // if the temperature rises 4-6 degrees, turn a second LED on  
  else if(degF >= baselineTemp+4 && degF < baselineTemp+6){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  } // if the temperature rises more than 6 degrees, turn all LEDs on
  else if(degF >= baselineTemp+6 && degF < baselineTemp+8){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    } // if the temperature rises more than 6 degrees, turn all LEDs on and sends tone to speaker
  else if(degF >= baselineTemp+8){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    tone(6,2000,50);
  }
  delay(10);
}