2014-12-06 54 views
1

我有一個簡單的Arduino程序,每5秒將溫度傳感器的傳感器值上傳到Google Spreadsheet。它一切正常。我有3個LED。一個設置完成時會永久打開,一個在上傳值時閃爍,另一個在上載完成時打開3秒。如何在Arduino中獲得連續的LED閃光燈? [C/C++]

我的問題是,我不知道如何讓引腳3 LED閃爍,而價值正在上傳。無論我把它放在哪裏,它似乎只運行一次,並繼續執行其餘的程序。我意識到這是一個非常基本的邏輯問題,但我無法弄清楚,真的很感謝一些幫助!

這是主程序。

/* Setup shield-specifiC#include statements */ 
#include <SPI.h> 
#include <WiFi.h> 
#include <WiFiClient.h> 
#include <Temboo.h> 
#include "TembooArduino.h" // Contains Temboo account information 


WiFiClient client; 

int numRuns = 1; // Execution count, so this doesn't run forever 
int maxRuns = 3; // Maximum number of times the Choreo should be executed 


void setup() { 
    Serial.begin(9600); 
    pinMode(A0,INPUT); 
    pinMode(4, OUTPUT); 
    digitalWrite(4, LOW); 
    pinMode(3, OUTPUT); 
    digitalWrite(3, LOW); 
    pinMode(2, OUTPUT); 
    digitalWrite(2, LOW); 
    // For debugging, wait until the serial console is connected. 
    delay(4000); 
    while(!Serial); 

    int wifiStatus = WL_IDLE_STATUS; 

    // Determine if the WiFi Shield is present. 
    Serial.print("\n\nShield:"); 
    if (WiFi.status() == WL_NO_SHIELD) { 
    Serial.println("FAIL"); 

    // If there's no WiFi shield, stop here. 
    while(true); 
    } 

    Serial.println("OK"); 

    // Try to connect to the local WiFi network. 
    while(wifiStatus != WL_CONNECTED) { 
    Serial.print("WiFi:"); 
    wifiStatus = WiFi.begin(WIFI_SSID, WPA_PASSWORD); 

    if (wifiStatus == WL_CONNECTED) { 
     Serial.println("OK"); 
    } else { 
     Serial.println("FAIL"); 
    } 
    delay(5000); 
    } 

    Serial.println("Setup complete.\n"); 
    digitalWrite(4,HIGH); 
} 

void loop() { 
    if (numRuns <= maxRuns) 
     { 
    Serial.println("Running AppendRow - Run #" + String(numRuns++) + "\n"); 

    int sensorVal = analogRead(A0); 

    //two lines to test the sensor value in serial output 
    Serial.print("sensor Value: "); 
    Serial.print(sensorVal); 

    float voltage = (sensorVal/1024.0) * 5.0; //testing voltage output in serial 

    Serial.print(", volts "); 
    Serial.print(voltage); 

    float temperature = (voltage - .5) * 100; 

    Serial.print(", temperature in C "); 
    Serial.print(temperature); 
    Serial.print("\n"); 

    TembooChoreo AppendRowChoreo(client); 


    // Invoke the Temboo client 
    AppendRowChoreo.begin(); 

    // Set Temboo account credentials 
    AppendRowChoreo.setAccountName(TEMBOO_ACCOUNT); 
    AppendRowChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); 
    AppendRowChoreo.setAppKey(TEMBOO_APP_KEY); 

    // Set Choreo inputs 
    String UsernameValue = "xxxx"; 
    AppendRowChoreo.addInput("Username", UsernameValue); 
    String PasswordValue = "xxxx"; 
    AppendRowChoreo.addInput("Password", PasswordValue); 
    String RowDataValue = (String)analogRead(A0); 
    AppendRowChoreo.addInput("RowData", RowDataValue); 
    String SpreadsheetTitleValue = "TempSensor"; 
    AppendRowChoreo.addInput("SpreadsheetTitle", SpreadsheetTitleValue); 

    // Identify the Choreo to run 
    AppendRowChoreo.setChoreo("/Library/Google/Spreadsheets/AppendRow"); 
    // Run the Choreo; when results are available, print them to serial 
    AppendRowChoreo.run(); 
    while(AppendRowChoreo.available()) 
    { 
     char c = AppendRowChoreo.read(); 
     Serial.print(c); 
    } 
    AppendRowChoreo.close(); 

    digitalWrite(2, HIGH); 
    delay(3000);  
    digitalWrite(2, LOW); 

     } 

    Serial.println("\nWaiting...\n"); 
    delay(5000); // wait 5 seconds between AppendRow calls 
} 

這裏是環我想實現

void flash() 
{ 
    digitalWrite(3, HIGH); 
    delay(100); 
    digitalWrite(3, LOW); 
    delay(100); 
} 

開始

TembooChoreo AppendRowChoreo(client); 

,並在

AppendRowChoreo.close(); 

回答

1

結束看一看的Timer Library。該庫允許您在不使用delay()函數的情況下在某些延遲之後運行任務。

該庫是一個使用計時器中斷來安排任務的實現。你可以手工做同樣的事情。

在這裏看到的主要想法是,使用delay()來安排任務通常是浪費的,因爲它將處理器鎖定在什麼都不做 - 「忙等待」。相反,嵌入式程序員需要了解如何使用定時器中斷來中斷主「線程」來運行任務,而不會浪費處理器週期。