Setting delay for capacitive touch sensor to prevent "double press"

0

I am building a number numpad using 3 different capacitive touch sensor. So my function will be to label each the 3 touch sensors with a different number to create a password system.

However i realised that whenever i try to press the button to key in the number, for example the button tagged with number "3", multple values of 3 came out even though i only pressed once. Is there any function on arduino that i can use so that it only generate 1 results with 1 press instead of multiple results in a press.

Is there anyway to change my code such that i can create some sort of debounce for each button so that when i press the button once only 1 result will be shown.

Would appreciate an edit on my code, thanks!

#include <LiquidCrystal.h>
#include <CapacitiveSensor.h>
CapacitiveSensor cs_2_3= CapacitiveSensor(2,3);
CapacitiveSensor cs_2_4= CapacitiveSensor(2,4);
CapacitiveSensor cs_2_5= CapacitiveSensor(2,5);
int pos=0;
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);

char pass[]= "321";
int currentposition=0;
char code=0;

void setup() {
cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);
lcd.setCursor(0,0);
lcd.println("Enter Password: ");
Serial.begin(9600);
lcd.begin(16,2);

}

void loop() {
 long total1= cs_2_3.capacitiveSensor(100);
  long total2=cs_2_4.capacitiveSensor(100);
  long total3= cs_2_5. capacitiveSensor(100);
    if(total1>=1000)
  {
   code= '1';

  }

   if(total2>=1000)
  {
    code= '2';

  }


   if(total3>=1000)
  {
    code='3';

  } 
  delay(100);

if(currentposition==0)
{
lcd.setCursor(0,0);
lcd.println("Enter Password: ");
}
int lu ;
if(code!=0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PASSWORD:");
lcd.setCursor(7,1);
lcd.print(" ");
lcd.setCursor(7,1);

for(lu=0;lu<=currentposition;++lu)
{
lcd.print(code);
delay(25);
}

if (code==pass[currentposition])
{
++currentposition;

if(currentposition==3)
{

delay(900);

lcd.setCursor(0,0);
lcd.println(" ");
lcd.setCursor(1,0);
lcd.print("Correct Password");
lcd.setCursor(4,1);
lcd.println("HELLO!!");
lcd.setCursor(15,1);
lcd.println(" ");
lcd.setCursor(16,1);
lcd.println(" ");
lcd.setCursor(14,1);
lcd.println(" ");
lcd.setCursor(13,1);
lcd.println(" ");
delay (5000);



lcd.clear();
lcd.setCursor(0,0);
lcd.println("Enter Password: ");
}

}

else
{
delay(500);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("PASSWORD");
lcd.setCursor(6,0);
lcd.print("INCORRECT");
lcd.setCursor(15,1);
lcd.println(" ");
lcd.setCursor(4,1);
lcd.println("GET AWAY!!!");

code=0;
lcd.setCursor(13,1);
lcd.println(" ");
delay(3000);
currentposition=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Enter Password: ");

}
}
}
c++
arduino
asked on Stack Overflow Oct 19, 2019 by baddieskater • edited Oct 19, 2019 by baddieskater

3 Answers

1

Just debounce it like any other button. Only accept signal changes longer then a certain time.

From https://www.arduino.cc/en/tutorial/debounce

/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached from pin 2 to +5V
  - 10 kilohm resistor attached from pin 2 to ground

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}
answered on Stack Overflow Oct 20, 2019 by Piglet
0

You could use INPUT_PULLUP like this:

void setup() {
  pinMode(pin, INPUT_PULLUP)
}

But since your using i library this may be an issue.

answered on Stack Overflow Oct 21, 2019 by Haroun Hajem
0

You could simply use the delay() function, as stated here: https://www.brainy-bits.com/arduino-switch-debounce/

This might be the fastest and simplest way to do it.

Debouncing the button will help - Adafruit got something especially on this topic: https://learn.adafruit.com/make-it-switch/debouncing

But for a quick and dirty solution delay() will do.

answered on Stack Overflow Oct 22, 2019 by JohnnyAwesome

User contributions licensed under CC BY-SA 3.0