Hi please help , i am trying to call this function which is not working , i have the comment in the loop section please help
float sampleArray[20] = {};
unsigned int num=20;
int sampleCounter = 0;
float avrgTempC = 0;
Serial.printlnn(reading()); //(This doent return the value)
//reading(); //(This works when i uncomment serialprint in function )
}
float reading(){
//----check if 1/2-sec has gone-------
unsigned long presentMillis = millis();
while (millis() - presentMillis < 5) //500 ms
{
;
}
presentMillis = millis(); //1/2-sec has gone
//----take a signal and save in array
float tempC = analogRead(33); //this is Formula; want derivation? place request
sampleArray[sampleCounter] = tempC; //every float sample occupied 4 memory locations
sampleCounter++;
if (sampleCounter==num) //20 samples are collected
{
for (int i = 0; i < 20; i++)
{
avrgTempC = avrgTempC + sampleArray[i]; //all 20 samples are added
}
avrgTempC = avrgTempC /num;
I think this does what you wanted your sketch to do. I don't know what Arduino you are using or what analog input is on Pin 33.
[code]const int numSamples = 20;
float avrgTempC = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(reading());
}
float reading()
{
int sumTempC = 0;
for (int i = 0; i < numSamples; i++)
{
int tempC = analogRead(33); // ???? What pin is this ???
sumTempC += tempC;
delay(500);
}
Thank you guys for your help I am not very good at coding at all ,but I figured it out. I used a For loop to store the 20 analog values into the array first then I don't have to zero the counter in the return loop I just zero it at the start of the loop .