i sent u a message, but i also post it (maybe its useful for anyone).
processing:
/* RC car project | Arduino UNO | zH32
*/
import processing.serial.*;
import controlP5.*;
import procontroll.*;
import net.java.games.input.*;
import java.io.*;
ControlP5 controlP5;
ControllIO controll;
ControllDevice device;
ControllStick stick;
ControllStick stick1;
ControllButton button1;
ControllButton button;
Serial myPort;
void setup()
{
println(Serial.list());
size(1024,600);
myPort = new Serial(this, "COM32", 57600);
frameRate(20);
PFont myFont;
myFont = createFont("Arial", 20);
textFont(myFont);
controlP5 = new ControlP5(this);
controlP5.addSlider("Lenkung in %",-100,100,50,10,50,400,40);
controlP5.addSlider("Power in %",-100,100,100,30,150,40,400);
controlP5.addSlider("Licht",0,100,25,200,150,40,400);
controll = ControllIO.getInstance(this);
device = controll.getDevice("Logitech Cordless RumblePad 2");
device.setTolerance(0.05f);
ControllSlider sliderX = device.getSlider("Z-Achse");
ControllSlider sliderY = device.getSlider("Z-Rotation");
ControllSlider sliderX1 = device.getSlider("X-Achse");
ControllSlider sliderY1 = device.getSlider("Y-Achse");
stick = new ControllStick(sliderX,sliderY);
stick1 = new ControllStick(sliderX1,sliderY1);
// get right value for servo
button = device.getButton("Taste 0");
button1 = device.getButton("Taste 2");
}
void draw()
{
background(150);
String n1;
String n2;
String sending;
int speed = int(stick.getY()*-255+255); // get right value for motor controller
int steer = int((stick1.getX()*45)+90);
n1 = Integer.toString(steer);
n2 = Integer.toString(speed);
// if n1 or n2 is less than 4 digits add a zero to the front of them
while (n1.length() < 4){
n1 = '0' + n1;
}
while (n2.length() < 4){
n2 = '0' + n2;
}
//combine the two 4 digit long strings into one 8 digit long one
sending = n1 + n2;
//write this to the serial port.
myPort.write(sending);
println(sending);
controlP5.controller("Lenkung in %").setValue((steer-90)*2.5);
controlP5.controller("Power in %").setValue(map(speed, 0, 510, -100, 100));
fill(0);
text("Sendestring: "+ sending, 800, 580);
delay(20);
}
arduino:
#include <Servo.h>
String readString, servo1, speed2;
Servo myservo1;
int motor1 = 6;
int motor2 = 7;
int motorpwm = 5;
int standby = 4;
void setup() {
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(57600);
myservo1.attach(9);
}
void loop() {
while (Serial.available()) {
if (Serial.available() >0) {
delay(2);
char c = Serial.read();
readString += c;
}
}
if (readString.length() >0) {
Serial.println(readString);
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4);
speed2 = readString.substring(4, 8);
Serial.println(servo1); //print ot serial monitor to see results
int n1;
int n2;
char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
speed2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
myservo1.write(n1); //set servo position
readString="";
int power = map(n2, 0, 510, -255, 255);
if (n2 > 255){
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(standby, HIGH);
analogWrite(motorpwm, power);
}
if (n2 < 255){
digitalWrite(motor2, HIGH);
digitalWrite(motor1, LOW);
digitalWrite(standby, HIGH);
analogWrite(motorpwm, power*-1);
}
if (n2 == 255){
digitalWrite(standby, LOW);
analogWrite(motorpwm, 0);}
}
}