我无法完全理解为什么该程序不起作用 - 液晶屏问题等等



我正在为自动Malteser(或类似产品)分配器做一个项目,我得到了代码来工作,但是在放置了一会儿后,屏幕播放了,它将无法正常工作。LED 也始终亮起,并且似乎可以从设置中移开

我试过:

  • 在代码中搜索故障
  • 检查接线
  • 重做接线以防我错过了什么

法典:

#include <Servo.h>
#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>
DS3231 Clock;
bool Century = false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
int second, minute, hour, date, month, year, temp;
int button = 8;
int maltesersEaten = 0;
int lastEatenSe = 0;
int lastEatenMi = 0;
int lastEatenHo = 0;
int lastEatenDa = 0;
int lastEatenMo = 0;
int lastEatenYe = 0;
bool eat;
int ledPin = 10;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myServo;
byte degree[8] = {
B00100,
B01010,
B00100,
B00000,
B00000,
B00000,
B00000,
};
void setup() {
**lcd.createChar(0, degree);
lcd.begin(16, 2);  
// Print a message to the LCD
lcd.print("INITIALIZING");
// Start the I2C interface
Wire.begin();
// Start the serial interface
Serial.begin(9600);
pinMode(button, INPUT);
myServo.attach(9);**
myServo.write(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(1000);
lcd.clear();
}
void getData() {
second = Clock.getSecond();
minute = Clock.getMinute();
hour = Clock.getHour(h12, PM);
date = Clock.getDate();
month = Clock.getMonth(Century), DEC;
year = Clock.getYear();
temp = Clock.getTemperature();
lcd.setCursor(0, 0);
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);
lcd.setCursor(0, 1);
lcd.print(date);
lcd.print(",");
lcd.print(month);
lcd.print(",");
lcd.print(year);
lcd.setCursor(9, 0);
lcd.print(temp);
lcd.write(byte(0));
lcd.print("C");
lcd.setCursor(9,1);
lcd.print("ATE:");
lcd.print(maltesersEaten);
}
void mE() {
maltesersEaten = (maltesersEaten + 1);
lastEatenSe = second;
lastEatenMi = minute;
lastEatenHo = hour;
lastEatenDa = date;
lastEatenMo = month;
lastEatenYe = year;
}

void check() {
lcd.clear();
lcd.print("-Last Malteser-");
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);
lcd.setCursor(0, 1);
lcd.print("Eaten: ");
lcd.print(maltesersEaten);
delay(5000);
lcd.clear();
}
void loop() {
if (digitalRead(button) == LOW) {
digitalWrite(ledPin, HIGH);
myServo.write(170);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("---- Have A ----");
lcd.setCursor(0, 1);
lcd.print("    Malteser    ");
delay(5000);
myServo.write(10);
lcd.clear();
check();
mE();
digitalWrite(ledPin, LOW);
}
else if (digitalRead(button) == HIGH) {
getData();
}
}

所需的效果是屏幕产生一条消息,LED 闪烁,然后显示房间的时间、日期和温度,以及吃了多少马耳他人。 然后当按下按钮时,伺服移动,LED 亮起,LCD 通过几个不同的屏幕变化,然后它返回时间, 食用的马耳他人的日期、温度和数量。然而,实际结果是 LED 永久打开,LCD 产生两个实心块,没有别的,并且舵机一瘸一拐,当我按下按钮时没有任何变化。我认为问题出在设置中,如果这没有帮助的话。

在程序中调用函数:check()。在循环中有一个 5 秒的延迟,在循环中还有另一个 5 秒的延迟。可能是您在延迟打开时总是按下按钮。然后程序不会注意到按钮被按下并再次继续循环。这也可以解释为什么 LED 不会熄灭,因为它会立即再次打开。 要测试是否是这种情况,只需按住按钮至少 10 秒,然后海是否有效。 如果是这种情况,要解决此问题,请尝试使用中断。 请参阅此处的文档:https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

相关内容

最新更新