1

このプログラムの目的は、Arduino の LCD に文字列を書き込み、2 つの異なるメッセージを循環させることです。私の現在のバージョンの問題は、遅延なく前後に循環することです。これらを一度に1つずつ書き込むにはどうすればよいですか?

これがコードです。無関係な部分をいくつか省略しました。

  #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)は常に true と比較されます (オーバーフロー時間の値がなく、大きな負の数と比較していないと仮定します)

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 に答える