Doubt regarding Moving Average Problem is STM32
Hi,
I want to compute the moving average of an IR sensor and set the brightness of an LED and im taking input values from read and I have set up an a timer interrupt which gets triggered every 100ms, and with this trigger I'm saving the read values into an array of size 10. My window size is 10. When I'm running the values keep running very rapidly to infinity. Please help. Thank you. Please check the timer interrupt loop for moving average, I feel something is wrong there.
uint16_t read;
float MA[10];
float sum =0;
uint16_t i=0;
uint16_t j=0;
while (1)
{
/* USER CODE END WHILE */
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
read = HAL_ADC_GetValue(&hadc1);
if(sum >=2470 && sum <=2530){
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1000);
}
else if(sum>=999 && sum <=2470){
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, sum/5);
}
else if(sum >= 2530 && sum <=4095){
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, (5095-sum)/5);}
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
float window_size = 10;
if(htim->Instance == TIM3){
MA[i%10] = read;
i=i+1;
for(j=0;j<10;j++){
sum = sum + MA[j]/window_size;
}
}
}
