Is there any good way of sending values from two joysticks from arduino A to arduino B? I tried sending using if statements, so if the Y- or X-axis' value changed then it would send that, but the servo motor on Arduino B went a little bit crazy.
Here's what I've done so far:
//Master Code
void loop()
{
yVal = analogRead(yAxis); // read the value of the Right Joystick (value between 0 and 1023)
yMap = map(yVal, 0, 1023, 0, 255);
xVal = analogRead(xAxis); // read the value of the left Joystick (value between 0 and 1023)
xMap = map(xVal, 0, 1023, 0, 110);
if(Serial.available() && !(xVal > 505 && xVal < 510)){
Serial.print('X');
delay(10);
Serial.write(xMap);
}
if(Serial.available() && !(yVal > 500 && yVal < 520)){
Serial.print('Y');
delay(10);
Serial.write(yMap);
}
delay(10);
}
And the Slave code:
//Slave code
void loop(){
while(Serial.available())
{
c = Serial.read();
delay(5);
val = Serial.read();
if(c == 'X'){
digitalWrite(13, LOW);
myServo.write(val);
}else if(c == 'Y'){
digitalWrite(13, HIGH);
}
}
}
Edit: I was thinking that maybe I should do something like a start and stop bit for X and Y?
Edit2: I wrote an if statement in Arduino B because it was reading the value -1, I don't know why, but after the if statement the servo motor hasn't been going crazy and the program works now. I still would like to know if I can do this in a better way. Thanks in advance!
-
There are a number of possible issues in the code. 1) Can the sender and receiver get out of sync. That is, can the sender send the "x" character when the receiver is expecting a servo-position-number? Perhaps you might map to 0 through 254 and reserve the value 255 as a synchronization marker. Then send "marker", X then Y over and over again.st2000– st200005/07/2016 13:41:15Commented May 7, 2016 at 13:41
2 Answers 2
Arduino A
You can read the values from two joysticks as normal and send it like this. This method ensures of no data loss. Ensure that you read the values and store them in appropriate variables and then send it over serial.
// Setting Sensor pins
const int potPin = A0;
const int pot1Pin = A1;
// Creating variables
uint8_t pot;
uint8_t pot1;
void setup() {
// Declearing the pinModes' for sensors
pinMode(potPin, INPUT);
pinMode(pot1Pin, INPUT);
// Begining Serial Communication
Serial.begin(9600);
}
void loop() {
// reading from the sensors
pot = map(analogRead(potPin),0 ,1023, 0, 255);
pot1 = map(analogRead(pot1Pin),0 ,1023, 0, 255);
//Packing the Serial message
Serial.print("U*"); //A header
Serial.print("A"); //a token to indicate the message payload
Serial.print(pot);
Serial.println("");
Serial.print("U*"); //A header
Serial.print("B"); //a token to indicate the message payload
Serial.print(pot1);
Serial.println("");
}
Arduino B
After the data are packed and sent over the serial. You need to unpack it with the other Arduino/device
The code will look like this:
String data; //Store each line of Serial communication
String pot1String = "0", pot2String = "0";
float pot1, pot2;
void setup()
{
//initialize Serial communication
Serial.begin(9600);
//read first package
if (Serial.available() > 0)
{
data = Serial.readStringUntil('\n');
}
//Disregard the first reading
data = "0";
}
void loop() {
//start reading from serial
if (Serial.available() > 0)
{
data = Serial.readStringUntil('\n');
}
String payload = "";
if (data != "0")
{
int offset = data.indexOf("U*"); //This is the header (0x552A)
if (offset >= 0)
{
payload = data.substring(offset + 3, data.indexOf('\n'));
char value = data.charAt (offset + 2);
switch (value)
{
case 'A':
pot1String = payload;
break;
case 'B':
pot2String = payload;
break;
//Add more case as the project grow
}
}
}
//convert strings to float
pot1 = pot1String.toFloat();
pot2 = pot2String.toFloat();
// Continue with the rest of your project
}
- Make sure to use the same baud rate in both sketches
Hope this helped. Ask me if you have farther questions
-
Oh wow, thank you for posting this. I'll see if I can get this to work :)Keilara– Keilara05/07/2016 14:03:01Commented May 7, 2016 at 14:03
-
I've been trying to look on Arduino B's serial monitor to see if the values of pot1 and pot2 change, but they don't. I haven't changed anthing in the code except for the baud rate. The values change the very first time I touch my joysticks, but after that it just prints those values over and over again.Keilara– Keilara05/07/2016 15:10:57Commented May 7, 2016 at 15:10
-
Can you tell me how you read the joystick values? Did you change the baud rate in both sketches? Is your baud rate in serial monitor the same?Amir– Amir05/07/2016 15:20:59Commented May 7, 2016 at 15:20
-
Try connecting the Rx, Tx of two Arduino together (Rx, Arduino A => Tx, Arduino B & Tx, Arduino A => Rx, Arduino B)Amir– Amir05/07/2016 15:47:35Commented May 7, 2016 at 15:47
-
I connected Rx to Tx and Tx to Rx and I read the joystick values with analogRead. I've set the baud rate of both bluetooth modules to 115200, but so far no luck.Keilara– Keilara05/07/2016 16:32:12Commented May 7, 2016 at 16:32
When using the ADC to control servos I get much better results when I process the ADC values through an exponential smoothing function:
There is less jitter. However there is more lag. You can tune the function by changing alpha between 1 (no history, effectively no smoothing at all) to 0 (no new data is averaged in, effectively not changing). Pick 0.5 to start with. If there is too much jitter pick smaller numbers until the jitter is reduced and the lag is still tolerable.