1

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

2 回答 2

2

这种情况if ((timer + 5000) > millis() < (timer + 10000))在 C 中没有任何意义——至少它没有达到你的预期。

它的调用如下:

  • first(timer + 5000) > millis()被调用,其值为 0 或 1
  • 下一个 0 或 1(从第一个条件开始)与(timer + 10000)哪个始终为真进行比较(假设您没有溢出时间值并且您没有与大的负数进行比较)

你应该使用类似 if ((timer + 5000) > millis() && mills() < (timer + 10000)) 或者更确切地说:

int hlp_time = millis(); 
if ((timer + 5000) > hlp_time && hlp_time < (timer + 10000))

因为返回的时间millis()将在每次签入 if 条件之间有所不同。

于 2012-12-03T15:11:49.607 回答
0

您是否尝试过设置 timer = millis(); 到循环之外?

于 2012-12-03T14:41:10.347 回答