gpt4 book ai didi

c - 响应某些命令的 Sketch 是如何完成的?

转载 作者:行者123 更新时间:2023-11-30 15:48:56 24 4
gpt4 key购买 nike

好吧,我现在有一个半完整的 Arduino 草图。基本上,如果一串字符等于 *{blink_Flow_A}*,下面的草图将使 kegboard-mini 扩展板上的 LED 闪烁,但是,在 Arduino 上加载当前草图时,LED 仅闪烁一次。我希望 Arduino 反复闪烁,直到“停止”命令发送到 Arduino。我最终想打开一个阀门,保持打开状态,直到阀门收到关闭命令,然后关闭阀门。草图如下所示,

/*
* kegboard-serial-simple-blink07
* This code is public domain
*
* This sketch sends a receives a multibyte String from the iPhone
* and performs functions on it.
*
* Examples:
* http://arduino.cc/en/Tutorial/SerialEvent
* http://arduino.cc/en/Serial/read
*/

// global variables should be identified with _

// flow_A LED
int led = 4;

// relay_A
const int RELAY_A = A0;

// variables from sketch example
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete

void setup() {

Serial.begin(2400); // open serial port, sets data rate to 2400bps
Serial.println("Power on test");
inputString.reserve(200);

pinMode(RELAY_A, OUTPUT);
}

void open_valve() {

digitalWrite(RELAY_A, HIGH); // turn RELAY_A on

}

void close_valve() {

digitalWrite(RELAY_A, LOW); // turn RELAY_A off
}

void flow_A_blink() {

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

void flow_A_blink_stop() {

digitalWrite(led, LOW);
}

void loop() {
// print the string when newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}

if (inputString == "{blink_Flow_A}") {
flow_A_blink();
}
}

//SerialEvent occurs whenever a new data comes in the
//hardware serial RX. This routine is run between each
//time loop() runs, so using delay inside loop can delay
//response. Multiple bytes of data may be available.

void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}

如果这有什么不同的话,IRC 上有人告诉我研究状态机挠头

最佳答案

要使 Led 闪烁而不阻塞程序,我建议您使用计时器(和 TimerOne library )。我制作了一个快速示例代码:

#include "TimerOne.h" //Include the librart, follow the previous link to download and install.

int LED = 4;
const int RELAY_A = A0;
boolean ledOn;

void setup()
{
pinMode(LED, OUTPUT)
Timer1.initialise(500000) // Initialise timer1 with a 1/2 second (500000µs) period
ledOn = false;
}

void blinkCallback() // Callback function call every 1/2 second when attached to the timer
{
if(ledOn){
digitalWrite(LED,LOW);
ledOn = false;
}
else{
digitalWrite(LED,HIGH);
ledOn = true;
}
}

void open_valve() {

digitalWrite(RELAY_A, HIGH); // turn RELAY_A on

}

void close_valve() {

digitalWrite(RELAY_A, LOW); // turn RELAY_A off
}

void serialEvent() {
while(Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}

void loop()
{
// print the string when newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}


if (inputString == "{blink_Flow_A}") {
Timer1.attachInterupt(blinkCallback); //Start blinking
}
if (inputString == "{stop}") {
Timer1.detachInterrupt(); //Stop blinking
}
if (inputString == "{open_valve}") {
open_valve();
}
if (inputString == "{close_valve}") {
close_valve();
}
}

注意:
考虑放置标签“c”或“java”以在代码上进行语法突出显示。

关于c - 响应某些命令的 Sketch 是如何完成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16532586/

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