Calling functions in loop

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;

void setup()
{
Serial.begin(9600);
analogReference(DEFAULT);
}

void loop()
{

//delay(500);

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;

//Serial.println(avrgTempC);

return avrgTempC;
sampleCounter = 0;
}

}

Here are a few things I noted about your code.

  • Try the Tools -> Auto Format to properly indent your code.

  • Try to post the code in code tags. Click the </> icon.

// Your code should looke like this
bool goodFormating = true;
  • The following is just as bad as using delay().
unsigned long presentMillis = millis();
while (millis() - presentMillis < 5) //500 ms
{
;
}

Have a look at the following example. The points is, your loop() should run as often as possible.

File -> Examples -> 02.Digital -> BlinkWithoutDelay

  • Instructions after return are not executed
{
...
return avrgTempC;
sampleCounter = 0; // this will never execute
}

The compiler might even tell you when you enable Warning ALL in File -> Preferences.

millis is not a float but an unsigned long integer.

What is 'printlnn'? Is this some new method of the print class?

Just an observation...
Putting code after the RETURN won’t work.

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

avrgTempC = sumTempC / (float) numSamples;
return avrgTempC;
}[/code]

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 .

Thank you I am actually using energia msp432 launch pad ,that's why .

Thank you for you help

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.