未正确检测到按钮信号
Push Button signal not detected properly
我有 6 个不同的 LED 连接到引脚 2 到 7,一个按钮连接到引脚 8。我有两个功能,一个以一秒的间隔打开和关闭 LED,另一个打开和关闭彩色 LED模式。当我按下按钮时,我试图在这两种模式之间切换。但它没有按照我想要的方式工作。我必须按住按钮直到所有 LED 灯都熄灭才能切换模式。我希望我的 led 在我按下按钮后立即切换模式。
我尝试过当引脚 8(按钮)处于高电平时循环切换模式,但我得到的结果相同,我必须按住才能使其工作。
这是我的代码:
//BUTTON STATE
int state = 0;
bool currentState = LOW;
bool lastState = LOW;
void setup() {
// put your setup code here, to run once:
for(int pin = 2;pin <8; pin++){//DECLARE PIN FOR OUTPUT FROM PIN 2 TO 7
pinMode(pin,OUTPUT);
pinMode(8,INPUT);
}
}
bool buttonState(bool last){
bool current = digitalRead(8);
if(last != current){
current = digitalRead(8);
}
return current;
}
void led_dance(int mode){
if(mode == 0 && digitalRead(8) != HIGH){
mode_sequeal();
}
else{
mode_color();
}
}
void mode_sequeal(){//TURN ON LED IN SERIES AND TURN IT OFF IN SERIES
for(int led = 2;led <8; led++){//FOR TURNING LED ON
digitalWrite(led, HIGH);//TURNS LED ON
delay(1000);//DELAY FOR 1 SECONDS
}
for(int led = 2;led <8; led++){//FOR TURNING LED OFF
digitalWrite(led, LOW);
delay(1000);//DELAY FOR 1 SECONDS
}
}
void mode_color(){
digitalWrite(2,HIGH);
digitalWrite(5,HIGH);
delay(500);
digitalWrite(2,LOW);
digitalWrite(5,LOW);
digitalWrite(3,HIGH);
digitalWrite(7,HIGH);
delay(500);
digitalWrite(3,LOW);
digitalWrite(7,LOW);
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
delay(500);
digitalWrite(4,LOW);
digitalWrite(6,LOW);
}
void loop() {
// put your main code here, to run repeatedly:
currentState = buttonState(lastState);
if(lastState == LOW && currentState == HIGH){
state++;
}
lastState = currentState;
if(state == 2){
state = 0;
}
led_dance(state);
}
您的问题是您仅在 led_dance
完成后才阅读您的按钮。
您有两个选择:
方案一:你可以让代码更杂乱,像这样在每次延迟前后添加阅读状态按钮:
int buttonChanged()
{
// Put logic here to check button state and save it according to your wishes, for example:
currentState = buttonState(lastState);
if(lastState == LOW && currentState == HIGH)
{
state++;
}
int stateChanged = lastState != currentState;
lastState = currentState;
if(state == 2)
{
state = 0;
}
return stateChanged;
}
void mode_sequeal()
{
//TURN ON LED IN SERIES AND TURN IT OFF IN SERIES
for(int led = 2; led <8; led++)
{
//FOR TURNING LED ON
digitalWrite(led, HIGH);//TURNS LED ON
if (buttonChanged())
{
return;
}
delay(1000);//DELAY FOR 1 SECONDS
if (buttonChanged())
{
return;
}
}
for(int led = 2;led < 8; led++)
{
//FOR TURNING LED OFF
if (buttonChanged())
{
return;
}
digitalWrite(led, LOW);
delay(1000);//DELAY FOR 1 SECONDS
if (buttonChanged())
{
return;
}
}
}
相应地更改其他功能。
选项二:让您的实施事件基于定时器库:
http://playground.arduino.cc/Code/Timer
我会把它的实现留给你,因为我的环境没有正确设置来测试代码。
提示:查看两个计时器示例,您需要一个用于 LED 的计时器,另一个用于检查按钮。
编辑:
最后说明:基于事件的实现更难编写和理解,但最终代码更清晰、更易读,并且确实希望您想要更好。不时检查按钮会减慢应用程序的速度,并且仍有 1 秒的延迟,但编写和理解起来要简单得多。
我建议您同时尝试这两种实现,并比较两种实现的结果文件和板的行为。我保证这将是最有教育意义的。
我有 6 个不同的 LED 连接到引脚 2 到 7,一个按钮连接到引脚 8。我有两个功能,一个以一秒的间隔打开和关闭 LED,另一个打开和关闭彩色 LED模式。当我按下按钮时,我试图在这两种模式之间切换。但它没有按照我想要的方式工作。我必须按住按钮直到所有 LED 灯都熄灭才能切换模式。我希望我的 led 在我按下按钮后立即切换模式。
我尝试过当引脚 8(按钮)处于高电平时循环切换模式,但我得到的结果相同,我必须按住才能使其工作。
这是我的代码:
//BUTTON STATE
int state = 0;
bool currentState = LOW;
bool lastState = LOW;
void setup() {
// put your setup code here, to run once:
for(int pin = 2;pin <8; pin++){//DECLARE PIN FOR OUTPUT FROM PIN 2 TO 7
pinMode(pin,OUTPUT);
pinMode(8,INPUT);
}
}
bool buttonState(bool last){
bool current = digitalRead(8);
if(last != current){
current = digitalRead(8);
}
return current;
}
void led_dance(int mode){
if(mode == 0 && digitalRead(8) != HIGH){
mode_sequeal();
}
else{
mode_color();
}
}
void mode_sequeal(){//TURN ON LED IN SERIES AND TURN IT OFF IN SERIES
for(int led = 2;led <8; led++){//FOR TURNING LED ON
digitalWrite(led, HIGH);//TURNS LED ON
delay(1000);//DELAY FOR 1 SECONDS
}
for(int led = 2;led <8; led++){//FOR TURNING LED OFF
digitalWrite(led, LOW);
delay(1000);//DELAY FOR 1 SECONDS
}
}
void mode_color(){
digitalWrite(2,HIGH);
digitalWrite(5,HIGH);
delay(500);
digitalWrite(2,LOW);
digitalWrite(5,LOW);
digitalWrite(3,HIGH);
digitalWrite(7,HIGH);
delay(500);
digitalWrite(3,LOW);
digitalWrite(7,LOW);
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
delay(500);
digitalWrite(4,LOW);
digitalWrite(6,LOW);
}
void loop() {
// put your main code here, to run repeatedly:
currentState = buttonState(lastState);
if(lastState == LOW && currentState == HIGH){
state++;
}
lastState = currentState;
if(state == 2){
state = 0;
}
led_dance(state);
}
您的问题是您仅在 led_dance
完成后才阅读您的按钮。
您有两个选择:
方案一:你可以让代码更杂乱,像这样在每次延迟前后添加阅读状态按钮:
int buttonChanged()
{
// Put logic here to check button state and save it according to your wishes, for example:
currentState = buttonState(lastState);
if(lastState == LOW && currentState == HIGH)
{
state++;
}
int stateChanged = lastState != currentState;
lastState = currentState;
if(state == 2)
{
state = 0;
}
return stateChanged;
}
void mode_sequeal()
{
//TURN ON LED IN SERIES AND TURN IT OFF IN SERIES
for(int led = 2; led <8; led++)
{
//FOR TURNING LED ON
digitalWrite(led, HIGH);//TURNS LED ON
if (buttonChanged())
{
return;
}
delay(1000);//DELAY FOR 1 SECONDS
if (buttonChanged())
{
return;
}
}
for(int led = 2;led < 8; led++)
{
//FOR TURNING LED OFF
if (buttonChanged())
{
return;
}
digitalWrite(led, LOW);
delay(1000);//DELAY FOR 1 SECONDS
if (buttonChanged())
{
return;
}
}
}
相应地更改其他功能。
选项二:让您的实施事件基于定时器库:
http://playground.arduino.cc/Code/Timer
我会把它的实现留给你,因为我的环境没有正确设置来测试代码。
提示:查看两个计时器示例,您需要一个用于 LED 的计时器,另一个用于检查按钮。
编辑:
最后说明:基于事件的实现更难编写和理解,但最终代码更清晰、更易读,并且确实希望您想要更好。不时检查按钮会减慢应用程序的速度,并且仍有 1 秒的延迟,但编写和理解起来要简单得多。
我建议您同时尝试这两种实现,并比较两种实现的结果文件和板的行为。我保证这将是最有教育意义的。