Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 3 months ago.
Temperature Sensor to only sense when there is a value
Hello. I am working with LM35 Temperature Sensor. I only want the temperature to print the temperature when it senses a new value (a value different from the one sensed and stored previously). This is my code, but i can't get it.
#include "mbed.h"
AnalogIn analog_value(PA_0);
Serial pc(SERIAL_TX, SERIAL_RX);
int main() {
float temp;
float prevalue=0;
float currvalue;
while(1) {
currvalue = analog_value.read();
if(prevalue==currvalue){
wait(0.1); }
else{
temp = ((currvalue*5000)/10);
pc.printf("temprature = %.0f ^C\r\n", temp);
prevalue=currvalue;
}
}
}
1 Answer
8 years, 3 months ago.
Hello Fawaz,
Since the measured temperature is always a bit floating I would suggest to neglect small changes for example as follows:
#include "mbed.h"
AnalogIn analog_value(PA_0);
Serial pc(SERIAL_TX, SERIAL_RX);
int main(void) {
const float DELTA = 1.0; // in degree C
const float CONV = 3300 / 10;
float prevvalue = 0;
float currvalue;
while (1) {
currvalue = analog_value.read() * CONV;
if (((prevvalue - DELTA) < currvalue) && (currvalue < (prevvalue + DELTA))) {
wait(0.1);
}
else {
prevvalue = currvalue;
pc.printf("temprature = %5.1f%cC\r\n", currvalue, 176);
}
}
}
Regarding the correctness and conversion of values measured with LM35 temperature sensor also have a look at:
https://developer.mbed.org/questions/6317/AnalogIn-problem-pics-included-strange-v/
TIP FOR EDITING: You can copy and paste a code into your question as text and enclose it within <<code>> and <</code>> tags as below (each tag on separate line). Then it's going to be displayed as code in a frame.
<<code>>
#include "mbed.h"
DigitalOut led1(LED1);
int main()
{
while (1) {
led1 = !led1;
wait(0.5);
}
}
<</code>>