Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3229|回復: 9
打印 上一主題 下一主題

void loop()中如何讓Sensor的更新頻率不同?

[複製鏈接]
跳轉到指定樓層
1#
發表於 2016-4-7 20:27:31 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
想詢問一下,我在void loop()中我想去設定溫度、濕度、土壤濕度、土壤電導度以及時間的擷取時間,目前我只有在所有程式的最後加上了delay(1000)去控制擷取時間,但是這樣會讓整個程式皆會以一秒的更新頻率去刷新SENSOR的數值,請問有無可以讓每種SENSOR皆有不同的更新頻率呢?
例如溫度是5秒一次、濕度是10秒一次、土壤濕度是30秒一次、土壤導電度是一天一次、時間就讓他1秒更新一次.....

感謝各位了

我真的很菜,再請求各位的幫助~
2#
發表於 2016-4-7 21:24:57 | 只看該作者
最簡單的方法,就是用變數去計數
int c0,c1,c2;
void loop(){
delay(1000);
if(++c0 >= 2){
c0 =0;
...
}
if(++c1 >= 5){
c1 =0;
...
}
if(++c2 >= 10){
c2 =0;
...
}
}
3#
 樓主| 發表於 2016-4-7 22:24:54 | 只看該作者
噎~有點看不太懂...
可否稍微再教導一下呢?
c0,c1,c2是指溫度、濕度、土壤濕度的數值對嗎?
您裡面的2,5,10...是指擷取的秒數嗎?
4#
發表於 2016-4-8 20:17:17 | 只看該作者
請在此貼出你的錯誤訊息
5#
發表於 2016-4-8 22:43:29 | 只看該作者
最簡單的方法,就是用變數去計數
int c0,c1,c2;
void loop(){
delay(1000);
if(++c0 >= 2){
c0 =0;
. ...
超新手 發表於 2016-4-7 21:24


超大的程式是每秒做三次if判斷,每次都會將裡面的c變數加1。如第一個if判斷前就會讓c0=c0+1,這樣到第2秒時,c0就會>=2,這樣就會去做if內指定要做的事。
同樣的到了第5秒,c1就會>=5.....,修改每個判斷的數字,並把你要執行的程式放在if內,就可以達到你要的目的了。
6#
 樓主| 發表於 2016-4-8 23:54:25 | 只看該作者
我有成功的將比較基本的溫溼度顯示於I2C 1602以及藉由SENSOR去控制RELAY的程式以變數包裹的方式來依據擷取秒數進行控制,但是目前比較頭大的是~我還希望可以用ESP8266將溫濕度的資料上傳到thingspeak上面,問題就來了~我將該上傳的程式用變數包裹後放入,就會一直出現錯誤。
以下是大致的程式碼
int c0,c1,c2;
void loop(){
delay(1000);
if(++c0 >= 1){
c0 =0;
...
}
if(++c1 >= 180){
c1 =0;
...
}
if(++c2 >= 600){
c2 =0;
dhtbuf[0] = dht11.readHumidity();
dhtbuf[1] = dht11.readTemperature();

    // 確認取回的溫溼度數據可用
    if( isnan(dhtbuf[0]) || isnan(dhtbuf[1]) )
    {
        debug.println( "Failed to read form DHT11" );
    }
    else
    {
        digitalWrite( _ledpin, HIGH );
        char buf[3];
        String HH, TT;
        buf[0] = 0x30 + dhtbuf[1] / 10;
        buf[1] = 0x30 + dhtbuf[1] % 10;
        TT = (String(buf)).substring( 0, 2 );
        buf[0] = 0x30 + dhtbuf[0] / 10;
        buf[1] = 0x30 + dhtbuf[0] % 10;
        HH = (String(buf)).substring( 0, 2 );

        updateDHT11( TT, HH );
        #ifdef DEBUG
            debug.print("Humidity: ");
            debug.print( HH );
            debug.print(" %\t");
            debug.print("Temperature: ");
            debug.print( TT );
            debug.println(" *C\t");
        #endif
        digitalWrite( _ledpin, LOW );
    }

    delay(60000);   // 60 second
}

void updateDHT11( String T, String H )
{
    // 設定 ESP8266 作為 Client 端
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += IP;
    cmd += "\",80";
    sendDebug(cmd);
    delay(2000);
    if( Serial.find( "Error" ) )
    {
        debug.print( "RECEIVED: Error\nExit1" );
        return;
    }

    cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
    Serial.print( "AT+CIPSEND=" );
    Serial.println( cmd.length() );
    if(Serial.find( ">" ) )
    {
        debug.print(">");
        debug.print(cmd);
        Serial.print(cmd);
    }
    else
    {
        sendDebug( "AT+CIPCLOSE" );
    }
    if( Serial.find("OK") )
    {
        debug.println( "RECEIVED: OK" );
    }
    else
    {
        debug.println( "RECEIVED: Error\nExit2" );
    }
}

void sendDebug(String cmd)
{
    debug.print("SEND: ");
    debug.println(cmd);
    Serial.println(cmd);
}

boolean connectWiFi()
{
    Serial.println("AT+CWMODE=1");
    delay(2000);
    String cmd="AT+CWJAP=\"";
    cmd+=SSID;
    cmd+="\",\"";
    cmd+=PASS;
    cmd+="\"";
    sendDebug(cmd);
    delay(5000);
    if(Serial.find("OK"))
    {
        debug.println("RECEIVED: OK");
        return true;
    }
    else
    {
        debug.println("RECEIVED: Error");
        return false;
    }

    cmd = "AT+CIPMUX=0";
    sendDebug( cmd );
    if( Serial.find( "Error") )
    {
        debug.print( "RECEIVED: Error" );
        return false;
    }
}
}

另外還有一個問題,於上傳雲端的程式中須有等待的時間,這是不是也會影響整個loop的運作時間?
7#
發表於 2016-4-9 09:46:31 | 只看該作者
>>我將該上傳的程式用變數包裹後放入,就會一直出現錯誤。
這裡指的到底是什麼錯誤??

>>另外還有一個問題,於上傳雲端的程式中須有等待的時間,這是不是也會影響整個loop的運作時間?
當然會, 如果要計算精確
你己經接了RTC, 直接讀出來看時間就更準了
還有後面那個  delay(60000);   // 60 second
應該不用加了吧
8#
 樓主| 發表於 2016-4-12 12:34:40 | 只看該作者
錯誤訊息是這樣:
Arduino:1.6.7 (Windows 8.1), 板子:"Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

D:\My Documents\Desktop\_2016-04-07final-test\_2016-04-07final-test.ino: In function 'void setup()':

_2016-04-07final-test:69: error: 'sendDebug' was not declared in this scope

     {sendDebug("AT");

                    ^

D:\My Documents\Desktop\_2016-04-07final-test\_2016-04-07final-test.ino:71:24: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

     if(Serial.find("OK"))

                        ^

_2016-04-07final-test:74: error: 'connectWiFi' was not declared in this scope

         connectWiFi();

                     ^

D:\My Documents\Desktop\_2016-04-07final-test\_2016-04-07final-test.ino: In function 'void loop()':

_2016-04-07final-test:198: error: 'updateDHT11' was not declared in this scope

         updateDHT11( TT, HH );

                             ^

_2016-04-07final-test:214: error: a function-definition is not allowed here before '{' token

{

^

_2016-04-07final-test:289: error: expected '}' at end of input

}

^

exit status 1
'sendDebug' was not declared in this scope
9#
發表於 2016-4-12 12:42:39 | 只看該作者
你括號括錯了
仔細看一下就知道了
SETUP 那邊少了一個 }
然後最下面connectWiFi 多一個 }
10#
 樓主| 發表於 2016-4-12 16:18:29 | 只看該作者
謝謝您~果然是括號的問題~新手習慣不好,括號有時會忘了加或忘了刪除...
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-5-5 08:55 , Processed in 0.071892 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表