如何解决带按钮的Arduino变量未正确更新
因此,我要对Arduino进行的操作是使它从我的变量timestate(它的状态为0)开始,然后在按下按钮时,将1添加到变量中。 LCD应该显示我的变量,但是即使我确实按下按钮,数字也始终保持为0。我试过使按钮状态为HIGH,但是它只显示两个1而不是更改数字。
老实说,我不知道这是否是我无法正确编码变量,LCD或按钮的问题。我的代码出了什么问题?我是Arduino的新手,所以我现在很迷茫。
我的代码:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int button1Pin = 1;
void setup() {
pinMode(button1Pin,INPUT);
lcd.begin(16,2);
}
void loop() {
int button1State;
button1State = digitalRead(button1Pin);
int timestate;
timestate = 0;
lcd.setCursor(0,1);
//lcd.print(timestate);
if (button1State == LOW)
{
timestate = timestate + 1;
lcd.setCursor(0,1);
lcd.print(timestate);
if (timestate == 10)
{
timestate = 0;
}
else
{
lcd.setCursor(0,0);
lcd.print(timestate);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。