gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:37 24 4
gpt4 key购买 nike

我有一个简单的 Arduino 程序,它每 5 秒将温度传感器的传感器值上传到 Google 电子表格。一切正常。我有 3 个 LED。一种在设置完成时永久点亮,一种在上传值时闪烁,一种在上传完成时点亮 3 秒。

我的问题是我不知道如何在上传值时让 pin 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();

最佳答案

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

该库是一个使用定时器中断来调度任务的实现。您可以手动执行相同的操作。

这里要看的主要思想是使用 delay() 来安排任务通常是浪费的,因为它会锁定处理器以使其无所事事 - “忙于等待”。相反,嵌入式程序员需要了解如何使用定时器中断来中断主“线程”来运行任务,而不会浪费处理器周期。

关于c++ - 如何在 Arduino 中获得连续的 LED 闪光灯? [C/C++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27333711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com