My Arduino controller reads data coming from four different sensors. After the data is read, I individually compute four input average values. I understood that I am having the same lines of code repeated four times. Instead, I want to define a function with an analog pin as input and compute the average. My code is working fine. The question I have is, will this function improve the speed, reduce memory?
My code:
// Samples average
sample_cnt = 1000;
//// Input the pin number and compute average value
float Average_input_value(int analog_pin){
float avg_val = 0;
for(int i=0;i<sample_cnt;i++){
avg_val += analogRead(analog_pin);
}
// Divide by Sample count to get the average value
avg_val = avg_val/sample_cnt;
return avg_val;
}