如果我在中断的情况下按两次按钮,我该如何告诉 Arduino 中的代码?
How do I tell the code in Arduino if I press a button twice with interrupts?
我在 Arduino 中使用 esp32。我想做的是:
如果我按一次按钮,它应该 Serial.print "I was pressed once"
如果我按两次按钮,它应该 Serial.print "I was pressed twice"
我正在使用 attachInterrupt() 函数,但是当我按下按钮两次时,我不知道如何告诉代码如何读取它。
我的代码还做的是当它感觉到我按下按钮时打开 LED。
这是我到目前为止所取得的成就:
int boton = 0;
int led = 5;
int valorBoton; //value of the button, if it off(1) or on (0)
unsigned int count = 0 ; //counter
void setup() {
Serial.begin(115200); //velocity
pinMode(led, OUTPUT); //OUTPUT LED
pinMode(boton, INPUT); //INFUPT BUTTON
digitalWrite(led, LOW); //THE LED IS LOW INITIALLY
attachInterrupt(digitalPinToInterrupt(0),button1,RISING);
}
void loop() {
count++;
Serial.println(count); //printing the counter
delay(1000);
}
void button1(){ //the function button1 is a parameter of attachInterrupt
digitalWrite(led, HIGH); //when it is pressed, led is on
Serial.println("I was pressed");
count = 0; // if I was pressed, then the count starts from cero all over again
}
我希望在按下按钮时打印 Serial.println("I was pressed twice")
它可以通过多种方式实现。一种方法是创建一个中断函数来增加一个计数器,然后在循环函数中检查用户是否按下了该函数两次(通过计算按下之间的延迟)然后决定是按下一次还是两次。
记得更改 max_delay
两次按下之间的最长等待时间。
// maximum allowed delay between two presses
const int max_delay = 500;
int counter = 0;
bool done = false;
const byte ledPin = 13;
const byte buttonPin = 0;
unsigned long first_pressed_millis = 0;
void counter_incr()
{
counter++;
}
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT); //OUTPUT LED
pinMode(buttonPin, INPUT_PULLUP); //INPUT BUTTON as pullup
digitalWrite(ledPin, LOW); //THE LED IS LOW INITIALLY
attachInterrupt(digitalPinToInterrupt(buttonPin), counter_incr, RISING);
}
void loop()
{
if (counter > 0)
{
first_pressed_millis = millis();
// wait for user to press the button again
while (millis() - first_pressed_millis < max_delay)
{
// if button pressed again
if (counter > 1)
{
Serial.println("Button pressed twice!");
done = true;
break;
}
}
// if on timeout no button pressed it means the button pressed only one time
if (!done)
Serial.println("Button pressed once!");
counter = 0;
}
}
我在 Arduino 中使用 esp32。我想做的是: 如果我按一次按钮,它应该 Serial.print "I was pressed once" 如果我按两次按钮,它应该 Serial.print "I was pressed twice"
我正在使用 attachInterrupt() 函数,但是当我按下按钮两次时,我不知道如何告诉代码如何读取它。 我的代码还做的是当它感觉到我按下按钮时打开 LED。
这是我到目前为止所取得的成就:
int boton = 0;
int led = 5;
int valorBoton; //value of the button, if it off(1) or on (0)
unsigned int count = 0 ; //counter
void setup() {
Serial.begin(115200); //velocity
pinMode(led, OUTPUT); //OUTPUT LED
pinMode(boton, INPUT); //INFUPT BUTTON
digitalWrite(led, LOW); //THE LED IS LOW INITIALLY
attachInterrupt(digitalPinToInterrupt(0),button1,RISING);
}
void loop() {
count++;
Serial.println(count); //printing the counter
delay(1000);
}
void button1(){ //the function button1 is a parameter of attachInterrupt
digitalWrite(led, HIGH); //when it is pressed, led is on
Serial.println("I was pressed");
count = 0; // if I was pressed, then the count starts from cero all over again
}
我希望在按下按钮时打印 Serial.println("I was pressed twice")
它可以通过多种方式实现。一种方法是创建一个中断函数来增加一个计数器,然后在循环函数中检查用户是否按下了该函数两次(通过计算按下之间的延迟)然后决定是按下一次还是两次。
记得更改 max_delay
两次按下之间的最长等待时间。
// maximum allowed delay between two presses
const int max_delay = 500;
int counter = 0;
bool done = false;
const byte ledPin = 13;
const byte buttonPin = 0;
unsigned long first_pressed_millis = 0;
void counter_incr()
{
counter++;
}
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT); //OUTPUT LED
pinMode(buttonPin, INPUT_PULLUP); //INPUT BUTTON as pullup
digitalWrite(ledPin, LOW); //THE LED IS LOW INITIALLY
attachInterrupt(digitalPinToInterrupt(buttonPin), counter_incr, RISING);
}
void loop()
{
if (counter > 0)
{
first_pressed_millis = millis();
// wait for user to press the button again
while (millis() - first_pressed_millis < max_delay)
{
// if button pressed again
if (counter > 1)
{
Serial.println("Button pressed twice!");
done = true;
break;
}
}
// if on timeout no button pressed it means the button pressed only one time
if (!done)
Serial.println("Button pressed once!");
counter = 0;
}
}