首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Arduino中获得连续的LED闪光灯?[C/C++]

如何在Arduino中获得连续的LED闪光灯?[C/C++]
EN

Stack Overflow用户
提问于 2014-12-07 00:23:52
回答 1查看 196关注 0票数 1

我有一个简单的Arduino程序,它每5秒将温度传感器的感应值上传到Google电子表格中。这一切都很好。我有3个LED。一个在设置完成时永久亮起,一个在上传数值时闪烁,还有一个在上传完成时亮起3秒。

我的问题是,我不知道如何让引脚3 LED闪烁,而值正在上传。无论我把它放在哪里,它似乎只运行一次,然后继续程序的其余部分。我意识到这是一个非常基本的逻辑问题,但我不能解决它,真的很感谢一些帮助!

下面是主程序。

代码语言:javascript
复制
/* 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
}

下面是我想要实现的循环

代码语言:javascript
复制
void flash()
{
    digitalWrite(3, HIGH);
    delay(100);
    digitalWrite(3, LOW);
    delay(100);
} 

开始于

代码语言:javascript
复制
TembooChoreo AppendRowChoreo(client);

和结束于

代码语言:javascript
复制
AppendRowChoreo.close();
EN

回答 1

Stack Overflow用户

发布于 2014-12-07 01:41:59

看一看Timer Library。此库允许您在特定延迟后运行任务,而无需使用delay()函数。

该库是一个使用计时器中断来调度任务的实现。你可以用手做同样的事情。

这里要看到的主要思想是,使用delay()来调度任务通常是浪费的,因为它将处理器锁定为不做任何事情--“忙于等待”。相反,嵌入式程序员需要学习如何使用定时器中断来中断主“线程”来运行任务,而不浪费处理器周期。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27333711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档