2016-02-23 37 views
-1

我想在LCD 16x2上顯示millis,但問題是在添加MySQL插入代碼後,計數器不會從零開始。實際上,有時在服務器連接4秒,40秒和79秒後開始連接。液晶顯示器上的Arduino Millis

我使用的Arduino類型是:「Arduino Uno」, 和Arduino IDE版本是1.6.7。

誰能幫我解決這個問題,這裏是代碼:

#include <Ethernet.h> 
#include <LiquidCrystal.h> 
#include <MySQL_Connection.h> 
#include <MySQL_Cursor.h> 

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 

IPAddress server_addr(); // IP of the MySQL *server* here 

char user[] = "";    // MySQL user login username 
char password[] = "";  // MySQL user login password 


EthernetClient client; 
MySQL_Connection conn((Client *)&client); 

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 

void setup() { 

// set up the LCD's number of columns and rows: 
lcd.begin(16, 2); 

// Print a message to the LCD. 
lcd.print("Millis"); 

Serial.begin(115200); 
while (!Serial); // wait for serial port to connect 
Ethernet.begin(mac_addr); 
Serial.println("Connecting..."); 
if (conn.connect(server_addr, 3306, user, password)) { 
delay(1000); 
} 

else 
Serial.println("Connection failed."); 
} 


void loop() { 

unsigned long var =(millis()/1000); 

if(var%40 == 0) 
{ 

char INSERT_SQL[] = "UPDATE Information.total SET reading=(%d) WHERE 
Name='Sam';"; 
char query[255]; 
sprintf(query, INSERT_SQL, var); 
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); 
cur_mem->execute(query); 

delete cur_mem; 

} 

lcd.setCursor(0, 1); 
lcd.print(var); 

} 

而且,這裏是其他代碼,其中米利斯從零開始(插​​入MySQL的代碼插入前):

#include <LiquidCrystal.h> 

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 

void setup() { 
lcd.begin(16, 2); 
Serial.begin(9600); 
lcd.print("Millis"); 

} 

void loop() { 
unsigned long var =(millis()/1000); 
var = (var==0)?1:var; 
if(var%40 == 0) 
{ 
Serial.println("Millis: "); 
Serial.println(var); 
    } 
lcd.setCursor(0, 1); 
lcd.print(millis()/1000); 
delay(1000); 
} 

所有答案都讚賞...

+0

您可以通過包含從零開始的代碼來編輯您的文章嗎? –

+0

您是否試圖每40毫秒更新一次數據庫? –

+0

你可以讓millis顯示,並且每次都從零開始,或者至少從你開始顯示後的時間開始? –

回答

0

米里斯是毫秒數,因爲該程序開始Arduino的

我會很驚訝,如果它是零(除非翻轉)

+0

感謝您的重播,實際上它之前是好的,從零開始就是明星。 –

+0

@Someone,這個答案不能解決問題中提到的問題。請考慮詳細說明你的答案。否則,評論會更合適。 –

+0

@Someone,我試圖每隔40毫秒將數值發送到數據庫,同時millis會不斷顯示在LCD上。 –

0

Millis()給你從設備開機後經過的毫秒量。它給你一個unsigned long。那麼如果達到了unsigned longHigh會發生什麼?...它翻轉並再次從零開始(請牢記這一點)。

因此,您有幾個選項可以顯示從零開始的毫秒顯示;

  1. 如果使用millis(),那麼你必須有一個保持第一個millis()你問一個變量。然後在每個循環中再次獲得millis()並從最新的一個扣除第一個millis()。你最終的實際數額已經過去了。訣竅是記住它可以翻轉,你必須做好準備。

  2. 使用像elapsedMillis這樣的第三方庫,它實際上給出了自定時器初始化爲零後經過的時間(以毫秒爲單位)。通過搜索很容易找到。

這裏是1點的例子:

#include <limits.h> 

unsigned long FirstMillis; 

void setup() { 
    // The value assigning you can put wherever you want when you want 
    // to start the initialization just remember that you can only 
    // declare the variable once otherwise it will be overwritten each time. 
    // Also do not assign on each loop without checking whether it is time 
    // to assign 
    FirstMillis = millis(); 
} 

void loop() { 
    unsigned long LatestMillis = millis(); 
    unsigned long TimeElapsed = 0; 

    // Check for overflow. I would assume that you will have restarted 
    // the timing again before the full range of FirstMillis + ULONG_MAX has been 
    // depleted. If so overflow will only happen once. ULONG_MAX 
    // in milliseconds is longer than 49 days. 
    if (LatestMillis < FirstMillis) 
    TimeElapsed = (ULONG_MAX - FirstMillis) + LatestMillis; 
    else 
    TimeElapsed = LatestMillis - FirstMillis; 

lcd.print(TimeElapsed); 
} 
0

假設MySQL是需要一些時間來建立連接,你只需要在那之後計算所經過的時間。我在代碼中添加了一個函數elapsedTime,該代碼計算連接建立後經過的秒數。如果您打算讓您的板保持超過49天您需要考慮@Blurry的建議。有關該問題的其他詳細信息see this link

更新代碼:

#include <Ethernet.h> 
#include <LiquidCrystal.h> 
#include <MySQL_Connection.h> 
#include <MySQL_Cursor.h> 

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 

IPAddress server_addr(); // IP of the MySQL *server* here 

char user[] = "";   // MySQL user login username 
char password[] = "";  // MySQL user login password 

EthernetClient client; 
MySQL_Connection conn((Client *)&client); 

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 

unsigned long initialTime; 

void setup() { 

    // set up the LCD's number of columns and rows: 
    lcd.begin(16, 2); 

    // Print a message to the LCD. 
    lcd.print("Millis"); 

    Serial.begin(115200); 
    while (!Serial); // wait for serial port to connect 
    Ethernet.begin(mac_addr); 
    Serial.println("Connecting..."); 
    if (conn.connect(server_addr, 3306, user, password)) { 
     delay(1000); 
    } else { 
     Serial.println("Connection failed."); 
    } 

    // Initial value of millis() after connection. 
    initialTime = millis()/1000; 
} 

unsigned long elapsedSeconds() { 
    return (millis()-initialTime)/1000; 
} 

void loop() { 
    unsigned long var = elapsedSeconds(); 
    lcd.setCursor(0, 1); 
    lcd.print(var); 

    if(var%40 == 0) { 
     char INSERT_SQL[] = 
      "UPDATE Information.total SET reading=(%d) WHERE Name='Sam';"; 
     char query[255]; 
     sprintf(query, INSERT_SQL, var); 
     MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); 
     cur_mem->execute(query); 
     delete cur_mem; 
    } 
} 

預計在液晶顯示器上輸出:

假設在循環功能的查詢時間不到一秒鐘,你應該看到下面的輸出:0 1 2 3 4 5 6 7...。但是,如果查詢需要幾秒鐘,例如10秒鐘後,您應該看到類似0 10 20 30...的輸出或略有不同。在這種情況下,我會說使用這種特殊方法不可能做到你想要的。

+0

@Artlon,感謝您的努力,但我仍然得到相同的結果!也許這是一些與端口號或DHCP響應有關的問題。 –

+0

@Noorallah,你在LCD中獲得了什麼輸出? –

+0

@Artlon,原始代碼的結果相同。 –