该程序的目标是向 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");
}
}
}
}