I'm trying to build a GUI that sends the data from processing to arduino via serial. With the help of a couple of guys on here, I have come up with a basic framework that sends a variable ID, variable value and termination character (\n). So far so good!
However, the [arduino reference][1] advises to only use char
for storing characters. How would I send a byte
or int
over serial, while still being able to use a termination character to ensure everything is working as expected?
edit: Using the arduino code from Edgar's answer below and the following processing code, I get the "could not parse value" error.
import processing.serial.*;
SerialConnection serialConnection;
boolean data_processed = false;
char[] var_array = new char[31];
char var_id;
char var_val;
char space = ' ';
char termchar = '\n';
void setup() {
serialConnection = new SerialConnection(this, 9600);
}
void draw() {
serialConnection.startSerialCommunication();
if (serialConnection.isReady) {
var_id = 'a';
var_val = 127;
serialConnection.serialPort.write(var_id);
serialConnection.serialPort.write(space);
serialConnection.serialPort.write(var_val);
serialConnection.serialPort.write(termchar);
delay(50);
while (serialConnection.serialPort.available() > 0) {
for (int i = 0; i < 30; i++) {
char inchar = serialConnection.serialPort.readChar();
var_array[i] = inchar;
print(var_array[i]);
}
}
}
}