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):
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:
#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.