2012-12-03 73 views
1

該程序的目標是向Arduino的LCD寫入一個字符串,並在兩個不同的消息之間循環。我目前版本的問題在於,它會毫不遲延地來回循環。我將如何獲得這些一次寫一個?使用Millis的Arduino Sketch Loop()

下面是代碼,我留下了一些不相關的部分組成:

#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"); 
     } 
     } 
     } 
    } 

回答

2

這個條件if ((timer + 5000) > millis() < (timer + 10000))在C中沒有意義 - 至少它不會做你期待的。

它被調用象下面這樣:

  • 第一(timer + 5000) > millis()被調用,它的值是0或1
  • 下一個0或1(從第一條件)與(timer + 10000)這始終是真的(假設相比你有沒有時間溢出值,你是不是有大的負數比較)

你應該使用類似IF((定時器+ 5000)>米利斯()& &廠()<(定時器+ 10000))或相當:

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

由於通過millis()返回時間將每個之間變化,如果條件檢查英寸

+0

謝謝,這工作。 – user1739123

0

你試過設置定時器=米利斯();到循環之外?