我和我的团队目前正在负责一台自动售货机。我们现在使用的编码如下,但它没有产生正确的结果来显示硬币接受器中插入的硬币数量
#include
#include
LiquidCrystal_I2C 液晶显示器(0x27、16、2);
int coinPin = 2;
float onePesoValue = 1.00;
float fivePesoValue = 5.00;
float tenPesoValue = 10.00;
浮动总金额 = 0.00;
无效设置(){
液晶显示器开始();
液晶背光();
液晶显示器();
lcd.setCursor(3,0);
lcd.print("WATER VENDING");
lcd.setCursor(4,3);
液晶打印(“机器”);
延迟(3000);
液晶显示器();
// 设置 para sa coinpin
pinMode(coinPin, INPUT_PULLUP);
// 设置 para sa Serial Monitor
Serial.begin(9600);
}
无效循环(){
液晶显示器开始();
液晶背光();
液晶显示器();
lcd.setCursor(3,0);
lcd.print("欢迎来到");
lcd.setCursor(4,3);
lcd.print("AQUACOIN");
延迟(3000);
液晶显示器();
pinMode (coinPin, INPUT_PULLUP);
lcd.setCursor(3,0);
lcd.print("选择类型");
lcd.setCursor(4,3);
lcd.print(“水的”);
延迟(3000);
液晶显示器();
// 用户投币
lcd.setCursor(2,0);
lcd.print("请插入");
lcd.setCursor(3,3);
lcd.print(“硬币!!!”);
延迟(3000);
液晶显示器();
// 检查 kung anong pera ang hinulog
如果(digitalRead(coinPin)== LOW){
延迟(50);
int duration = pulseIn(coinPin, HIGH);
如果(持续时间 >= 35 && 持续时间 <= 45) { // Peso coin
totalAmount += onePesoValue;
} else if(持续时间 >= 75 && 持续时间 <= 85) { // limang Peso coin
totalAmount += fivePesoValue;
} else if (持续时间 >= 110 && 持续时间 <= 120) { // sampung Peso coin
totalAmount += tenPesoValue;
}
// 显示用餐量 sa lcd
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PHP");
lcd.print(totalAmount, 2);
//显示饭量 sa serial monitor
Serial.print("总金额:PHP");
Serial.println(totalAmount, 2);
}
}
我们已经测试了代码,串行监视器显示的数量不正确。
它显示不正确的数量,并且每个硬币掉落都有明显的延迟。