Hi guys, now I using LDR to measure the light and read the value using Arduino. How can I comparing the current measure value with the previous measure value? For example, the first value read is 555 and delay for 5 seconds. After 5 seconds, the second value read is 520. I wanted to find the difference of the current value(second value) and the previous value(first value), how can I achieve this?
Use a static or global variable to store the older reading.
(deleted)
AWOL:
Use a static or global variable to store the older reading.
Since the measuring is continuously, so can the static variable update every time?
You can't update a static variable,
but you can use a current and a previous variable and calculate the difference every x seconds.
currentValue = analogRead(ldrPin);
difference = currentValue - previousValue;
Serial.println(difference);
previousValue = currentValue;
delay(5000);
Leo..
Wawa:
You can't update a static variable,
but you can use a current and a previous variable and calculate the difference every x seconds.currentValue = analogRead(ldrPin);
difference = currentValue - previousValue;
Serial.println(difference);
previousValue = currentValue;
delay(5000);
Leo..
I found a solution using millis()! Thanks for the reply!
Wawa:
You can't update a static variable,
That is not correct. It is a CONST that you cannot update.
"static" is used to tell the compiler that a local variable must be preserved between calls to a function. (And, before you say it, I agree that "static" was poor word to choose for the purpose).
...R