我有一个学校项目要做。这有点简单,我想说,如果你曾经玩过 Arduino。我没有。我要做一个闹钟,我做了时钟部分。问题出在警报上,我必须在其中使用按钮。 所以,我必须按下按钮,这样我才能进入“警报”选项。再次按下按钮返回“时钟”选项。
#define AlarmSND 6
#include <Adafruit_LiquidCrystal.h>
int hours = 0;
int minutes = 0;
int seconds = 0;
Adafruit_LiquidCrystal lcd_1(0);
const int buttonPin1 = 2;
const int ledPin1 = 13;
int buttonState1 = 0;
void setup()
{
lcd_1.begin(16, 2);
pinMode(buttonPin1, INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH) {
lcd_1.setCursor(0, 0);
lcd_1.print("Clock");
} else {
lcd_1.setCursor(0, 0);
lcd_1.print("Alarm");
delay(500);
}
}
我不明白我哪里错了,为什么我按下按钮时它没有进入“警报”选项?
我试过问chatgt,这是它附带的
if (buttonState == HIGH) {
delay(50);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
currentState = 1 - currentState;
if (currentState == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stare 1");
lcd.setCursor(0, 1);
lcd.print("push again");
} else {
lcd.clear();
lcd.print("push button");
}
}
理论上,我说应该可以吧?但它只是从选项菜单跳到选项警报,而我没有按下按钮。 我真的不明白我应该做什么。
我试过用他的方法,读取按钮状态,验证它,延迟一小段时间再读取按钮状态,但是这次它没有在液晶显示器上写任何东西。 我提到我使用 ThinkerCad,所以元素处于非常好的状态,并且我正确地将按钮连接到显示器。