Hi all,
i need to catch touches from a TouchPin, but i have problems because i would like to grab one, two or three touches in a "window" of 1500ms (1 second and half), keeping in mind that any of the touches could stay LOW for more than 1500ms
How to get around this problem?
I began in this way
int touchVal = touchRead(BUTTON);
if(touchVal < TOUCH_THRESHOLD ){
lasttouch = millis();
digitalWrite(LED, LOW);
} else {
digitalWrite(LED, HIGH);
}
P.s. Skipping obvious lines...
I mean
#define BUTTON 27
#define LED 22
const int TOUCH_THRESHOLD = 50;
static uint32_t timenow;
static uint32_t timeend = millis();
static uint32_t lasttouch = 0;
static uint32_t firsttouch = 0;
i wrote something bad, just to get first and second touch, even when pressed, but i don't like it...
timenow = millis();
int touchVal = touchRead(BUTTON);
if ( timenow - timeend > 1500 ) {
if(touchVal < TOUCH_THRESHOLD ){
lasttouch = millis();
digitalWrite(LED, LOW);
} else {
digitalWrite(LED, HIGH);
}
if(touchVal > TOUCH_THRESHOLD && firsttouch == 0 && lasttouch != 0 ){
firsttouch = lasttouch;
lasttouch = 0;
}
if ( timenow - firsttouch < 1000 && timenow - firsttouch > 200 && touchVal < TOUCH_THRESHOLD ){
Serial.println("Second touch");
firsttouch = 0;
lasttouch = 0;
timeend = millis();
} else if ( timenow - firsttouch > 1000 && firsttouch != 0 ) {
Serial.println("First touch");
firsttouch = 0;
lasttouch = 0;
timeend = millis();
}
}
The assumption is that you need 200ms to catch a touch, less then 1000ms to get the second one, if there is only one touch in 1000ms, reset the cycle... (1500ms are to skip the first touch when you hit second touch...)
But as i told you, i don't like this code
Solved by myself...
This is the solution:
if(touchVal < TOUCH_THRESHOLD ){
if ( datepressed == 0) { datepressed = millis(); }
digitalWrite(LED, LOW);
} else {
if ( datepressed != 0) {
if ( now - datepressed > 80 ) {
elapsed = now - datepressed;
release = now;
lasttouch = now;
datepressed = 0;
}
}
digitalWrite(LED, HIGH);
}
if ( elapsed > 15000 ){
elapsed = 0;
release = 0;
//Serial.println("CONFIGURATION MODE");
} else if ( elapsed > 3000 ){
elapsed = 0;
release = 0;
//Serial.println("DISCONNECT");
} else if ( elapsed > 500 ){
elapsed = 0;
release = 0;
firsttouch = false;
delay(80);
//Serial.println("ENTER");
} else {
if ( release != 0 && firsttouch ){
firsttouch = false;
release = 0;
delay(80);
//Serial.println("SECOND TOUCH");
} else if ( release != 0 && !firsttouch ){
firsttouch = true;
release = 0;
//Serial.println("SET FIRST TOUCH");
delay(80);
} else {
if ( firsttouch && now - lasttouch > 600 ){
firsttouch = false;
release = 0;
delay(80);
//Serial.println("FIRST TOUCH");
}
}
}
system
Closed
August 26, 2022, 5:50pm
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.