Skip to content

Commit 186aa39

Browse files
committed
added SoftPot example
1 parent 855d501 commit 186aa39

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

SoftPot/SoftPot.ino

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Soft pot sensor reading
3+
4+
Reads a SoftPot and uses the peak touch for each time you touch
5+
to set an LED's brightness.
6+
7+
The softpot has a pulldown resistor on the wiper
8+
per this SparkFun tutorial:
9+
https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v32/experiment-10-reading-a-soft-potentiometer
10+
The pulldown resistor ensures that the pot returns
11+
a low value when not touched
12+
13+
created 30 Jan 2017
14+
by Tom Igoe
15+
16+
*/
17+
18+
int lastReading = 0; // last sensor reading
19+
int threshold = 10; // touch threshold
20+
int brightness = 0; // LED brightness
21+
int peak = 0; // touch peak
22+
23+
void setup() {
24+
Serial.begin(9600); // initialize serial
25+
pinMode(11, OUTPUT); // make the LED pin an output
26+
}
27+
28+
void loop() {
29+
int sensor = analogRead(A0); // read sensor
30+
if (sensor > threshold) { // if it's above touch threshold
31+
if (sensor > lastReading) { // and it's above last reading
32+
peak = sensor; // then it's a peak
33+
} else { // otherwise,
34+
peak = lastReading; // last reading is a peak
35+
}
36+
brightness = peak / 4; // brightness is based on peak
37+
analogWrite(11, brightness); // set LED
38+
Serial.println(brightness); // print brightness
39+
}
40+
lastReading = sensor; // save current reading for next time
41+
}

0 commit comments

Comments
 (0)