Let's Make Robots!

Reading characters/strings with arduino serial port

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


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() {
  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



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Just a continuation of my previous post - I've added some code to parse the incoming string (the array of characters from the function in my previous post) to trigger various events on the ardbot. Here's the function - it takes an array of characters that look like "99:999" (my homegrown protocol) where the first two digits are the "device id" on the robot, and the last three digits are a value to set for the device, for example, setting a servo to a number of degrees. The strtok_r function comes in handy for this kind of parsing...perhaps there is a better way but this code seems to do the job:

const char separator[] = ":";  //just a holder for the string delimiter i use

void process(char* signal) {
  char* accum; // used by the strtok_r function
  char* first = strtok_r(signal,separator,&accum);//get the device id
  int device = atoi(first);//convert the characters to an int
  char* second = strtok_r(NULL,separator,&accum);//get the value
  int value = atoi(second);
  bluetooth.print("Device ID: ");//debugging!
  bluetooth.print("Value: ");
  switch(device) {
  case 12://IR sensor - get the current reading
    distance = analogRead(IR_SENSOR_PIN);
    bluetooth.print("Range: ");
  case 13://LED - blink the led at pin 13 on the arduino

void blink_led() {
  digitalWrite(LED_PIN, HIGH);
  digitalWrite(LED_PIN, LOW);

float read_gp2d12_range(int distance) {
  if (distance < 3)
    return -1; // invalid value
  return (6787.0 /((float)distance - 3.0)) - 4.0;

Here is a handy link for the avr string library documentation useful for arduino coding:


Are you using the Arduino BT board or a standard USB board hooked up with a bluetooth slave module? I wanna do the latter and it's pretty hard to find info on that....

Hi - I'm using a standard Arduino Duemilanove board and a Bluetooth modem similar to this one. I'm using the NewSoftSerial library to hook the modem up to pins 3(RX) and 2(TX) so to the software it looks and functions just like a serial port. So in my code example, the "bluetooth" variable is defined like this:


#define BT_RX_PIN 3
#define BT_TX_PIN 2


NewSoftSerial bluetooth(BT_RX_PIN,BT_TX_PIN);


On the PC side, I'm using the java rxtx library or the minicom terminal program to send data back and forth using the PC serial ports.

I only have the servos and a sonar sensor working over the bluetooth link at this point, but I plan to send commands to the motors as well..

Hope that helps...