Project #3 - Love-O-Meter

Who is they?

This stuff

 int sensorVal = analogRead(sensorPin);
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);

isn't there for decoration.

I'm brand new to this, so I apologize for my lack of knowledge, but I have no idea what you are getting at. I see the line of code on my screen. What are you trying to point out? Is there something I am supposed to change?

What I'm pointing out is that the sketch has a built-in mechanism for reporting the actual values read from the sensor.
This is what you should be looking at, not the lights.
This will help indicate whether you have your sensor wired correctly and if it is working as expected.

Thank you.
I have it working now (wire was not pushed all the way in on the bread board).

That's one of the other things about debugging - use as many different approaches as you can to examine the problem or its symptoms, but make sure you understand the limitations of each method.

I appreciate the input

Hello,

Could you explain to me why do we have to remove -0,5 to convert the voltage temperature, I don't understand why it's neceesary to create an offset.

Thanks in advance :wink:

If you open the serial monitor you will see the value being returned from the temperature sensor, go to line two of your code -"const float baselineTemp = 20.0;" and change the 20.0 to the value shown in the serial monitor. This will calibrate your program correctly.

If you want the code to calibrate itself simply remove the "const" from line 2 and ad the line "baselineTemp = analogRead(sensorPin);" to your setup void. This will record the initial value returned from the sensor and use that as your base line. Since you will be doing this in the setup void it will happen only once each time the arduino is reset or turned on.

Another option is to add a push button to your setup and add the appropriate code to set your baseline when the button is pressed. This can turn your Love-o-meter into a Who's Hotter game!

twelch:
Another option is to add a push button to your setup and add the appropriate code to set your baseline when the button is pressed. This can turn your Love-o-meter into a Who's Hotter game!

I want to do this with a push button but I can't seem to get it working. I have set up a circuit with the button feeding into a pin and tested that the button works with a little program that prints something to the serial monitor when the button is pressed/not pressed. I think it must be something to do with my code. I would be super grateful if someone would take a look and let me know what I'm doing wrong. I've tried to simply replicate the button pressing code from an earlier project...

const int sensorPin = A0; // name the analog pin for temp
int switchState = 0; //set a variable for the pushbutton on/off
float baselineTemp = 0.0; //introduce the baselineTemp variable

/*set the various pins to output and default off*/
void setup() {
   for(int pinNumber = 2; pinNumber<5; pinNumber++) {
    pinMode (pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
  pinMode (7, INPUT); //set the pushbutton pin to input
}

void loop() {
  int sensorVal = analogRead(sensorPin); //set a variable that holds the sensor value
  float voltage = (sensorVal/1024.0) * 5.0; //convert sensor value to voltage
  float temperature = (voltage - .5) * 100; //convert voltage to temp C
  float temperaturef = (temperature *1.8) + 32; //convert temp C to temp F

  
  switchState = digitalRead(7); //check pin 7 for current
  if(switchState == HIGH) { //if button is pushed
  float baselineTemp = temperaturef; //change baselineTemp to currently measured temp
  }  
  
  /*The rest of the code is the stock LED on/off code from the text and works fine*/
  
  else if(temperaturef < baselineTemp){ 
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  }
  
else if(temperaturef >= baselineTemp+2 &&
temperaturef < baselineTemp+4) {
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
}

else if(temperaturef >= baselineTemp+4 && 
temperaturef < baselineTemp+6) {
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
}else if(temperaturef >= baselineTemp+6) {
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}
delay(1);
}

edit: I put a Serial.print command after the button push to see if it was seeing the button push. It IS seeing the button push, but the change to the variable baselineTemp doesn't seem to be happening.

Sorry its been a while, have you sorted this out yet?

I haven't! I went on to other projects, but I'd still be interested in knowing how to do this, since it seems like a useful thing to be able to do.

Okay, well right off the top in your code you are setting the value for baselinetemp when the button is pressed. I would recomend creating a seperate function for callibration, call that routine in you setup funtion and then in your loop function when the button is pressed. You may need to integrate some debouncing on the button as well. Also take a look at the MAP function, it will certainly simplify your conversions.

sorry don't have as much time today as I though.... I was wrong about using the map function (unless you want to forget about actual temperature and just work on a scale of x to y) here is my code that I used you can compare it to yours and perhaps figure out your issue...

const int sensorPin = A0;
float baselineTemp = 0.0;
int sensorVal = 0;
float voltage = 0.0;
float  temperature = 0.0;
int buttonPin = 5;
int button = 0;
int oldbutton = 0;


void setup()
{
  Serial.begin(9600); //open serial port
  for(int pinNumber = 2; pinNumber < 5; pinNumber++) //define pins 2,3, and 4 as outputs and make sure they are off
  {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
  pinMode(buttonPin, INPUT);
  //callibrate baseline temperature
  calibrate();
}

void loop()
{
  button = digitalRead(buttonPin);
  if ((button == HIGH) && (button != oldbutton))
  {
    calibrate();
  }
  oldbutton = button;
  sensorVal = analogRead(sensorPin); //read value from sensor
  /*Serial.print("Sensor Value: ");
  Serial.print(sensorVal); //send current sensor reading
  voltage = (sensorVal/1024.0) * 5.0; //convert analog reading to voltage
  Serial.print(" , Volts: ");
  Serial.print(voltage); //send output voltage
  temperature = (voltage - .5) * 100; //convert voltage to actual temp.
  Serial.print(" , degrees C: ");
  Serial.println(temperature); //send temp.*/
  
  if(temperature < baselineTemp)
  {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  if(temperature >= baselineTemp+2 && temperature < baselineTemp+4)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  if(temperature >= baselineTemp+4 && temperature < baselineTemp+6)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }
  if(temperature >= baselineTemp+6)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(1);
}

void calibrate()
{
  sensorVal = analogRead(sensorPin); //read sensor
  voltage = (sensorVal/1024.0) * 5; //convert to voltage
  temperature = (voltage - .5) * 100; // convert to temp
  baselineTemp = temperature; //set baseline
}

Hi,

Sorry to dredge up an old topic, but thought it was better than starting a new one for the same project.

I am only able to get the digital 3 output to light up, both other LEDs stay off - no matter what changes I make to the baseline temp and other debugging I've tried. The circuit is set up exactly as shown in the guide, my code is below:

const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup (){
 Serial.begin(9600); // open a serial port
for(int pinNumber = 2; pinNumber<5; pinNumber++){
 pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop (){
  int sensorVal = analogRead (sensorPin);
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);
  //convert the ADC reading to voltage
  float voltage = (sensorVal/1024.0) *5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);
  Serial.print(", Degrees C: ");
  // convert voltage to temperature in degrees
  float temperature = (voltage - .5) * 100;
  Serial.println(temperature);
  if(temperature < baselineTemp){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }else if(temperature >= baselineTemp+2 &&
  temperature < baselineTemp+4) {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    }else if(temperature >= baselineTemp+4 &&
  temperature < baselineTemp+6) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    }else if(temperature >= baselineTemp+6 &&
  temperature < baselineTemp+8) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(1);
}

I'd really appreciate any tips anyone can provide, or if you can see where I'm going wrong, it would be a great help.

Thanks,

Tai

Hi Tai,

I don't see anything really wrong (e.g., syntax errors) in the code and I was able to compile it (but not to test it on my Arduino, since I don't have the circuit built at the moment). Some suggestions:

  1. Set a larger delay, for example:
delay(3000);

so that you are actually able to see on the serial monitor if the values of the parameters are really changing. Does the value of "temperature" change? And how much? I ask this, because this brings me to the next point...

  1. In your code I see an undefined situation that happens when the "temperature" variable gets larger than "baselineTemp+8".
    For example, in my code the last "else...if" condition is like this:
else if (temperature >= baselineTemp + 6.0)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);    
  }

