响应某些命令的草图,它是如何完成的? [英] Sketch that responds to certain commands, how is it done?

查看:15
本文介绍了响应某些命令的草图,它是如何完成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Alright I have a half complete Arduino sketch at the moment. Basically the sketch below will blink an LED on a kegboard-mini shield if a string of chars equals *{blink_Flow_A}* However the LED only blinks once with the current sketch loaded on the Arduino. I will like the Arduino to blink repeatedly until the "stop" command is sent to the Arduino. I would eventually like to open a valve, keep it open until the valve receives to the close command then close the valve. The sketch looks like the following,

/*
 * 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 it makes any difference someone on IRC told me to research state machines scratches head

推荐答案

状态机(最简单的 - 它可能更复杂)可以只是一组条件语句(if/else 或 switch/case),其中您根据变量的状态执行某些行为,并更改该变量的状态.因此,可以将其视为处理或处理一系列条件的方式.

A state machine (at it's simplest - it can be lots more complicated) can be just a set of conditional statements (if/else or switch/case) where you do certain behaviors based on the state of a variable, and also change that variable state. So it can be thought of as a way of handling or progressing through a series of conditions.

所以你有你的 LED/阀门的状态 - 它是闪烁(打开)或不闪烁(关闭).在这里用伪代码:

So you have the state of your LED/valve - it is either blinking (open) or not blinking (closed). In pseudo code here:

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() 位是您编写的一个函数,用于检查您的输入——键、序列号、按钮等.它应该返回一个布尔值.

The blinking LED part should be easy to implement if you get the gist of the code above. The checkForCorrectCommand() bit is a function you write for checking whatever your input is - key, serial, button, etc. It should return a boolean.

这篇关于响应某些命令的草图,它是如何完成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