好吧,我必须在此刻完成了一半的Arduino草图。 基本上下面的草图将闪烁的LED上的kegboard迷你屏蔽如果字符的字符串等于* {blink_Flow_A} *然而,LED只与装载在Arduino的当前草图闪烁一次。 我会喜欢的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上有什么区别有人告诉我研究状态机的划痕头
状态机(在它的最简单的 - 它可以是很多更复杂)(if / else语句或开关/箱),您就基于一个变量的状态某些行为可能只是一组条件语句,也改变变量州。 因此,它可以被看作是处理或进展通过一系列的条件,方式。
所以,你有你的LED /阀的状态 - 它要么是闪烁(开)或不闪烁(关闭)。 在这里伪代码:
boolean LED_state = false; //init to false/closed
void loop(){
if (checkForCorrectCommand() == true){ //
if (LED_State == false){
open_valve();
LED_State = true;
} else {
close_valve();
LED_State = false;
}
}
}
闪烁的LED部分应该很容易,如果你的代码的要点上面实现。 该checkForCorrectCommand()
位是你写检查无论你输入的功能-键,串口,按钮等,应该返回一个布尔值。
为了使LED闪烁不阻塞程序,我建议你使用定时器(和TimerOne库 )。 我做一个快速的示例代码:
#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的有语法高亮显示的代码。
也许类似在IDE中“ 闪烁无延迟 ”的例子。 你检查的时间,并决定何时以及如何改变LED /数字出来。
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup(){
// Your stuff here
}
void loop()
{
// Your stuff here.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
让我提供了一些小改动建议草图。 使用计时器的Bastyen的想法是相当不错,使代码更容易。 我建议的方法是永远都定时器流行音乐在固定的时间间隔(100毫秒在我的素描)。 如果指示灯不应该闪烁,它保持关闭。 如果LED应该闪烁,它从关闭到每个定时器熄灭时间接通或反之亦然。
#include "TimerOne.h"
/*
* 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
boolean shouldBeBlinking = false;
boolean ledOn = false;
void setup() {
Serial.begin(9600); // open serial port, sets data rate to 2400bps
Serial.println("Power on test");
inputString.reserve(200);
pinMode(RELAY_A, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Timer1.initialize(100000);
Timer1.attachInterrupt(timer1Callback);
}
void loop() {
if (!stringComplete)
return;
if (inputString == "{blink_Flow_A}")
flow_A_blink_start();
if (inputString == "{blink_Flow_B}")
flow_A_blink_stop();
inputString = "";
stringComplete = false;
}
void timer1Callback() {
/* If we are not in blinking mode, just make sure the LED is off */
if (!shouldBeBlinking) {
digitalWrite(led, LOW);
ledOn = false;
return;
}
/* Since we are in blinking mode, check the state of the LED. Turn
it off if it is on and vice versa. */
ledOn = (ledOn) ? false : true;
digitalWrite(led, ledOn);
}
void flow_A_blink_start() {
shouldBeBlinking = true;
open_valve();
}
void flow_A_blink_stop() {
shouldBeBlinking = false;
close_valve();
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
}
//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() {
if (stringComplete)
return;
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString unless it is a newline
if (inChar != '\n')
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
else {
stringComplete = true;
}
}
}
几点注意事项:
设置功能建立与100毫秒的间隔定时器并附加回调例程。 根据我的测试,这只需要做一次。
主循环会忽略一切,除非输入字符串是完整的。 如果输入的字符串是准备好了,则输入字符串被检查两个已知值并采取相应的步骤。 然后输入字符串被丢弃。
计时器回调例程力LED熄灭,如果我们不是在闪烁模式。 否则,它只是切换LED的状态。
流出例程的流量与设定的闪烁状态如需要的话,控制阀
串行事件例程有两个变化。 首先,输入被忽略(并保存在缓冲区),如果输入字符串已经完成。 这将保留在正在处理的当前命令正在发送到Arduino命令。 其次,换行符不会添加到输入字符串。 这使得检查输入字符串稍微容易些。