I need to read a string sent from processing to the arduino. Seems simple enough. I have a processing program that justs write "abc123" to the arduino via serial and a arduino program that trys to read it and send it back. The problem I am getting is that the arduino serial monitor is showing stuff like this:
abc123
ab23
ab23
ab23
ab23
ab23
bcac123
a123
a1
a1
1
Weird. Here is my arduino code:
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
//float foo = readFloatSerial();
//Serial.println(foo, DEC);
delay(5);
String foo = readStringUntil('\n');
Serial.println(foo);
}
}
String readStringUntil(char terminator) {
byte index=0;
byte timeout=0;
char data[18] = "";
while((data[index] != terminator) && index<18 && timeout<5) {
if(Serial.available() == 0) {
delay(8);
timeout++;
}
else {
data[index] = Serial.read();
timeout=0;
//Serial.println(data[index]);
index++;
}
//Serial.println(index,DEC);
}
//Serial.println(data);
return (String)data;
}
and processing code:
import processing.serial.*;
Serial arduinoPort;
void setup() {
size(100,100);
arduinoPort = new Serial(this,Serial.list()[0],9600);
}
void draw() {
background(50);
//arduinoPort.write(composePitch(0.1));
String str = "abc123";
arduinoPort.write(str);
delay(1000);
}
Could the problem be that my readStringUntil method is not getting the correct terminator character? As a side question, are there any library methods like readString() or readStringUntiL(char)? I know there are both of those in processing. Anyways, I hope someone can help me out here. Over 9000 kudos to someone who can! Thanks.