素描,响应某些命令,它怎么办? [英] Sketch that responds to certain commands, how is it done?

查看:191
本文介绍了素描,响应某些命令,它怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我必须在此刻完成了一半的Arduino草图。下面基本上草图将点亮LED上kegboard迷你盾,如果字符的字符串等于* {} blink_Flow_A *但是,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

推荐答案

一个状态机(在它的最简单的 - 它可以是很多更复杂)可能是只是一组条件语句(如/ else或开关/箱),其中你基于一个变量的状态某些行为,也改变变量的状态。因此,它可以被认为是处理或进展通过一系列的条件的一种方式。

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 /阀的状态 - 它要么是闪烁(开)或不闪烁(关闭)。在伪code位置:

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部分应该很容易,如果你得到了code的主旨以上实现。在 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天全站免登陆