In my experience with coding, aside from Arduino, I learned that is always better to catch all the possible conditions while setting up your "else..if" control expressions, instead of leaving them undefined.

Apart from these tips, if I understood correctly you are in the situation in which output #2 and output #4 are off, while output #3 lights up.
This configuration is not met anywhere in your code, so it's perhaps pointing to some errors/bad connections in wiring the circuit. In this case a photo of the setup might help :slight_smile:

Bests,
Fabio

Hi Fabio,

Thanks so much for the response.

Based upon your confirmation that nothing looks wrong in the code I went ahead and tried to move some things around on the circuit, it turns out that two of the 3 LEDs were bust- I guess this was a rookie error. Thanks again, really appreciate you pointing me in the right direction.

Hi,

So I am having abit of the same issue. I got the technical down its the coding that is giving me of a hard time. If you can look this over for me it will be greatly appreciated.


const int sensorPin = A0;
const float baslineTemp = 20.0;
void setup() {
Serial.begin(9600); //open a serie port
for(int pinNumber = 2; pinNumber<5; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop(){
int sensorVal = analogRead(sensorPin);
Serial.print ("Sensor Value: ");
Serial.print(sensorVal);
// Convert the ADC reading to Voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print (voltage);
Serial.print(", degree C: ");
//convert Voltage to temp in degrees
float temperature = (voltage - .5) * 100;
Serial.println (temperature);
if(temperature < baselineTemp){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if(temperature >= baseline +2 &&
temperature < basline+4){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if(temperature >= baselinetemp+4 &&
temperature < baseline Temp +6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}

<const int sensorPin = A0;
const float baslineTemp = 20.0;
void setup() {
Serial.begin(9600); //open a serie port
for(int pinNumber = 2; pinNumber<5; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop(){
int sensorVal = analogRead(sensorPin);
Serial.print ("Sensor Value: ");
Serial.print(sensorVal);
// Convert the ADC reading to Voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print (voltage);
Serial.print(", degree C: ");
//convert Voltage to temp in degrees
float temperature = (voltage - .5) * 100;
Serial.println (temperature);
if(temperature < baselineTemp){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if(temperature >= baseline +2 &&
temperature < basline+4){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if(temperature >= baselinetemp+4 &&
temperature < baseline Temp +6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}