2014-12-06 58 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()来安排任务通常是浪费的,因为它将处理器锁定在什么都不做 - “忙等待”。相反,嵌入式程序员需要了解如何使用定时器中断来中断主“线程”来运行任务,而不会浪费处理器周期。