I am writing two Arduino Uno programs. One is a remote with a joystick and the other has five LED's indicating which direction the joystick is pointing. They are Bluetooth, so everything is sent and received through Serial. I am sending three variables by joining them into one string, separated by colons. Then I separate them back into three variables on the other arduino The joystick, Bluetooth, and LED's part of the programs work, I'm having trouble receiving the data and separating it. I have included both programs below. As it is now, it doesn't work. In the slave code, you can change the int xPosition = getValue(input, ':', 0).toInt(); to int xPosition = getValue(input2, ':', 0).toInt();( also change yPosition and buttonState) and everything works. What is the difference between that string and what I'm receiving?
Slave code:
#include <SoftwareSerial.h>
#define RxD 7
#define TxD 6
SoftwareSerial BlueToothSerial(RxD,TxD);
char flag=1;
String input;
int xPosition;
int yPosition;
int buttonState;
char outstring[30];
char *input2 = "510:609:1\r\n";
void sendBlueToothCommand(char *Command)
{
BlueToothSerial.print(Command);
Serial.print(Command);
delay(100);
while(BlueToothSerial.available())
{
Serial.print(char(BlueToothSerial.read()));
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1 };
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void setup()
{
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
Serial.begin(38400);
BlueToothSerial.begin(38400);
delay(500);
Serial.println("Starting...");
}
void loop()
{
if(BlueToothSerial.available())
{
input = char(BlueToothSerial.read());
int xPosition = getValue(input, ':', 0).toInt();
int yPosition = getValue(input, ':', 1).toInt();
int buttonState = getValue(input, ':', 2).toInt();
Serial.println("xPosition");
Serial.println(xPosition);
Serial.println("yPosition");
Serial.println(yPosition);
Serial.println("buttonState");
Serial.println(buttonState);
if ((xPosition == 507)&&(yPosition == 506)){
clear();
digitalWrite(8, HIGH);
} else if ((xPosition == 507)&&(yPosition == 0)){
clear();
digitalWrite(12, HIGH);
} else if ((xPosition == 507)&&(yPosition == 1023)){
clear();
digitalWrite(10, HIGH);
} else if ((xPosition == 1023)&&(yPosition == 507)){
clear();
digitalWrite(11, HIGH);
} else if ((xPosition == 0)&&(yPosition == 1023)){
clear();
digitalWrite(9, HIGH);
}
}
}
void clear() {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
}
Master code:
#include <SoftwareSerial.h>
#define RxD 7
#define TxD 6
SoftwareSerial BlueToothSerial(RxD,TxD);
int xPin = A1;
int yPin = A0;
int buttonPin = 2;
char outstring[30];
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
void sendBlueToothCommand(char *Command)
{
BlueToothSerial.print(Command);
Serial.print(Command);
delay(100);
while(BlueToothSerial.available())
{
Serial.print(char(BlueToothSerial.read()));
}
}
void setup()
{
Serial.begin(38400);
BlueToothSerial.begin(38400);
delay(500);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
BlueToothSerial.println("Hello");
}
void loop()
{
joy();
sprintf(outstring, "%d:%d:%d\r\n", xPosition, yPosition, buttonState);
BlueToothSerial.println(outstring);
Serial.println(outstring);
}
void joy() {
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
}