Temp sensor

I received new temperature sensors in the mail late yesterday eve. Oh joy! today I managed to get one to work. The kind I got is called the LM35 from National Semiconductor. It's output number is in millivolts, which has to be converted to temperature for any kind of serial port publishing. The conversion to Celsius and then Fahrenheit is commented out for now. Also, I have created a formula for getting the millivolt reading to analogue write in the range for an led. I have not included an upper limit in the coding (past 255), but that would mean a reading of past 102 degrees F.

Here is the code I am using:

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a #define rather than a normal variable lets
// use this value to determine the size of the readings array.

#define NUMREADINGS 4

int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int temperature = 0;
int Ftemperature = 0; // to convert to Fahrenheit
int inputPin = 0;
int LedBlue = 9; // led connected to 9
int squarex = 0; // sets value of squarex

void setup()
{
Serial.begin(9600); // initialize serial communication with computer
for (int i = 0; i < NUMREADINGS; i++)
readings[i] = 0; // initialize all the readings to 0
pinMode(LedBlue, OUTPUT); // sets the pin as output
}

void loop()
{
total -= readings[index]; // subtract the last reading
readings[index] = analogRead(inputPin); // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index

if (index >= NUMREADINGS) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning

average = total / NUMREADINGS; // calculate the average

// temperature = (5.0 * average * 100.) /1023 ; //switches average read to celcius temp
// Ftemperature = (temperature * 1.8) + 32.0 ; //converts to Fahrenheit

// analogWrite (LedBlue, (Ftemperature - 70.0) * 10.2 );
squarex = (average-10) * (average-10) ;
analogWrite (LedBlue, (squarex / 100) * (squarex / 1000 ) );




Serial.println(average); // send it to the computer (as ASCII digits)
// Serial.println(Ftemperature); // send it to the computer (as ASCII digits)
}



No comments: