Sketch that responds to certain commands, how is it done?

后端 未结 4 991
执笔经年
执笔经年 2021-01-27 08:50

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}*

4条回答
  •  无人及你
    2021-01-27 08:58

    Let me offer a suggested sketch with a few changes. Bastyen's idea of using a timer is quite good and makes the code much easier. The approach I would suggest is to have the timer pop forever at a fixed interval (100 milliseconds in my sketch). If the LED should not be blinking it stays off. If the LED should be blinking, it switches from off to on or vice versa each time the timer goes off.

    #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;
        }
      }
    }
    

    A few notes:

    1. The setup function establishes the timer with a 100 millisecond interval and attaches the callback routine. Based on my testing, this only needs to be done once.

    2. The main loop just ignores everything unless an input string is complete. If an input string is ready, then the input string is checked for two known values and the appropriate steps are taken. The input string is then discarded.

    3. The timer callback routine forces the LED off, if we are not in blinking mode. Otherwise, it just toggles the state of the LED.

    4. The flow on and flow off routines set the blinking state as need be and control the valve

    5. The serial event routine has two changes. First, input is ignored (and kept in the buffer) if an input string is already complete. This will preserve commands that are being sent to the Arduino while the current command is being processed. Second, the newline character is not added to the input string. This makes checking the input string slightly easier.

提交回复
热议问题