Hello guys,
Im having problems reading in data which i send from Processing(pc) to the arduino.
this is my processing code, it reads in data from my xbox controller and writes a string over serial in this format: xxx;xxx;xx;x;x;x;x with the first 3 digits being x-axis data, the next 3 y-axis, then z-axis, and a,b,x,y buttons. as initial value is should return 090;090;00;0;0;0;0
import procontroll.*;
import java.io.*;
import processing.serial.*;
Serial port;
ControllIO controll;
ControllDevice device;
ControllStick stick1,stick2;
ControllSlider slider1;
ControllButton button1, button2, button3,button4,button5,button6,button7,button8,button9,button10;
void setup(){
char[] data_array= new char[12];
size(400,400);
port = new Serial(this, "COM4", 115200);
controll = ControllIO.getInstance(this);
device = controll.getDevice("Controller (XBOX 360 For Windows)");
device.setTolerance(0.05f);
ControllSlider sliderX = device.getSlider("X Axis");
ControllSlider sliderY = device.getSlider("Y Axis");
ControllSlider sliderRX = device.getSlider("X Rotation");
ControllSlider sliderRY = device.getSlider("Y Rotation");
stick1 = new ControllStick(sliderX,sliderY);
stick2 = new ControllStick(sliderRX,sliderRY);
slider1 = device.getSlider("Z Axis");
slider1.setMultiplier(50);
button1 = device.getButton("Button 0");
button2 = device.getButton("Button 1");
button3 = device.getButton("Button 2");
button4 = device.getButton("Button 3");
button5 = device.getButton("Button 4");
button6 = device.getButton("Button 5");
button7 = device.getButton("Button 6");
button8 = device.getButton("Button 7");
button9 = device.getButton("Button 8");
button10 = device.getButton("Button 9");
fill(0);
rectMode(CORNERS);
}
void draw(){
//make interface and show values on it
background(255);
fill(0,255,0);
//FILL ZONE!!!
rect(100,100, 50, 100 + slider1.getValue());
line(50,50,50,150);
line(50,50,100,50);
line(100,50,100,150);
line(50,150,100,150);
ellipse(200+ stick1.getX()*50 ,100 +stick1.getY()*50,5,5);
ellipse(300+ stick2.getX()*50 ,100 +stick2.getY()*50,5,5);
//NO FILL ZONE!!!!!
noFill();
ellipse(200,100,100,100);
line(150,100,350,100);
line(200,50,200,150);
ellipse(300,100,100,100);
line(300,50,300,150);
fill(0);
text("ingedrukte knoppen", 50, 250);
String txt = "";
if(button1.pressed())txt += "knopA ";
if(button2.pressed())txt += "knopB ";
if(button3.pressed())txt += "knopX ";
if(button4.pressed())txt += "knopY ";
if(button5.pressed())txt += "knopLB ";
if(button6.pressed())txt += "knopRB ";
if(button7.pressed())txt += "back ";
if(button8.pressed())txt += "start ";
if(button9.pressed())txt += "knopLP ";
if(button10.pressed())txt += "knopRP ";
text(""+txt,50, 275);
int tmp1 = (int)90-round(stick1.getY()*90);
int tmp2 = (int)90-round(stick1.getX()*90);
//port.write((int)tmp2);
//port.write((int)tmp2);
//make string to send over serial//
String temp = "";
if(tmp1 < 1) temp = temp+"000";
else if(tmp1 <10) temp = temp +"00"+(int)tmp1;
else if( tmp1 <100)temp = temp +"0"+(int)tmp1;
if(tmp1 > 100) temp = temp+(int)tmp1;
temp = temp + ";";
if(tmp2 < 1) temp = temp+"000";
else if(tmp2 <10) temp = temp +"00"+(int)tmp2;
else if( tmp2 <100)temp = temp +"0"+(int)tmp2;
if(tmp2 > 100) temp = temp+(int)tmp2;
temp = temp + ";";
temp = temp + nf((int)slider1.getValue(),2);
temp = temp + ";";
temp = temp +(int)button1.getValue()/8;
temp = temp + ";";
temp = temp +(int)button2.getValue()/8;
temp = temp + ";";
temp = temp +(int)button3.getValue()/8;
temp = temp + ";";
temp = temp +(int)button4.getValue()/8;
//print(temp + "\n");
//
port.write(temp +'\n');
}
i want to split the strings into values i can use to let a servo follow my controller input, and later to controll ESC's.
this is the code for the arduino i have at this moment, but the data it returns isnt what im sending to the arduino:
#include <Servo.h>
#include <Wire.h>
//#include <stdlib.h>
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservo;
//Servo myservo1;
//int incomingByte1 = 0;
//char input[18];
//char value[7];
String string1;
void setup()
{
Serial.begin(115200);
// myservo.attach(9);
// myservo.write(0);
delay(3000);
}
void loop()
{
}
void serialEvent()
{
if(Serial.available() >17)
{
while(Serial.peek() != '\n')
{
char c = Serial.read();
string1 = string1 + c;
}
Serial.print(string1);
}
}
ive been trying to get it to work for a couple of days now, and i dont know what im doing wrong....
it might be something really small.
could anyone help me out?
thanks