I’m learning how to use processing to communicate via serial with arduino. Using the basic code below works, both variables are getting written to the arduino. And they then get printed to the processing console. (serialConnection is a custom class which identifies the port on which the arduino is connected).
if (serialConnection.isReady) {
var_id = 1;
var_val = 127;
serialConnection.serialPort.write(var_id);
serialConnection.serialPort.write(var_val);
// println(1);
}
delay(50);
while (serialConnection.serialPort.available() > 0) {
int inByte = serialConnection.serialPort.read();
println(inByte);
// println(3);
}
}
Arduino code:
void loop() {
if (Serial.available()) {
for (int i = 0; i < 2; i++) {
serial_array [i] = Serial.read();
}
delay(50);
Serial.write(serial_array [1]);
// Serial.print(var);
}
}
Changing the code to check a Boolean variable (so the array in the arduino only gets written to once per cycle) causes the while serial available
statement to never execute. I cannot figure out why. No other changes made to the code.
if (serialConnection.isReady) {
if (data_processed == false) {
var_id = 1;
var_val = 127;
serialConnection.serialPort.write(var_id);
serialConnection.serialPort.write(var_val);
data_processed = true;
println(1);
}
delay(50);
while (serialConnection.serialPort.available() > 0) {
int inByte = serialConnection.serialPort.read();
println(inByte);
data_processed = false;
println(3);
}
}
I would appreciate any guidance for what could be going wrong.