Skip to content

Commit 80ad628

Browse files
committed
Create PotentiometerChange.ino
1 parent ae8f30c commit 80ad628

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Stable Potentiometer change sensor
3+
4+
Sometimes you need a continuously changing analog sensor
5+
(like a potentiometer) to send only when it stops changing.
6+
One way to do this is to only send periodically, and only
7+
if the sensor has changed significantly. This sketch shows
8+
how that can work.
9+
10+
Circuit:
11+
- potentiometer attached to pin A0
12+
13+
created 23 Feb 2020
14+
by Tom Igoe
15+
*/
16+
17+
// noise threshold. any change less than this is noise:
18+
int noise = 5;
19+
// previous reading:
20+
int lastReading = 0;
21+
// send interval, in ms:
22+
int sendInterval = 1000;
23+
// last time you sent a reading, in ms:
24+
long lastSendTime = 0;
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
}
29+
30+
void loop() {
31+
// if the send interval has passed:
32+
if (millis() - lastSendTime > sendInterval) {
33+
// read the sensor:
34+
int sensor = analogRead(A0);
35+
// map to a different range if needed:
36+
// sensor = map(sensor, 0, 1023, 0, 255);
37+
// if the difference between current and last > noise:
38+
if (abs(sensor - lastReading) > noise) {
39+
// send it:
40+
Serial.println(sensor);
41+
}
42+
// save current reading for comparison next time:
43+
lastReading = sensor;
44+
// take a timestamp of when you sent:
45+
lastSendTime = millis();
46+
}
47+
}

0 commit comments

Comments
 (0)