我正在使用3种不同的电容式触摸传感器构建数字小键盘。因此,我的功能是为3个触摸传感器分别贴上不同的编号,以创建密码系统。
但是我意识到,每当我尝试按该按钮以键入数字时,例如标记有数字“ 3”的按钮,即使我只按了一次,仍会出现3的多个值。我可以使用arduino上的任何功能,使其仅在1次印刷中产生1个结果,而不是一次印刷中产生多个结果。
无论如何,我是否可以更改代码,以便可以为每个按钮创建某种反跳,所以当我按下按钮一次时,只会显示1个结果。
非常感谢您对我的代码进行编辑,谢谢!
#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: ");
}
}
}
答案 0 :(得分:1)
就像其他按钮一样将其反跳。仅在一段时间后才能接受信号更改。
来自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;
}
答案 1 :(得分:0)
您可以这样使用 INPUT_PULLUP :
void setup() {
pinMode(pin, INPUT_PULLUP)
}
但是由于您使用的是i库,这可能是一个问题。
答案 2 :(得分:0)
您可以简单地使用delay()函数,如此处所述:https://www.brainy-bits.com/arduino-switch-debounce/
这可能是最快,最简单的方法。
弹跳该按钮会有所帮助-Adafruit在此主题上有一些特别之处:https://learn.adafruit.com/make-it-switch/debouncing
但是对于快速而肮脏的解决方案,delay()可以。