0

よし、現時点で半分完成した Arduino スケッチがある。基本的に、以下のスケッチは、文字列が *{blink_Flow_A}* に等しい場合、kegboard-mini シールドの LED を点滅させますが、現在のスケッチが Arduino にロードされている場合、LED は 1 回だけ点滅します。「停止」コマンドが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 の誰かが私にステート マシンのスクラッチ ヘッドを調査するように言った

4

4 に答える 4

1

プログラムをブロックせずに LED を点滅させるには、Timer (および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' を配置して、コードで構文を強調表示することを検討してください。

于 2013-05-14T07:09:02.950 に答える
0

おそらく、 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);
}
}
于 2013-05-13T23:55:36.803 に答える