long press with Capacitive touch

Hi everyone! Anyone has tried long press (3 seconds) to turn on/off LED using capacitive touch?
Mind to share how to code?

Thank you

What have you tried ?

UKHeliBob:
What have you tried ?

Hi UKHeliBob, I am tying millis() but still not working as intended. Saw alot of long press examples using button.

I am tying millis() but still not working as intended.

Please post the code that you tried and describe the problems you encountered

UKHeliBob:
Please post the code that you tried and describe the problems you encountered

Problems I encountered;

Millis() continues to run and will eventually fulfill the if condition, I place exit(1) to stop after checking if condition but it is not a good idea as the loop breaks. And I am not be able to insert the opposite if condition to off the LED when the capacitive is touched. The LED is now able to turn on when endTimer > longPressTime and will not turn on if endTimer < longPressTime.

Thank you. Appreciate your help.

#include <MedianFilter.h>
#include <CapacitiveSensor.h>

MedianFilter test(20, 0);

CapacitiveSensor capSensor = CapacitiveSensor(2, 11);

bool led = false;
int threshold = 200;
const int ledPin = 12;
unsigned long startTimer = 0;
long longPressTime = 5000;
bool hasStarted = false;
unsigned long endTimer;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  long sensorValue = capSensor.capacitiveSensor(30);
  test.in(sensorValue);
  sensorValue = test.out();

  Serial.println (sensorValue);
  delay(250);

  if (sensorValue > threshold && hasStarted == false)
  {
    startTimer = millis();
    hasStarted = true;
    Serial.println("Start");
  }

  if (sensorValue < threshold & hasStarted == true) {
    endTimer = millis() - startTimer;
    Serial.print("EndTimer : ");
    Serial.println(endTimer);

    if (endTimer > longPressTime) {
      digitalWrite(ledPin, HIGH);
      delay(500);
      exit(1);
    }
    else {
      delay(500);
      exit(1);
    }
  }
}

When the button becomes pressed save the value of millis() as the start time and set a boolean to true. If the button becomes released set the boolean to false

Separately in loop(), if the boolean is true and the current value of millis() minus the start time is equal to or greater than the required period then the button has been pressed for the required time

Hi UKHelibob..Thank you for the hint! I think I solved it for now! Able to on off with new millis() counting values without breaking the loop. Thank you, appreciate your help.

Hi, to further automate the LED, is it possible to put iteration to identify the elapsed time has exceeded press limit and turn on the LED even we have not released the finger from the capacitive touch?

, is it possible to put iteration to identify the elapsed time has exceeded press limit and turn on the LED even we have not released the finger from the capacitive touch?

Yes, as long as you have written the code correctly, but it is impossible to say unless/until you post your current code.

If you have written it correctly then you will know when the sensor became pressed and how long has passed since then so if the sensor is currently pressed and the required time has passed then do what you want/need to do

to make this simple as possible. you need to change standard logic. you need make things happen when when the button changes to an "UP" state instead of making things happen when the button state is changed to "Down".

when your state changes to down you mark a timer. when the button changes to up you compair the timer to the current time to get the amount of time it was down and do stuff.

int pinnumber;
int cur;
int last;
unsigned long timer;
void setup() {
Serial.begin(9600);
pinnumber=SomePinNumber!!!
}

void loop() {
 cur = digitalRead(pinnumber);
 if(cur!=last){
 if(cur == 1){timer=millis();}
 if(cur == 0){Serial.println(String(millis()-timer));timer=0;}
 last = cur;
 }
}