thdfx
May 14, 2021, 9:08am
#1
This code is an example code for a water level sensor and was given to me without any attached explanation of how it works. can anyone please explain?
int adc_id = 0;
int HistoryValue = 0;
char printBuffer[128];
void setup()
{
Serial.begin(9600);
}
void loop()
{
int value = analogRead(adc_id);
if(((HistoryValue>=value) && ((HistoryValue - value) > 10)) || ((HistoryValue<value) && ((value - HistoryValue) > 10)))
{
sprintf(printBuffer,"ADC%d level is %d\n",adc_id, value);
Serial.print(printBuffer);
HistoryValue = value;
}
}
if(((HistoryValue>=value) && ((HistoryValue - value) > 10)) || ((HistoryValue<value) && ((value - HistoryValue) > 10)))
(
if the previous value is greater than or equal to the value just read
AND
the difference between the previous value and the value just read is greater than 10
)
OR
(
the previous value is not zero
AND
the value just read is at least 10 greater than the previous value
)
then do something
MarkT
May 14, 2021, 11:05am
#3
Its using hysteresis to avoid being affected by small changes or noise in the signal. It would be much clearer and simpler if written as:
if (abs (value - HistoryValue) > 10) // if more than 10 different from last recorded reading
{
...
HistoryValue = value ;
}
system
Closed
September 11, 2021, 11:06am
#4
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.