From f856428cb18470cb0a32baee6f9bda5d01568615 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 1 Nov 2008 18:03:07 +0000 Subject: [PATCH] Adding a calibration example and moving knock into the sensors folder. --- .../Analog/Calibration/Calibration.pde | 49 +++++++++++++++++++ .../{Analog => Sensors}/Knock/Knock.pde | 0 2 files changed, 49 insertions(+) create mode 100644 build/shared/dist/examples/Analog/Calibration/Calibration.pde rename build/shared/dist/examples/{Analog => Sensors}/Knock/Knock.pde (100%) diff --git a/build/shared/dist/examples/Analog/Calibration/Calibration.pde b/build/shared/dist/examples/Analog/Calibration/Calibration.pde new file mode 100644 index 000000000..29d75798c --- /dev/null +++ b/build/shared/dist/examples/Analog/Calibration/Calibration.pde @@ -0,0 +1,49 @@ +/* + * Calibration + * + * Demonstrates one techinque for calibrating sensor input. The + * sensor readings during the first five seconds of the sketch + * execution define the minimum and maximum of expected values. + */ + +int sensorPin = 2; +int ledPin = 9; + +int val = 0; +int sensorMin = 1023, sensorMax = 0; + +void setup() { + // signal the start of the calibration period + pinMode(13, OUTPUT); + digitalWrite(13, HIGH); + + // calibrate during the first five seconds + while (millis() < 5000) { + val = analogRead(sensorPin); + + // record the maximum sensor value + if (val > sensorMax) { + sensorMax = val; + } + + // record the minimum sensor value + if (val < sensorMin) { + sensorMin = val; + } + } + + // signal the end of the calibration period + digitalWrite(13, LOW); +} + +void loop() { + val = analogRead(sensorPin); + + // apply the calibration to the sensor reading + val = map(val, sensorMin, sensorMax, 0, 255); + + // in case the sensor value is outside the range seen during calibration + val = constrain(val, 0, 255); + + analogWrite(ledPin, val); +} diff --git a/build/shared/dist/examples/Analog/Knock/Knock.pde b/build/shared/dist/examples/Sensors/Knock/Knock.pde similarity index 100% rename from build/shared/dist/examples/Analog/Knock/Knock.pde rename to build/shared/dist/examples/Sensors/Knock/Knock.pde