我正在为一个自动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永久开启,液晶显示器产生两个实体块,没有其他任何东西,而伺服系统一瘸一拐,当我按下按钮什么也不改变。我认为问题就在设置中,如果没有帮助的话。
发布于 2019-03-30 00:06:21
在程序中调用函数: check()。在5秒的延迟中,在循环中还有另一个5s延迟。可能是你总是在延迟的时候按下按钮。然后,程序将不会注意到按钮被按下,并继续循环。这也解释了为什么led不会关闭,因为它马上又打开了。要测试是否是这种情况,只需按住按钮至少10s和sea,如果它有效的话。如果是这样的话,要解决这个问题,请尝试使用一个中断。参见这里的文档:https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
https://stackoverflow.com/questions/55426842
复制相似问题