I've removed the almost all unused things
Tx
/*
Example 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
PS3BT PS3(&Btd); // This will just create the instance
bool printTemperature;
bool printAngle;
const uint8_t LED_tx = 2;
char dataPacket[64];
int Servo1 = 90; // 2 char
int Servo2 = 0; // 3 char
int Servo3 = 0;
int led1 = 0;
int led2 = 0;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use by strtok() function
char messageFromPC[numChars] = {0};
int servo = 90;
int integerFromPC = 0;
float floatFromPC = 0.0;
int shock = 0;
boolean newData = false;
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"));
Serial.println("This demo expects 3 pieces of data - text, an integer a floating point and an integer value");
Serial.println("Enter data in this style <text,12,24.7,55> ");
Serial.println();
}
void loop() {
Usb.Task();
if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
servo = map(PS3.getAnalogHat(LeftHatX), 0, 255, 0, 180);
if (PS3.getButtonClick(TRIANGLE)) {
led1 = 1;
} else {
led1 = 0;
}
if (PS3.getButtonPress(CIRCLE)) {
led2 = 1;
} else {
led2 = 0;
}
}
Serial.print("<");
Serial.print("hello CAR");
Serial.print(",");
Serial.print("1");
Serial.print(",");
Serial.print("2.3");
Serial.print(",");
Serial.print(servo);
Serial.print(",");
Serial.print(led1);
Serial.print(",");
Serial.print(led2);
Serial.println(">");
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() replaces the commas with \0
parseData();
newData = false;
}
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);
}
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
shock = atoi(strtokIndx);
}
Rx
#include <Servo.h>
const int ledPin = 12; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
int val = 0;
int grado = 99;
Servo myservo;
// Receive with start- and end-markers combined with parsing
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use by strtok() function
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int servo = 8;
int led1 = 0;
int led2 = 0;
int integerFromPC = 0;
float floatFromPC = 0.0;
int shock = 77;
boolean newData = false;
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);
Serial.println("This demo expects 3 pieces of data - text, an integer, a floating point, and 3 other integer values");
Serial.println();
}
void loop() {
Serial.print("<");
Serial.print("hello PS3");
Serial.print(",");
Serial.print("3");
Serial.print(",");
Serial.print("2.1");
Serial.print(",");
Serial.print(shock);
Serial.println(">");
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() replaces the commas with \0
parseData();
newData = false;
}
val = servo;
myservo.write(val);
if (led1 == 1) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (led2 == 1) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
servo = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
led1 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
led2 = atoi(strtokIndx);
}