
circuit turns on, press the sensor down a few
times while it calibrates. I usually put this in my
setup function:
int sensorPin = 0;
int sensorMax, sensorMin;
[...]
void calibrate() {
while (millis() < 5000) { // Wait 5
seconds (5000 milliseconds)
sensorValue = analogRead(0); //
Read your analog value
if (sensorValue > sensorMax) { //
If it is greater than the previous max
value, it is the new highest value
sensorMax = sensorValue;
}
if (sensorValue < sensorMin) { //
If it is less than the previous min
value, it is the new lowest value
sensorMin = sensorValue;
}
}
}
You can then take your reading using a function
like the following code, which ...