12345678910111213141516171819202122232425262728293031323334 |
- #define SENSORPIN A7
- #define OUTPIN 13
- #define DELAYMILLIS 50
- #define NSAMPLES 20
- #define MINCHANGE 13
- #define ON HIGH
- #define OFF LOW
-
- int samples[NSAMPLES] = {0};
- int index;
-
- void setup() {
- pinMode(OUTPIN, OUTPUT);
- Serial.begin(115200);
- index = 0;
- }
-
- void loop() {
- samples[index] = analogRead(SENSORPIN);
- index = (index + 1) % NSAMPLES;
- int maxlevel = -1;
- int minlevel = 2000;
- for (int i = 0; i < NSAMPLES; i++) {
- if (samples[i] < minlevel) {
- minlevel = samples[i];
- }
- if (samples[i] > maxlevel) {
- maxlevel = samples[i];
- }
- }
- Serial.println(maxlevel - minlevel);
- digitalWrite(OUTPIN, (maxlevel - minlevel) > MINCHANGE ? ON : OFF);
- delay(DELAYMILLIS);
- }
|