Pages: [1]   Go Down
Author Topic: Processing to Arduino serial communication. Looking for some help.  (Read 2116 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello forum.

Anyway, to get to the point, I'm having some difficulty sending some data from Processing to Arduino. I've tested it out a fair bit, and I know that I'm getting information from my phone and I'm packing it into a 10 digit long string which I'm sending over the serial to Arduino.
I also know that I can unpack the 10 digit long string on the other side to control two servo motors and a dc motor.
Now, I've previously been able to control a servo using the phone by writing the rounded float value straight to the serial and on to Arduino, so I know that I can send such data across, though that was only three digits maximum in size.

The problem is that the string data cannot bridge the gap between the two, and I'm not sure where I'm going wrong. Is anyone able to help. My code and a more detailed description of what I'm doing follows:






What I'm doing:

Essentially, I'm taking pitch, roll and touch data from an android phone using Charlie Robert's Control and sending it to Processing.
After I'm finished receiving it, I turn the floats into ints, and the ints into strings so that I can send it over to Arduino to be unpacked.

Here's the code for Processing (as you may guess, it's a whole bunch of sketches from around the place cut up to try and get what I want to do work):

Code:
import processing.serial.*;

Serial myPort;

import oscP5.*;
import netP5.*;

float y2;
float z2;

String yy;
String zz;
String ii;
String sending;

OscP5 osc;


void setup() {
  osc = new OscP5(this, 10000);
  osc.plug(this, "tilting", "/tilt");
  osc.plug(this, "touching", "/touch");
  println(Serial.list());
    myPort = new Serial (this, Serial.list()[0], 9600, 'N', 8, 1.0);
}




void draw () {

  sending = yy + zz + ii;
  myPort.write(sending);
  println (sending);
}


void touching(int i) {
 
ii = Integer.toString(i);
ii = '0' + ii;

}


void tilting(float x, float y, float z) {
 
  yy = nf(round(map (y, 0, 127, 0, 180)), 3);
 
  zz = nf(round(map (z, 0, 127, 0, 180)), 3);
 
  yy = '0' + yy;
 
  zz = '0' + zz;

 
  delay(10);
}


On the Arduino side of things, I determine what pins I want to use and then I start to unpack the series of numbers that I'm anticipating.

(Note that a general sort of string I'm hoping to receive looks like this: 0060007000. Here, the first 0 is a padding digit, the next three digits are the values from 0-180 for the first servo. This is followed by another padding 0, and then the next three digits for the second servo. The second last digit is a padding 0 and the last digit is either a 0 or 1 depending on whether the screen is pressed.)

I unpack the string and assign the extracted values to y,z, and i. Then I just send those values to the servos. I've tested this code with the serial monitor, and it seems to be working perfectly, but still, there might be something wrong with the fact that I'm sending a 10 digit string over the serial, I'm not sure.

Here's Arduino's code:

Code:
#include <Servo.h>

String readString, servoY, servoZ, servoI;

Servo myservoY;
Servo myservoZ;
Servo myservoI;



int potpin = 0; 
int val;   

void setup()
{
  Serial.begin(9600);
  myservoY.attach(9); 
  myservoZ.attach(10);
 
 // Serial.println("servo test");
}


void loop()
{
while (Serial.available()) {
  delay(10);
  if (Serial.available() >0) {
    char c = Serial.read();
    readString += c;
  }
}

if (readString.length() > 0) {
 
  servoY = readString.substring(0,4);
  servoZ = readString.substring(4,8);
  servoI = readString.substring(8,10);

  //Serial.println("y: " + servoY + ", z: " + servoZ + ", i: " + servoI);
 
  int y;
  int z;
  int i;
   
  char carrayY[10];
  servoY.toCharArray(carrayY, 10);
  y = atoi(carrayY);

  char carrayZ[6];
  servoZ.toCharArray(carrayZ, sizeof(carrayZ));
  z = atoi(carrayZ);
 
  char carrayI[6];
  servoI.toCharArray(carrayI, sizeof(carrayI));
  i = atoi(carrayI);

  //Serial.print("y converted: ");
  Serial.println(y);
  //Serial.print("z converted: ");
  Serial.println(z);
  //Serial.print("i converted: ");
  Serial.println(i);


 
  myservoY.write(y);
  myservoZ.write(z);

 
  if (i>0) {
    pinMode(5, HIGH);
  } else {
    pinMode(5, LOW);
  }
 
  readString = "";
  Serial.flush();
}
}
 


Just to reiterate so you don't have to scroll all the way back up to the top, my present issue is the fact that I can't get that string of digits from Processing to Arduino. For some reason. If you'd like any further details, just let me know.
Any help/advice on what I'm most certainly doing wrong would be hugely appreciated.


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be a lot simpler if you sent start and end of packet marker. That way, the Arduino could read until it found an end of packet marker.

Change Processing:
  sending = "<" + yy + zz + ii + ">";

Then, some code like this on the Arduino:
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
Where it says "Process the packet" is where you deal with the contents of the inData array without having to use the cursed String class. (Changing Process to use , between values would let you use strtok() to get the tokens, and atoi() to convert the tokens to values.)

And get rid of that stupid call to flush().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry about the incredibly long pause, but thanks PaulS. A few little tweaks and whatnot have got it working. Sort of. (I'm super useful like that)

The problem is to do with Processing, which may not be of anyone's strong suit here, but I'll ask anyway. Once I start up the Arduino, then get Processing running, I can see from println() that the packet of information I'm sending is something like: <0060,0070,null> This is up until I press the button on my phone, whereupon it changes to <0060,0070,00> (well the last set of digits were 01 for a second while I pressed it, but you get the idea). Ok, so all I need to do is press the button once to have it start sending the right info? Well, not really. See, after a short while, around five to ten seconds after I press the button and replace the null with 0/1, Processing just...stops.

No indication, no error messages, it just stops sending information. If anyone knows why this is, or has a way to fix it, please let me know.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Once I start up the Arduino, then get Processing running, I can see from println() that the packet of information I'm sending is something like: <0060,0070,null>
Using some code we can't see...

Quote
This is up until I press the button on my phone, whereupon it changes to <0060,0070,00> (well the last set of digits were 01 for a second while I pressed it, but you get the idea).
Your phone? What does your phone have to do with anything?

Quote
See, after a short while, around five to ten seconds after I press the button and replace the null with 0/1, Processing just...stops.
No, I don't see. I don't see your phone code. I don't see what using your phone to talk to Processing has to do with the Arduino forum.

Perhaps if you posted on the Porsche forum, they could help you.
Logged

Pages: [1]   Go Up
Jump to: