Hi guys,
I'm fighting with this code by two days, read and tried to edit many things but nothing..
The project consists of sending multi values (ps3 controller) from the the first Arduino and send them to the second arduino via Serial wireless using 2 xbee modules.
There is a part that seems to work fine where the first TX Arduino can send the multi values and the RX Arduino can receive them correctly, but thanks to the serial monitor of the RX it seems that the problem begin when the values exceed three digits, for example all work fine when the value are
94
58
0
0
0
but when they reach three digits like here
87
235
0
0
0
the RX arduino freeze and back to work and respond to commands only after some seconds that i don't press any button of the controller.
I'm still a newbie but it seems a buffer issue what do you think?
These are my two sketches:
TX
/*
Edited version of the sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
For more information visit my blog: http://blog.tkjelectronics.dk/ or
send me an e-mail: kristianl@tkjelectronics.com
*/
#include <PS3BT.h>
#include <usbhub.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
//USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
PS3BT PS3(&Btd); // This will just create the instance
//PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
bool printTemperature;
bool printAngle;
const uint8_t LED_tx = 2;
char dataPacket[64];
int Servo1 = 0; // 2 char
int Servo2 = 0; // 3 char
int Servo3 = 0; // 3 char
int led1 = 0;
int led2 = 0;
void setup() {
pinMode(LED_tx, OUTPUT);
Serial.begin(57600);
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}
void loop() {
Usb.Task();
if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
Servo1 = map(PS3.getAnalogHat(LeftHatX), 0, 255, 0, 180);
Servo2 = PS3.getAnalogButton(L2);
Servo3 = PS3.getAnalogButton(R2);
if (PS3.getButtonClick(TRIANGLE)) {
led1 = 1;
} else {
led1 = 0;
}
if (PS3.getButtonPress(CIRCLE)) {
led2 = 1;
} else {
led2 = 0;
}
sprintf(dataPacket,"<%d,%d,%d,%d,%d>",Servo1, Servo2, Servo3, led1, led2); //sprintf(dataPacket, "X%d" ,gx);
Serial.println(dataPacket);
delay(10);
}
if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
if (PS3.getButtonClick(PS)) {
Serial.print(F("\r\nPS"));
PS3.disconnect();
}
else {
// if (PS3.getButtonClick(TRIANGLE))
// Serial.print("\r\nH");
// if (PS3.getButtonPress(CIRCLE))
// Serial.print(F("\r\nH"));
if (PS3.PS3Connected)
digitalWrite(LED_tx, PS3.getButtonPress(CROSS));
else
digitalWrite(LED_tx, LOW);
if (PS3.getButtonClick(SQUARE))
Serial.print(F("\r\ndigitalWrite(ledPin, HIGH);"));
if (PS3.getButtonClick(UP)) {
Serial.print(F("\r\nUp"));
if (PS3.PS3Connected) {
PS3.setLedOff();
PS3.setLedOn(LED4);
}
}
}
}
}
RX
#include <Servo.h>
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[64];
byte index;
const int ledPin = 12; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
int valo = 0;
Servo myservo;
void setup() {
Serial.begin(57600); // initialize serial communication
Serial.println( "Serial Start" );
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
myservo.attach(7);
}
void loop() {
if (Serial.available() > 0) { // see if there's incoming serial data:
delay(1); //small delay to allow input buffer to fill
/*
incomingByte = Serial.read(); // read the oldest byte in the serial buffer
if (incomingByte == 'H') { // if it's a capital H (ASCII 72), turn on the LED
digitalWrite(ledPin, HIGH);
// delay(10);
digitalWrite(ledPin, LOW);
}
if (incomingByte == 'L') { // if it's an L (ASCII 76) turn off the LED
digitalWrite(ledPin, LOW);
}
*/
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
return;
}
else
{
if(index < 63)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
int values[5]; // Array to hold the values
byte index = 0; // index into array
char *token = strtok(inData, ","); // Get the first token
while(token)
{
values[index] = atoi(token); // convert the token to an int and store it in the array
index++; // Increment the index
token = strtok(NULL, ","); // Keep parsing the same string
}
Serial.println("---------");
Serial.println(values[0]);
Serial.println(values[1]);
Serial.println(values[2]);
Serial.println(values[3]);
Serial.println(values[4]);
valo = values[0];
myservo.write(valo);
if (values[3] == 1) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (values[4] == 1) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
// delay(10);
}
}