mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-01 03:47:23 +03:00
Adding a calibration example and moving knock into the sensors folder.
This commit is contained in:
49
build/shared/dist/examples/Analog/Calibration/Calibration.pde
vendored
Normal file
49
build/shared/dist/examples/Analog/Calibration/Calibration.pde
vendored
Normal file
@ -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);
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
/* Knock Sensor
|
||||
* by DojoDave <http://www.0j0.org>
|
||||
*
|
||||
* Program using a Piezo element as if it was a knock sensor.
|
||||
*
|
||||
* We have to basically listen to an analog pin and detect
|
||||
* if the signal goes over a certain threshold. It writes
|
||||
* "knock" to the serial port if the Threshold is crossed,
|
||||
* and toggles the LED on pin 13.
|
||||
*
|
||||
* http://www.arduino.cc/en/Tutorial/Knock
|
||||
*/
|
||||
|
||||
int ledPin = 13; // led connected to control pin 13
|
||||
int knockSensor = 0; // the knock sensor will be plugged at analog pin 0
|
||||
byte val = 0; // variable to store the value read from the sensor pin
|
||||
int statePin = LOW; // variable used to store the last LED status, to toggle the light
|
||||
int THRESHOLD = 100; // threshold value to decide when the detected sound is a knock or not
|
||||
|
||||
void setup() {
|
||||
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
|
||||
Serial.begin(9600); // use the serial port
|
||||
}
|
||||
|
||||
void loop() {
|
||||
val = analogRead(knockSensor); // read the sensor and store it in the variable "val"
|
||||
if (val >= THRESHOLD) {
|
||||
statePin = !statePin; // toggle the status of the ledPin (this trick doesn't use time cycles)
|
||||
digitalWrite(ledPin, statePin); // turn the led on or off
|
||||
Serial.println("Knock!"); // send the string "Knock!" back to the computer, followed by newline
|
||||
}
|
||||
delay(100); // we have to make a delay to avoid overloading the serial port
|
||||
}
|
||||
|
Reference in New Issue
Block a user