This will I guess be a basic question, but I’m new to C and trying to write a timer to be used inside a for loop (with the functionality of delay(10)
). I have got timers to work previously, but now I need to pass the timer an argument (a counter) so I can add items to an array inside the timer function. I have read up on pointers as this is new to me, but am still not getting how to write the code. See error and code below:
error: passing argument 3 of ‘mgos_set_timer’ from incompatible pointer type
static void timer_read(int *i) {
counter = *i;
buf_pH[counter]=mgos_adc_read_voltage(SensorPin_pH);
}
float calc_pH() {
LOG(LL_INFO, ("Starting calculation"));
// Calculating pH:
for(int i=0;i<10;i++) //Get 10 sample values from the sensor to smooth the value
{
mgos_set_timer(10, 1, timer_read, &i);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf_pH[i]>buf_pH[j])
{
float temp_pH=buf_pH[i];
buf_pH[i]=buf_pH[j];
buf_pH[j]=temp_pH;
}
}
}
avgValue_pH=0;
for(int i=2;i<8;i++) { //sum 6 center values
avgValue_pH+=buf_pH[i];
}
LOG(LL_INFO, ("summed items (6): %ld", avgValue_pH));
float pH_float=(float)avgValue_pH*5.0/1024/6; //convert the analog into millivolt
pH_float=3.5*pH_float+Offset_pH; //convert the millivolt into pH value
LOG(LL_INFO, ("After calculating: pH=%f", pH_float));
return pH_float;
}
static void timer_print(void *user_data) {
float pH_float = calc_pH();
float EC_float = calc_EC();
LOG(LL_INFO, ("After calculating: pH=%f, EC=%f", pH_float, EC_float));
(void) user_data;
}