Hi All,
I know this topic is a common pitfall for folks, as I have read many posts on the subject, but remain puzzled with my program's behavior.
I am trying to maintain a moving average of analog readings from two sensors. Six readings are stored in an array for each sensor. At a specified interval, I want to call my "update_local_read" function, which takes an array reference and an analog pin number as arguments. The function then updates the average by shifting values one position up in the referenced array, taking a new analog reading for the [0] position, and then calling another function to calculate the average of the array.
The function(s) works as I expect when I reference my temperature readings array ("indoor_Temps"), but when I add a subsequent (or preceding) call to the same function with a reference to the "indoor_Humids" array, the indoor_Humids array does not behave as I would expect it to (see output below). However, if I comment out the function call for the "indoor_Temps" array and only call update_local_read (HUMID_PIN, indoor_Humids), then the indoor_Humids array behaves as I would expect.
I was able to make a modified version of this code compile and run in C99 (using keyboard input instead of sensor input), and both function calls worked in that case. However with the Arduino, it feels like somehow, the function is not "accepting" the reference to the indoor_Humids array, but rather it ends up incorporating random numbers from the indoor_Temps array.
Thanks very much in advance for any thoughts or advice!
Mike
const byte TEMP_PIN = 0;
const byte HUMID_PIN = 1;
const byte NUM_SAMPLES = 6;
float indoor_avg_H = 0;
float indoor_avg_T = 0;
int indoor_Temps[NUM_SAMPLES];
int indoor_Humids[NUM_SAMPLES];
unsigned long local_read_time = 0;
// Array handling function prototypes
float update_local_read (byte read_pin, int indoor_reads[]);
float average(int indoor_array[]);
void setup()
{
//initialize arrays
for (int i = 0; i < NUM_SAMPLES; i++)
{
indoor_Temps[i] = 0;
indoor_Humids[i] = 0;
}
Serial.begin(9600);
Serial.print("===============================================");
Serial.println();
}
void loop()
{
if (millis() >= local_read_time)
{
Serial.print("Humidity: ");
indoor_avg_H = update_local_read(HUMID_PIN, indoor_Humids);
Serial.print(" ");
Serial.print("Temp: ");
indoor_avg_T = update_local_read(TEMP_PIN, indoor_Temps);
Serial.println();
local_read_time = millis() + 1000;
}
}
// update indoor analog readings
float update_local_read (byte read_pin, int indoor_reads[])
{
// shift array one pos right
for (int i = NUM_SAMPLES - 1; i >= 0; i--)
indoor_reads[i + 1] = indoor_reads[i];
//newest reading into [0]
indoor_reads[0] = analogRead(read_pin);
indoor_reads[0] = analogRead(read_pin); // add delay?
for (int i = 0; i < NUM_SAMPLES; i++)
{
Serial.print(indoor_reads[i]);
Serial.print(", ");
}
float indoor_avg = average(indoor_reads);
Serial.print("avg = "); Serial.print(indoor_avg, 2);
return indoor_avg;
}
// average the array for local T or H
float average(int indoor_array[])
{
float avg = 0;
int sum = 0;
for (byte i = 0; i < NUM_SAMPLES; i++)
sum += indoor_array[i];
avg = (float) sum / NUM_SAMPLES;
return avg;
}
Output. indoor_Temps array fills and behaves as expected, but not the indoor_Humids.
Humidity: 402, 0, 0, 0, 0, 0, avg = 67.00 Temp: 503, 0, 0, 0, 0, 0, avg = 83.83
Humidity: 401, 0, 0, 0, 0, 0, avg = 66.83 Temp: 503, 503, 0, 0, 0, 0, avg = 167.67
Humidity: 401, 0, 0, 0, 0, 0, avg = 66.83 Temp: 503, 503, 503, 0, 0, 0, avg = 251.50
Humidity: 401, 0, 0, 0, 0, 0, avg = 66.83 Temp: 503, 503, 503, 503, 0, 0, avg = 335.33
Humidity: 401, 0, 0, 0, 0, 0, avg = 66.83 Temp: 503, 503, 503, 503, 503, 0, avg = 419.17
Humidity: 402, 0, 0, 0, 0, 0, avg = 67.00 Temp: 503, 503, 503, 503, 503, 503, avg = 503.00
Humidity: 405, 0, 0, 0, 0, 0, avg = 67.50 Temp: 506, 503, 503, 503, 503, 503, avg = 503.50
Humidity: 403, 503, 0, 0, 0, 0, avg = 151.00 Temp: 503, 506, 503, 503, 503, 503, avg = 503.50
Humidity: 402, 503, 503, 0, 0, 0, avg = 234.67 Temp: 503, 503, 506, 503, 503, 503, avg = 503.50
Humidity: 402, 503, 503, 503, 0, 0, avg = 318.50 Temp: 503, 503, 503, 506, 503, 503, avg = 503.50
Humidity: 402, 503, 503, 503, 503, 0, avg = 402.33 Temp: 503, 503, 503, 503, 506, 503, avg = 503.50
Humidity: 402, 503, 503, 503, 503, 503, avg = 486.17 Temp: 503, 503, 503, 503, 503, 506, avg = 503.50
Humidity: 403, 503, 503, 503, 503, 503, avg = 486.33 Temp: 503, 503, 503, 503, 503, 503, avg = 503.00
Humidity: 404, 506, 503, 503, 503, 503, avg = 487.00 Temp: 503, 503, 503, 503, 503, 503, avg = 503.00
Humidity: 404, 503, 506, 503, 503, 503, avg = 487.00 Temp: 503, 503, 503, 503, 503, 503, avg = 503.00
Here is the program output when only update_local_read(HUMID_PIN, indoor_Humids) is called...it behaves as I want/expect it to...
Humidity: 394, 0, 0, 0, 0, 0, avg = 65.67
Humidity: 394, 394, 0, 0, 0, 0, avg = 131.33
Humidity: 393, 394, 394, 0, 0, 0, avg = 196.83
Humidity: 395, 393, 394, 394, 0, 0, avg = 262.67
Humidity: 396, 395, 393, 394, 394, 0, avg = 328.67
Humidity: 396, 396, 395, 393, 394, 394, avg = 394.67
Humidity: 396, 396, 396, 395, 393, 394, avg = 395.00
Humidity: 396, 396, 396, 396, 395, 393, avg = 395.33
Humidity: 396, 396, 396, 396, 396, 395, avg = 395.83
Humidity: 412, 396, 396, 396, 396, 396, avg = 398.67
Humidity: 457, 412, 396, 396, 396, 396, avg = 408.83
Humidity: 485, 457, 412, 396, 396, 396, avg = 423.67