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:http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231650517Here’s my code in its preliminary state. The idea will be to transmit a short string of characters from the


This is a companion discussion topic for the original entry at https://community.robotshop.com/blog/show/reading-characters-strings-with-arduino-serial-port

Reading characters/strings with arduino serial port(continued)

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.println(device);
bluetooth.print("Value: ");
bluetooth.println(value);
switch(device) {
case 12://IR sensor - get the current reading
distance = analogRead(IR_SENSOR_PIN);
bluetooth.print("Range: ");
bluetooth.println(read_gp2d12_range(distance));
blink_led();
break;
case 13://LED - blink the led at pin 13 on the arduino
blink_led();
break;
}
}

void blink_led() {
digitalWrite(LED_PIN, HIGH);
delay(100);
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:

http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

Just curious…
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…

re: just curious

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…