|
@@ -0,0 +1,34 @@
|
|
1
|
+#define SENSORPIN A7
|
|
2
|
+#define OUTPIN 13
|
|
3
|
+#define DELAYMILLIS 50
|
|
4
|
+#define NSAMPLES 20
|
|
5
|
+#define MINCHANGE 13
|
|
6
|
+#define ON HIGH
|
|
7
|
+#define OFF LOW
|
|
8
|
+
|
|
9
|
+int samples[NSAMPLES] = {0};
|
|
10
|
+int index;
|
|
11
|
+
|
|
12
|
+void setup() {
|
|
13
|
+ pinMode(OUTPIN, OUTPUT);
|
|
14
|
+ Serial.begin(115200);
|
|
15
|
+ index = 0;
|
|
16
|
+}
|
|
17
|
+
|
|
18
|
+void loop() {
|
|
19
|
+ samples[index] = analogRead(SENSORPIN);
|
|
20
|
+ index = (index + 1) % NSAMPLES;
|
|
21
|
+ int maxlevel = -1;
|
|
22
|
+ int minlevel = 2000;
|
|
23
|
+ for (int i = 0; i < NSAMPLES; i++) {
|
|
24
|
+ if (samples[i] < minlevel) {
|
|
25
|
+ minlevel = samples[i];
|
|
26
|
+ }
|
|
27
|
+ if (samples[i] > maxlevel) {
|
|
28
|
+ maxlevel = samples[i];
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+ Serial.println(maxlevel - minlevel);
|
|
32
|
+ digitalWrite(OUTPIN, (maxlevel - minlevel) > MINCHANGE ? ON : OFF);
|
|
33
|
+ delay(DELAYMILLIS);
|
|
34
|
+}
|