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

后端 未结 4 1033
执笔经年
执笔经年 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 09:01

    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.

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

    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.

提交回复
热议问题