Arduino Forum

Products => The Arduino Starter Kit and Basic Kit => Topic started by: dzulkiewski on Jan 14, 2014, 05:34 pm

Title: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 14, 2014, 05:34 pm
I am new to Arduino and am working my way through the project book. I am having trouble with the coding from Project #3. I have typed and re-typed the code and still run into the same problem when verifying: 'baseline' was not declared in this scope. I have attached a screen shot of the error. Thanks
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 14, 2014, 05:41 pm
Posting code is better.
Like the compiler, I can't see "baseline" either.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 14, 2014, 06:04 pm
Here is the entire code:
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup (){
  Serial.begin(9600); //open 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 the voltage to temerpature 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 < baseline+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){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(1);
}
 
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 14, 2014, 06:07 pm
So, nothing called "baseline", when "baselineTemp" is used everywhere else.

Please use code tags when posting code.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 14, 2014, 06:11 pm
Sorry, I'm new to all of this, so bare with me. What are code tags and how do I use them and post them? I typed the code word for word from the book and have re-typed it twice with the same results.
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 14, 2014, 06:15 pm
The clue is
Quote
nothing called "baseline", when "baselineTemp" is used everywhere else.


Quote
so bare with me

Never on a first date.

Code tags are explained here (http://forum.arduino.cc/index.php?topic=149075.0), in bold at the top of just about every section of the forum.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 14, 2014, 07:56 pm
Code: [Select]
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup (){
  Serial.begin(9600); //open 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 the voltage to temerpature 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 < baseline+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){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(1);
}

 
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 14, 2014, 08:02 pm
Have another look at reply #3.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 14, 2014, 08:06 pm
I'm sorry I am new to this and very unexperienced, but I have no idea which one is reply 3. Please get right to the point and let me know what I need to do to get help with this.
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 14, 2014, 08:43 pm
Quote
but I have no idea which one is reply 3

It's the one on this page labelled "Reply #3"

The clue is also repeated in reply #5.


Further hints:look carefully at these two snippets of code
Code: [Select]
if(temperature >= baselineTemp+2 &&   temperature < baseline+4)and
Code: [Select]
if(temperature >=baselineTemp+4 && temperature < baselineTemp+6){

Notice any similarities?
Notice any glaring differences?

Not surprisingly, I don't have the Arduino starter kit, so I can't verify you copied the code correctly or not.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 02:12 pm
I do, thank you for the assistance. That worked
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 15, 2014, 02:17 pm
OK, thanks for getting back.

Was the code correct in the book, or was there a typo?
If in the book, it should be possible to reach the author and get the correction in for the future.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 02:18 pm
I made the error, the book was correct.
I typed the code twice and must have missed it both times.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 02:36 pm
I have uploaded the code and all 3 lights turn on. I have tried blowing hot air on the sensor and also putting a bag of snow next to the sensor, but all 3 lights remain on. What am I doing wrong?
I found the calibration page on the Arduino website, can I plug that code into the Love-O-Meter code or do I need to calibrate separately?
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 15, 2014, 02:41 pm
Fine - glad you got it sorted out.

I'm sorry about the oblique pointers to the solution, but software's often like that, and it is useful to develop a different way of thinking, and working around the problem, based on what the tools are telling you.

Like what Indiana Jones said in The Last Crusade, "...and 'X'' never marks the spot" (even though in his case, it did!)

Quote
I have uploaded the code and all 3 lights turn on.

OK, now comes the first tool of debugging - the serial print.
What do they tell you?
(Ignore the lights)
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 02:45 pm
The lights tell me if the temp is above or below the baseline
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 15, 2014, 02:59 pm
OK, now comes the first tool of debugging - the serial print.
What do they tell you?
(Ignore the lights)
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 03:14 pm
Who is they?
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 15, 2014, 03:44 pm
This stuff
Code: [Select]
int sensorVal = analogRead(sensorPin);
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);

isn't there for decoration.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 03:47 pm
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?
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 15, 2014, 03:52 pm
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.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 03:53 pm
Thank you.
I have it working now (wire was not pushed all the way in on the bread board).
Title: Re: Project #3 - Love-O-Meter
Post by: AWOL on Jan 15, 2014, 04:39 pm
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.
Title: Re: Project #3 - Love-O-Meter
Post by: dzulkiewski on Jan 15, 2014, 04:40 pm
I appreciate the input
Title: Re: Project #3 - Love-O-Meter
Post by: romain45 on Jan 15, 2014, 08:34 pm
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 ;-)
Title: Re: Project #3 - Love-O-Meter
Post by: twelch on Jan 31, 2014, 11:21 pm
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!
Title: Re: Project #3 - Love-O-Meter
Post by: asimplecapsule on May 02, 2014, 05:52 pm

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

Code: [Select]
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.
Title: Re: Project #3 - Love-O-Meter
Post by: twelch on May 21, 2014, 04:12 pm
Sorry its been a while, have you sorted this out yet?
Title: Re: Project #3 - Love-O-Meter
Post by: asimplecapsule on May 21, 2014, 04:49 pm
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.
Title: Re: Project #3 - Love-O-Meter
Post by: twelch on May 21, 2014, 04:57 pm
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.
Title: Re: Project #3 - Love-O-Meter
Post by: twelch on May 21, 2014, 05:32 pm
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...
Code: [Select]
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
}
Title: Re: Project #3 - Love-O-Meter
Post by: Taiduino on Aug 31, 2014, 01:58 pm
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:

Code: [Select]
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
Title: Re: Project #3 - Love-O-Meter
Post by: Fabio881 on Sep 01, 2014, 01:30 pm
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:
Code: [Select]
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...

2) 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:
Code: [Select]

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 :-)

Bests,
Fabio
Title: Re: Project #3 - Love-O-Meter
Post by: Taiduino on Sep 01, 2014, 03:49 pm
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.
Title: Re: Project #3 - Love-O-Meter
Post by: roozy on Sep 23, 2016, 02:49 am
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);
  }
 
Title: Re: Project #3 - Love-O-Meter
Post by: roozy on Sep 23, 2016, 03:02 am
<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);
  }
  >