53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
|
|
#include <Arduino.h>
|
||
|
|
// JTR 2020-09-06: Doesn't work yet...
|
||
|
|
|
||
|
|
//#define LED_BUILTIN 1
|
||
|
|
|
||
|
|
int sensorPin = 7; // select the input pin for the potentiometer
|
||
|
|
int ledPin = 1; // select the pin for the LED
|
||
|
|
|
||
|
|
int minSens = 32767;
|
||
|
|
int maxSens = 0;
|
||
|
|
|
||
|
|
bool isInit=true; // the "millis() function will overflow after 50 days, therefore using a flag..."
|
||
|
|
|
||
|
|
void setup() {
|
||
|
|
pinMode(ledPin, OUTPUT);
|
||
|
|
pinMode(sensorPin, INPUT);
|
||
|
|
//Serial.begin(115200);
|
||
|
|
//delay(1000); // give me time to bring up serial monitor
|
||
|
|
//Serial.println("ESP32");
|
||
|
|
}
|
||
|
|
|
||
|
|
void loop() {
|
||
|
|
// read the value from the sensor:
|
||
|
|
int sensorValue = analogRead(sensorPin);
|
||
|
|
if (sensorValue > maxSens) { maxSens=sensorValue; }
|
||
|
|
if (sensorValue < minSens) { minSens=sensorValue; }
|
||
|
|
// stop the program for <sensorValue> milliseconds:
|
||
|
|
|
||
|
|
double diff=(double)(sensorValue-minSens+1)/(double)(maxSens-minSens+1);
|
||
|
|
|
||
|
|
if ((isInit) && (millis() < 1000*30)){
|
||
|
|
digitalWrite(ledPin, HIGH);
|
||
|
|
delay(diff*100);
|
||
|
|
digitalWrite(ledPin, LOW);
|
||
|
|
} else {
|
||
|
|
isInit=false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ((diff > 0.1) && (!isInit)) {
|
||
|
|
for (int i=0;i<60*5;i++){
|
||
|
|
// looks like really long delays are causing the thing to crash, therefore do something in the loop ;)
|
||
|
|
digitalWrite(ledPin, LOW);
|
||
|
|
digitalWrite(ledPin, HIGH);
|
||
|
|
|
||
|
|
delay(1000);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
digitalWrite(ledPin, LOW);
|
||
|
|
}
|
||
|
|
|
||
|
|
delay(50);
|
||
|
|
}
|