Hello, I’m in the midst of making a project which utilizes 3 temperature sensors. The problem I’m encountering is that when I try to average the readings, it seems the max I can sample is 3 before all I get is zeros. This is with the while loop, originally I tried a FOR loop and the max sample rate was 2 for that? Seems weird to me but let me explain how this happens. Initially when the code is uploaded with a higher sample rate, let’s say 10, the code will work perfectly but once I unplug the USB and plug it back in via USB or wall jack all I will get is the zeros. It will only work with a maximum of 3 (or 2 with FOR) when I unplug it. Resetting has no effect either way, when its working it stays working, when its unplugged it still won’t work. Here is the code:
#include <LiquidCrystal.h>
LiquidCrystal lcd (13,12,7,6,5,4);
void loop () {
lcd.clear(); // clear LCD screen
tempS(1);
tempS(2);
tempS(3);
delay(350);
};
void setup()
{
lcd.begin(20, 4); // need to specify how many columns and rows are in the LCD unit
lcd.setCursor(0,0);
lcd.print("Reef Controller");
lcd.setCursor(0,1);
lcd.print("test * v0.4");
lcd.setCursor(0,2);
lcd.print("TEMP");
delay(2000);
}
void tempS(int sensor)
{
int nsamp = 3;
float temp;
float tempValue;
int j = 0;
//for (byte j=0;j<nsamp;j++)
while (j < nsamp)
{
tempValue =tempValue + analogRead(sensor);
j++;
}
tempValue=tempValue/nsamp;
temp = tempValue;
temp = temp*5/1024.0;
//temp = analogRead(sensor)*5/1024.0;
temp = temp - 0.5;
temp = temp / 0.01;
if (sensor == 1)
{
lcd.setCursor(0,0);
}
if (sensor == 2)
{
lcd.setCursor(0,1);
}
if (sensor == 3)
{
lcd.setCursor(0,2);
}
lcd.print("Temp");
lcd.print(sensor,DEC);
lcd.print(": ");
lcd.print(temp,0); //# means decimal places
lcd.print((char)223); //degree
lcd.print("C");
}
Any help would be appreciated, thanks!