Go Down

Topic: Project #3 - Love-O-Meter (Read 11538 times) previous topic - next topic

dzulkiewski

The lights tell me if the temp is above or below the baseline

AWOL

OK, now comes the first tool of debugging - the serial print.
What do they tell you?
(Ignore the lights)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dzulkiewski


AWOL

This stuff
Code: [Select]
int sensorVal = analogRead(sensorPin);
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);

isn't there for decoration.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dzulkiewski

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?

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dzulkiewski

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

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dzulkiewski


romain45

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

twelch

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!

asimplecapsule

#26
May 02, 2014, 05:52 pm Last Edit: May 02, 2014, 05:56 pm by asimplecapsule Reason: 1

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.

twelch

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

asimplecapsule

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.

twelch

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.

Go Up