Hello,
So I am using the touch pins of an ESP32 to control a relay to start a device, I have achieved my goal but the issue Im having is that due to interference the capacitance of the touch pins reduces below my threshold sometimes even going to 0, thereby triggering my relay without even giving input, so i figured id try to introduce a time function (millis()) to read the value for say 2 secs and until and unless the same value is read for that said time, only then would the relay get triggered, im having trouble with the code as millis() is continuous as soon as it starts, could you guys look at my code and help me solve my trouble?
const int touchPin5 = 13;
const int relay5 = 19;
const int threshold = 40;
int stayOn = 3000;
unsigned long startTime = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Touch sensor tutorial ESP32");
pinMode (relay5, OUTPUT);
digitalWrite(relay5, HIGH);
}
void loop()
{
int touchValue5;
touchValue5 = touchRead(touchPin5);
Serial.print("Touch0 value is = ");
Serial.println( touchValue5);
delay(500);
if(touchValue5 < threshold)
{
digitalWrite(relay5, HIGH);
}
if((touchValue5 < threshold) && ((millis() - startTime) + 2000UL) > (millis() - startTime))
{
Serial.println(millis() - startTime);
digitalWrite(relay5, LOW);
Serial.println(" - LED ON");
delay(stayOn);
digitalWrite(relay5, HIGH);
}
}
Thank You & Cheers