首页 > 解决方案 > 在具有多个按钮的 arduino 中一次使用一个按钮

问题描述

我编写了一个代码,我可以使用我的 arduino 一次使用多个按钮。但是,我现在想要修改代码,以便在按下一个按钮时,在第一个按钮被释放之前,其他按钮都不能工作。

谁能帮我解决这个问题?代码如下:

#include <ezButton.h>
#include <SoftwareSerial.h>


ezButton button1 (22);
ezButton button2 (24);
ezButton button3 (26);
ezButton button4 (28);
ezButton button5 (30);


SoftwareSerial espSerial(18, 19);
String str;
int Myval = 255;


void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
espSerial.begin(9600);
button1.setDebounceTime(50);
button2.setDebounceTime(50);
button3.setDebounceTime(50);
button4.setDebounceTime(50);
button5.setDebounceTime(50);


} 

void loop() {
// put your main code here, to run repeatedly:
button1.loop();
button2.loop();
button3.loop();
button4.loop();
button5.loop();


int btn1S = button1.getState();
int btn2S = button2.getState();
int btn3S = button3.getState();
int btn4S = button4.getState();
int btn5S = button5.getState();

Serial.print("button 1 state: ");
Serial.println(btn1S);
Serial.print("button 2 state: ");
Serial.println(btn2S);
Serial.print("button 3 state: ");
Serial.println(btn3S);
Serial.print("button 4 state: ");
Serial.println(btn4S);
Serial.print("button 5 state: ");
Serial.println(btn5S);
  
if(button1.isPressed())
Serial.println("Fucntion for 22 is now on");

if(button1.isReleased())
Serial.println("Function for 22 is now off");

if(button2.isPressed())
Serial.println("Fucntion for 24 is now on");

if(button2.isReleased())
Serial.println("Function for 24 is now off");

if(button3.isPressed())
Serial.println("Fucntion for 26 is now on");

if(button3.isReleased())
Serial.println("Function for 26 is now off");

if(button4.isPressed())
Serial.println("Fucntion for 28 is now on");

if(button4.isReleased())
Serial.println("Function for 28 is now off");

if(button5.isPressed())
Serial.println("Fucntion for 30 is now on");

if(button5.isReleased())
Serial.println("Function for 30 is now off");

str = String("Coming from arduino: ")+String("Button 1 is now: ")+String(btn1S)+String("Button 2 is  
now: ")+String(btn2S)+String("Button 3 is now: ")+String(btn3S)+String("Button 4 is now: 
")+String(btn4S)+String("Button 5 is now: ")+String(btn5S);  
 espSerial.println(str);


 delay(1000);
}

代码正在运行,但唯一剩下的就是我想提出的条件。谁能做修改或告诉我怎么做?

标签: arduinonodemcu

解决方案


也许通过使用设置为 false 的布尔值可以工作。释放按钮时将布尔值设置为 true/1。


推荐阅读