液晶显示器上的Arduino Millis

时间:2016-02-23 16:26:46

标签: networking port router arduino-uno dhcp

我想在LCD 16x2上显示millis,但问题是在我添加MySQL插入代码后计数器不会从零开始。实际上,有时与服务器的连接在4秒,40秒,有时79秒后开始。

我使用的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);

}

并且,以下是millis从Zero开始的其他代码(在插入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);
 }

感谢所有答案......

3 个答案:

答案 0 :(得分:0)

Millis是自该程序在arduino

上启动以来的毫秒数

如果它永远为零,我会感到惊讶(除非被翻过来)

答案 1 :(得分:0)

Millis()为您提供自设备开机以来经过的毫秒数。它会为您提供unsigned long。那么如果达到High的{​​{1}}会发生什么呢?......它会翻身并再次从零开始(记住这一点)。

因此,您有几个选项可以显示从零开始的毫秒显示;

  1. 如果您使用unsigned long,那么您必须拥有一个变量,以保留您要求的第一个millis()。然后在每个循环中再次获取millis()并从最新的millis()中扣除第一个millis()。你最终得到了实际的金额。诀窍是要记住它可以翻滚,你必须准备好。

  2. 使用像elapsedMillis这样的第三方库,它实际上提供了自您将计时器初始化为零以来经过的时间(以毫秒为单位)。通过搜索很容易找到。

  3. 以下是第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);
    }
    

答案 2 :(得分:0)

假设MySQL需要一些时间来建立连接,您需要在此之后计算经过的时间。我在您的代码中添加了一个函数elapsedTime,用于计算建立连接后经过的秒数。如果您计划将电路板保持超过49天,则需要考虑@ Blurry&#39;咨询。有关该问题的其他详细信息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;
   }  
}

LCD上的预期输出:

假设循环函数中的查询不到一秒钟,您应该看到以下输出:0 1 2 3 4 5 6 7...。但是,如果查询需要几秒钟,例如10秒,您应该会看到0 10 20 30...或稍微不同的输出。在这种情况下,我会说使用这种特殊方法不可能做你想做的事。