Hey, so i'm messing around with some IR sensors for a school project, and I'm trying to get the average values of the past 50 analog sensor readings from 3 different IR sensors in a function outside of the loop(). In my code I've called these variables avgLeft, avgMid, avgRight.
I've identified the average value variables as global variables. So in theory I should be able to do a Serial.print() outside of the function that I have written the code in (ie the loop()). Unfortunately when I run the code, it does not work this way. It seems that the only thing that I can see in the serial monitor is the serial print i've placed inside the for loop of the function called "findAvgValues()"
So the Serial.print("in function loop") does not show up in the serial monitor,
neither does the Serial.println("before for loop").
Any ideas as to why this is not working?? Shouldnt "in function loop" show up after the program has run through the "findAvgValues" function?? And shouldnt the string "before for loop" show up before the for loop starts to execute??
Here is the code...
int leftIR = A8;
int midIR = A10;
int rightIR = A15;
int SIZE = 50;
float avgLeft = 0;
float avgMid = 0;
float avgRight = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
findAvgValues();
Serial.print("in function loop");
}
void findAvgValues()
{
Serial.println("before for loop");
for(int i = 0; i < SIZE; i++)
{
/* Create avg of values, this will only pass 1 number to rest of program
over longer period of times */
float leftOut = analogRead(leftIR);
//Serial.println(leftOut);
float midOut = analogRead(midIR);
//Serial.println(midOut);
float rightOut = analogRead(rightIR);
//Serial.println(rightOut);
Serial.print(i);
Serial.println(" in for loop");
float leftTotal = 0;
float midTotal = 0;
float rightTotal = 0;
leftTotal += leftOut;
midTotal += midOut;
rightTotal += rightOut;
if (i == (SIZE-1))
{
Serial.println("in if statement");
avgLeft = leftTotal / SIZE;
avgMid = midTotal / SIZE;
avgRight = rightTotal / SIZE;
i = 0;
leftTotal = 0;
midTotal = 0;
rightTotal = 0;
}
}
}