C 使用Millis()的Arduino草图循环
该程序的目标是将字符串写入Arduino的LCD,并在两条不同的消息之间循环。我当前版本的问题是它没有延迟地来回循环。我如何让它们一次写一个 下面是代码,我省略了一些不相关的部分:C 使用Millis()的Arduino草图循环,c,arduino,C,Arduino,该程序的目标是将字符串写入Arduino的LCD,并在两条不同的消息之间循环。我当前版本的问题是它没有延迟地来回循环。我如何让它们一次写一个 下面是代码,我省略了一些不相关的部分: #include <LiquidCrystal.h> #include <string.h> // These are the pins our LCD uses. LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // initalize th
#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");
}
}
}
}
#包括
#包括
//这些是我们LCD使用的引脚。
液晶显示器(8,9,4,5,6,7);
//初始化lcd,按钮输入为零。
int lcd_键=0;
int adc_key_in=0;
//为每个按钮定义值
#定义btnRIGHT 0
#定义btnUP 1
#定义btnDOWN 2
#定义btnLEFT 3
#定义b选择4
#定义btnNONE 5
//阅读按钮
int read_LCD_按钮()
{
adc_key_in=analogRead(0);//从按钮检测值
//按钮给出的值与我们设置的值接近。
如果(adc_key_in>1000)返回btnNONE;//当输入大于1000时,表示未按下任何按钮,
如果(adc_键输入<50)返回btnRIGHT;
如果(adc_key_in<195)返回btnUP;
如果(adc_键输入<380)返回btnDOWN;
如果(adc_key_in<555)返回btnLEFT;
如果(adc_键输入<790)返回b选择;
返回btnNONE;//如果值有问题,程序将不会中断。
}
无效设置()
{
Serial.begin(9600);//设置串行监视器。
lcd.begin(16,2);//设置lcd
}
void循环()
{
计时器=毫秒();
if(left==true)//右对齐
{
lcd.clear();//清除任何现有文本
lcd.setCursor(5,0);//将光标设置到右侧。
计时器=毫秒();
如果(毫秒()<(计时器+5000)){
if(show1==true)//查看是否应显示第一行。如果为false,则不显示任何内容。
{
lcd.打印(“时间”);
}
//第二线
lcd.setCursor(4,1);
if(show2==true)//查看是否应显示第二行
{
lcd.打印(“下午12:00”);
}
}
如果((计时器+5000)>毫秒()<(计时器+10000)){
//显示日期
lcd.setCursor(5,0);
if(show1==true)//查看是否应显示第一行。
{
lcd.打印(“日期”);
}
//第二线
lcd.setCursor(1,1);
if(show2==true)//查看是否应显示秒。
{
lcd.打印(“2012年11月16日”);
}
}
}
}
您是否尝试过设置timer=millis();要跳出循环?如果((timer+5000)>millis()<(timer+10000))这种情况在C中没有任何意义-至少它没有达到您期望的效果
它的调用方式如下所示:
- 首先调用
,其值为0或1(计时器+5000)>毫秒()
- 下一个0或1(从第一个条件开始)与
进行比较,该值始终为真(假设您没有溢出时间值,并且没有与大负数进行比较)(计时器+10000)
int hlp_time = millis();
if ((timer + 5000) > hlp_time && hlp_time < (timer + 10000))
int hlp_time=millis();
如果((计时器+5000)>hlp_时间和hlp_时间<(计时器+10000))
因为millis()
返回的时间在每个签入if条件下都会有所不同