Reading characters/strings with arduino serial port

Posted on 09/10/2009 by project2501
Tags: Legacy

Finally, I can send and receive messages between the ardbot and my pc, over bluetooth. This post on the Arduino forum was very helpful:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231650517

Here's my code in its preliminary state. The idea will be to transmit a short string of characters from the pc to the ardbot. The '!' character represents the start of a message. The code fragment below just echoes back to the PC terminal program what was sent.

 

void loop() {
  bluetooth.flush();
  char input[10];
  memset(input, '\0', 10);
  byte inByte = '\0';
  while(inByte != '!') {
    inByte = bluetooth.read(); // Wait for the start of the message
  }
  if(inByte == '!') {
    while(bluetooth.available() < 5) { // Wait until we receive 5 characters
      ;
    }
    for (int i=0; i < 5; i++) {
      input[i] = bluetooth.read(); // Read the characters into an array
    }
  }
  bluetooth.print("Received: "); // Send the message back to the pc terminal
  bluetooth.println(input);
}

 

 

Flag this post

Thanks for helping to keep our community civil!


Notify staff privately
It's Spam
This post is an advertisement, or vandalism. It is not useful or relevant to the current topic.

You flagged this as spam. Undo flag.Flag Post