该程序的目标是将字符串写入Arduino的LCD,并在两个不同的消息之间循环。我当前版本的问题是它来回循环而没有延迟。我怎样才能让它们一次写一个?
这是代码,我遗漏了一些不相关的部分:
#include <LiquidCrystal.h>
#include <string.h>
// These are the pins our LCD uses.
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// initalize the lcd, and button input at zero.
int lcd_key = 0;
int adc_key_in = 0;
//define values for each button
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // detects the value from the buttons
// The buttons give values close to which values we saet them between.
if (adc_key_in > 1000) return btnNONE; // When the input is greater than 1000 that means no buttons are being pressed,
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // if there is some issue with values, the programs will not break.
}
void setup()
{
Serial.begin(9600); //Set the serial monitor.
lcd.begin(16, 2); //Set the LCD
}
void loop()
{
timer = millis();
if (left == true) //Right alignment
{
lcd.clear() ; //Clear any existing text
lcd.setCursor(5, 0); //Set cursor to right side.
timer = millis();
if (millis() < (timer + 5000)) {
if (show1 == true) //See if first line should be displayed. If false, nothing is displayed.
{
lcd.print("Time");
}
//Second line
lcd.setCursor(4, 1);
if (show2 == true)//See if second line should be displayed
{
lcd.print("12:00 PM");
}
}
if ((timer + 5000) > millis() < (timer + 10000)) {
//Display Date
lcd.setCursor(5, 0);
if (show1 == true)//See if first line should be displayed.
{
lcd.print("Date");
}
//Second line
lcd.setCursor(1, 1);
if (show2 == true)//See if second second should be displayed.
{
lcd.print("Nov. 16, 2012");
}
}
}
}
答案 0 :(得分:2)
这种情况if ((timer + 5000) > millis() < (timer + 10000))
在C语言中毫无意义 - 至少它没有达到你期望的效果。
如下所示调用:
(timer + 5000) > millis()
,其值为0或1 (timer + 10000)
进行比较,该int hlp_time = millis();
if ((timer + 5000) > hlp_time && hlp_time < (timer + 10000))
始终为真(假设您没有溢出时间值,并且您没有与较大的负数进行比较)你应该使用if((timer + 5000)&gt; millis()&amp;&amp; mills()&lt;(timer + 10000))或者更确切地说:
millis()
因为{{1}}返回的时间因条件而在每次签入时都会有所不同。
答案 1 :(得分:0)
您是否尝试过设置timer = millis();到了循环之外?