I have the Arduino mark each serial output with a letter character. This is the Arduino code:
Code:void loop(){
// read the analog input on pin 0:
analogValue = analogRead(0);
if (abs(analogValue-lastValue) > analogDelta) {
Serial.print(analogValue);
Serial.print("A");
lastValue = analogValue;
digitalWrite(ledPin, HIGH);
}
if ( analogValue == 0 & lastValue != 0) {
Serial.print(analogValue);
Serial.print("A");
lastValue = analogValue;
digitalWrite(ledPin, HIGH);
}
if (analogValue == 1023 & lastValue != 1023){
Serial.print(analogValue);
Serial.print("A");
lastValue = analogValue;
digitalWrite(ledPin, HIGH);
}
delay(10);
digitalWrite(ledPin, LOW);
}
In this scheme each analog output data is followed by character A. Here the interest is data change, not data over time. So this code writes to serial only when there is a data change. Not knowing how to handle 0 and 1023 is taken care of by the two crude exception if statements.
The following seems to work on the Processing side where MARKER is 65 for the letter A:
Code:
void setupSerial(){
port = new Serial(this, Serial.list()[2], 14400);
port.bufferUntil(MARKER);
}
void serialEvent(Serial port) {
dataStr = (port.readStringUntil(MARKER));
if (dataStr.length() >=2){
dataStr = dataStr.substring(0, dataStr.length()-1); // strip off the last char
potV = float(dataStr)*litersPerDataVal;
if ((dataStr != null) & (curTest < qtyTests)) { // ie stop after last test
arypotV[curTest] = potV;
if(arypotV[curTest] > arypotMax[curTest]){
arypotMax[curTest] = arypotV[curTest];
}
}
}
}
The serialEvent seems to occur autonomously whenever there is data keeping the data current enough for the rest of the application. I think the port.bufferUntil(MARKER) keeps the data from piling up. That used to happen before using bufferUntil.