0

目前我正在尝试制作一个草图,其中 Arduino 将读取一系列字符作为命令,并根据从 iDevice 发送的一系列字符做一些事情。我正在使用越狱的 iPhone 3GS 向 Arduino 发送字符。发送串行字符的方法如下所示,

- (IBAction)blinkFlow_A_LED:(id)sender {

    // method to blink the Flow_A LED on the kegboard-mini Arduino sheild.

    NSLog(@"blink Flow_A btn pressed");

    // open serial port / interface

    [serial open:B2400];
    NSLog(@"%c", [serial isOpened]);

    // send serial data (tx)

    char buffer [7];

    buffer[0] = '{';
    buffer[1] = 'b';
    buffer[2] = 'l';
    buffer[3] = 'i';
    buffer[4] = 'n';
    buffer[5] = 'k';
    buffer[6] = '}';

    [serial write:buffer length:7];
}

我创建了一个简单的草图,它使我正在使用的防护罩上的 LED 闪烁,但我希望 LED 在 iOS 应用程序中单击按钮时有条件地闪烁。闪烁 LED 的草图如下所示,

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
  This sketch is specific to making the kegboard-mini shield.

  http://arduino.cc/forum/index.php?topic=157625.new;topicseen#new

  This example code is in the public domain.
*/

// pin D4 - should be connected to the flow_A LED

// give it a name

int led = 4;

// the setup routine runs once when you press reset:

void setup() {
  //initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}

// the loop routine run over and over again forever:

void loop() {
  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
}
4

4 回答 4

1

这是一个简单的草图!

您可能想先查看 Arduino 参考页面: http ://arduino.cc/en/Reference/Serial

在设置中,您至少需要 Serial.begin(2400);

现在,我建议读取和解码字符串 "{blink}" 似乎有点矫枉过正。让我建议你发送一个字符(例如'b'),并检测一个字符,至少开始。查看串行参考页 .available() 和 .read()。有了这些,您可以确定一个字符是否已到达 Arduino 并读取单个字符。

然后,如果您想一次构建一个字符串并将其与 String("{blink}") 进行比较,则可以使用这些。这有点复杂,特别是如果您考虑到异常(如丢失或损坏的字符)。

我很好奇——iPhone 是如何将数据输入到 Arduino 的串口中的?您可以使用串行监视器工具轻松测试您的程序——只是建议您必须点击“发送”才能使字符消失。

于 2013-04-17T04:03:14.407 回答
0

我写了一篇关于从 Serial 中获取输入并让 arduino 做某事的博客文章。就我而言,它是打开/关闭 LED 或使其闪烁。

于 2013-04-17T16:24:21.113 回答
0

如果你想用 Arduino 闪烁 LED,你肯定想看看我的 blinkenlight 博客。我至少有一个实验,通过串行监视器控制闪烁。实际上,我对那里闪烁 LED 的主题有很多变化。

如果您需要一个更复杂的示例,我也有一个基于有限状态机作为解析器的示例。

于 2013-04-17T20:02:52.023 回答
0

我结束了这样一个简单的草图,它允许我将一个串行字节数组存储到一个字符串中,这要归功于这个例子,http://arduino.cc/en/Tutorial/SerialEvent

我目前正在使用的草图如下所示,

/*
 * 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;
    }
  }
}
于 2013-05-13T22:53:03.430 回答