Go Down

Topic: problems reading data from Processing in arduino (Read 931 times) previous topic - next topic

lordprimoz

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
Code: [Select]

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:

Code: [Select]

#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

Grumpy_Mike

So what isit returning and what do you expect it to return?

lordprimoz

hello Grumpy_mike,

thanks for the reply.
the Data that is being sent by processing is correct. In my arduino code i first had this code which was reading in all the data and returning it correctly:

Code: [Select]

void serialEvent()
{
  if(Serial.available() >17)
  {
   Serial.readBytesUntil('\n',input, 18)
  }
for(int i = 0; i <18; i++)
{
    Serial.write(input[i]);
}
}



which was being read in by processing with this code:
Code: [Select]

byte[] inbuffer = new byte[18];
while(port.available() >18)
{
   inbuffer = port.readBytes();
   port.readBytesUntil('\n',inbuffer);
   if(inbuffer != null)
   {
     String myString = new String(inbuffer);
     println(myString);
   }
   println("test:");
  }


but my problem is not sending the data to the arduino, as i have had that working. Im having issues manipulating the data i get in the arduino so i can use that data to control my servo's.

PaulS

Code: [Select]
  if(Serial.available() >17)
  {
    while(Serial.peek() != '\n')
        {
          char c = Serial.read();
           string1 = string1 + c;
        }
    Serial.print(string1);
}

You know that there are 17 characters to read. Why do you need a String? Use a char array.

Where do you reset string1? (Or the char array, when you get smart).

At some point, it just might actually be necessary to read that carriage return to make it go away.

lordprimoz

like i said, i had it working with a char array:

Code: [Select]
void serialEvent()
{
  if(Serial.available() >17)
  {
   Serial.readBytesUntil('\n',input, 18)
  }
for(int i = 0; i <18; i++)
{
    Serial.write(input[i]);
}
}


which worked fine for returning the data i sent to the arduino.
I also preffer arrays, and the code in which i used the string is another test.

The problem is that i want to drive a servo with the data i get from my pc.
I tried using this code to get the values split up, but this wouldnt work for me:
Code: [Select]


char *p = input;
char *str;
Serial.begin(115200);
while ((str = strtok_r(p, ",", &p)) != '\n') // delimiter is the semicolon
{
   value[z] = *str;
   z++;
   p = NULL;
}


any ideas?

PaulS

Quote
any ideas?

Yes. The strtok_r() function is quite complex, and intended for use on multithreaded systems. Since you are not using threads on the Arduino (for good reason; you can't), there is no reason to use a thread-safe version of the strtok() function. The strtok() function has two other advantages. First, it is a lot smaller. Second, it is a lot less complicated.

Finally, "but this wouldnt work for me" doesn't mean squat. The code did something. You expected it to do something. What either of those somethings is is not at all clear.

However, look at what you are doing. You are parsing a string pointed to by p. Then, you store the token in p. Then, you set p to NULL, and expect to get the next token from a NULL string. How can you possibly expect to get a token from a NULL string?

lordprimoz

how would you use the strtok() function then?
say i have my input array filled with 090;090;00;0;0;0;0 ( input[18] )
and i would want to read out the first 3 values  090, 090 and 00. ( the others will be for later, i dont need them at this time)

PaulS

Quote
how would you use the strtok() function then?
say i have my input array filled with 090;090;00;0;0;0;0 ( input[18] )
and i would want to read out the first 3 values  090, 090 and 00.

Code: [Select]
int tokenCount = 0;
int tokenValues[3];

char *token = strtok(input, ",");
while(token && tokenCount < 3)
{
   // Do something with token
   tokenValues[tokenCount++] = atoi(token);

   token = strtok(NULL, ",");
}

// Now, tokenValues[] contains 90, 90, and 0

lordprimoz


Go Up