I have been trying to develop a connection from the arduino IDE to Processing so I can send data measured on the arduino Pins to processing to display.
I first packaged the 10-bit integers I was receiving from the arduino into characters each with their own byte. For example, 1023 was a 10-bit integer which i packaged into 4 8-bit characters, 1, 0, 2 and 3. These could then be sent over the serial port and were reconstructed on the other end by processing.
However, I have been trying to make a call and response relationship between the two where after one program is done receiving and processing the data, it asks the other to send the next set.
I used the example code given on the arduino website which works fine on its own. But when i try and implement the character packaging to it, it stops working. The output in processing always miss out the first couple of characters.
Whats more is that i deleted the myPort.print('A') from the processing code, and arduino was still sending data repeatedly, despite processing not sending it's response back.
It is a vague question, but where have i gone wrong when implementing the packaging code? and why does the arduino keep sending data regardless of whether processing responds.
Thanks a lot
===================================
Arduino Code:
int inByte = 0;
//void establishContact();
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
establishContact();
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
// get incoming byte:
//inByte = Serial.read();
int MzValue = analogRead(A0);
int FzValue = analogRead(A1);
int FyValue = analogRead(A2);
char Mz_Str[10];
char Fz_Str[10];
char Fy_Str[10];
sprintf(Mz_Str, "%d", MzValue);
sprintf(Fz_Str, "%d", FzValue);
sprintf(Fy_Str, "%d", FyValue);
// Now str contains the integer as characters
Serial.write(Mz_Str);
Serial.write(",");
Serial.write(Fz_Str);
Serial.write(",");
Serial.write(Fy_Str);
Serial.write(",");
Serial.write("\n");
delay(20);
}
else {
delay(20);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(1000);
}
}
=================================== Processing Code:
import processing.serial.*;
Serial myPort; // The serial port
boolean firstContact = false; // Whether we've heard from the microcontroller
String val; // Data received from the serial port
int numValues = 3;
float[] values = new float[numValues];
int inByte = 0;
void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Print a list of the serial ports for debugging purposes
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list());
// I know that the first port in the serial list on my Mac is always my FTDI
// adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw() {
// read a byte from the serial port:
if ( myPort.available() > 0) {
inByte = myPort.read();
}
//print(inByte);
// if this is the first byte received, and it's an A, clear the serial
// buffer and note that you've had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
println(inByte);
}
}
else {
val = myPort.readStringUntil('\n'); // read it and store it in val
println(val); //print it out in the console
values = float(splitTokens(val, ","));
myPort.write('A');
delay(20);
}
}
Serial.print(MzValue);
will do the same as your 'character packaging'