... ok.. I guess it was too easy.. again... my last question of this kind (turn speakers on) was answered in record time, too :-(
Code:
const unsigned p1 = 17; // LED-Anode
const unsigned p2 = 16; // LED-Kathode
unsigned measure()
{
unsigned long long m;
//discharge any remaining load
pinMode(p2, OUTPUT);
digitalWriteFast(p2, LOW);
delay(1);
//Load the LED capacitance:
digitalWriteFast(p1, LOW); //Anode: GND
digitalWriteFast(p2, HIGH); //Kathode: 3V
delay(50);
//Now measure the time it needs to discharge through the
//high-impendance input:
m = millis();
pinMode(p2, INPUT);
while (digitalReadFast(p2) && (millis() - m < 100) ) {;}
m = millis() - m;
return m;
}
void ledsON() {
digitalWriteFast(LED_BUILTIN, HIGH);
digitalWriteFast(p1, HIGH);//Anode: 3V
digitalWriteFast(p2, LOW); //Kathode: GND
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(p1, OUTPUT);
}
void loop() {
if (measure() > 10) {
ledsON();
delay(20);
}
else {
digitalWriteFast(LED_BUILTIN, LOW);
}
}
Important: kathode on pin 16 (or edit the code).
Additional question for the experts: Why?