Hi all,
I have been working on my project for a week or so now and have been hitting a few obstacles.
I'm trying to get my PS3 Controller and Processing and Arduino to work in harmony with each other, but I'm finding that when I run both of the below sketches I'm getting undesired results.
A little explanation:
My PS3 Controller is connected to MAC OSX via bluetooth. Tick.
I use ProControll with the PS3 Controller to read the values of the analog sticks - the leftStick gives the x values from -1 to +1 so I can control left and right directions, the rightStick gives the y values from -1 to +1 so I can control up and down/forwards backwards. Tick.
I map these -1 to +1 values from 10 to 90 and concatenate them into a string to give a four digit reading, convert this into integer and send to Processing's println function and also over Serial to Arduino.
This is where my problem is, when I read the values from println everything looks as it should. However, when I use Serial.read in the Arduino sketch, the numbers aren't what I'm sending from Processing. They can be -/+a couple of thousand digits from what I'm expecting to see.
I've attached both sketches which should be self explanatory for most of you, if anyone needs any clarification on anything please shout out.
If anyone can help I would be most happy!!
Processing:
// This program is almost working
// The mapping of the PS3 controller works successfully,
// however when the data is sent to arduino, the serial.read
// function returns incorrect numbers
import procontroll.*;
import processing.serial.*;
import java.io.*;
Serial myPort;
ControllIO control;
ControllDevice device;
ControllSlider sliderX;
ControllSlider sliderY;
void setup(){
size(255,255);
myPort = new Serial(this, Serial.list()[1], 9600);
control = ControllIO.getInstance(this);
device = control.getDevice("PLAYSTATION(R)3 Controller");
device.printSticks();
device.printSliders();
device.printButtons();
device.setTolerance(0.5f);
sliderX = device.getSlider("x");
sliderY = device.getSlider("rz");
}
void draw(){
background(255);
float x = sliderX.getValue();// + width/2;
int leftStick = int(map(x, -1, 1, 10, 90));
//println(leftStick);
float y = sliderY.getValue();// + height/2;
int rightStick = int(map(y, -1, 1, 10, 90));
//println(rightStick);
String output = str(leftStick) + str(rightStick);
int Output = int(output);
println(Output);
myPort.write(Output);
}
Arduino:
// this program works with PS3_Sticks_Mapped_Working
// but does not receive the correct numbers from processing
// when the PS3 Controller is connected through processing
// run to find out the issue
const int loadTime = 1000; // delay time in void setup.
int Command[]= {5,0,5,0};
int leftSpeed = 50; // the starting speeds of the left
int rightSpeed = 50; // and right motors.
/*
At speed 50, both motors will map this value to a
speed of zero. Neither motor will move before any commands
are sent.
*/
void setup()
{
Serial.begin(9600); // open communication between xBees
/*
Next, we delay for loadTime to ensure that both
the Serial communication is ready and that
the NXT has had enough time to reset.
*/
Serial.print("Delaying for... "); Serial.print(loadTime);
delay(loadTime);
Serial.println("I'm now ready for action!");
}
void loop()
{
if (Serial.available() > 3)
{
for (int i = 0; i<3; i++)
{
// store each command to an array
Command[i] = Serial.read();// - 48;
}
Serial.flush();
}
// Now convert this array to two integers that correspond to speed
// values.
leftSpeed = 10*Command[0] + Command[1];
rightSpeed = 10*Command[2] + Command[3];
// uncomment this component for tests.
Serial.print(leftSpeed);
Serial.print(" ");
Serial.print(rightSpeed);
Serial.println(" ");
/* Now, we need to decide:
the speed and
the direction that each motor should move.
This task is handled by an if/else statement for each motor.
*/
// For the Left Motor:
if (leftSpeed >= 50)
/* Move forward if the command is a value from 45 to 80,
but first Map the value to a value understood by the
Nxt I2c motor library */
{
leftSpeed = map(leftSpeed,50,90,0,80);
Serial.println(leftSpeed);
}
else
/* Move backward if the command is a value from 0 to 44,
but first Map the value to a value understood by the
Nxt I2c motor library */
{
leftSpeed = map(leftSpeed,49,10,0,80);
Serial.println(leftSpeed);
}
// For the Right Motor
if (rightSpeed >=50)
{
rightSpeed = map(rightSpeed,50,90,0,80);
Serial.println(rightSpeed);
}
else
{
rightSpeed = map(rightSpeed,49,10,0,80);
Serial.println(rightSpeed);
}
// Uncomment this code for tests.
/*
Serial.print(leftSpeed);
Serial.print(" ");
//delay(500);
Serial.print(rightSpeed);
Serial.println(" ");
//delay(500);
*/
}