Enclosed are the sections of Arduino & Processing Functiosn
// Processing Collection of functions to send data to Ard //<>// //<>// //<>//
/*
There are flags for when a message or ping has been sent. When a flag is TRUE,
it means a msg or data has been sent but not acknowleded.
*/
String procACK = "P";
String inBuf = " ";
String dataPak = " ";
String[] serialInArray = new String[30]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int inByte;
int lf = 10, ctr = -1; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
byte Mode; // '0' = wait
void sendData(long dst, long brg) {
dataPak = (':' + str(dst) +':' + str(brg) + ':' + '\n');
//dataPak format = (":123:45:");
comPort.write(dataPak);
delay(6);
//comPort.write(0);
msgSent = true;
}
// Process incoming msgs from Arduino
void receiveData() {
mcMsg = '.';
charct = 0;
if (comPort.available()>0) {
comBuf = comPort.readStringUntil(lf);
delay(2);
}
if (comBuf != null) mcMsg = comBuf.charAt(0); // Message from Arduino/Motor controller
switch(mcMsg) {
case 'A':
isConnected = true;
wasConnected = true;
//ardup = true;
break;
case 'K':
msgSent = false; // The msg was recieved at Arduino
//isConnected = true;
//wasConnected = true;
break;
case 'Z': // P is the real Ping ack - z for debug
//isConnected = true; // Ard sent a Ping ack
//wasConnected = true;
pingsent = false; // Ack from Ard Ping has been received
pingAck = true;
break;
default:
//wasConnected = pingAck = false;
break;
}
}
void sendTest() {
String tstmsg = "123:345:567:888";
for (i = 0; i<10; i++) {
comPort.write(tstmsg);
delay (200);
}
}
Here Follows Ard Code:
boolean recieveData() {
msgin = false;
len = 0;
cnt = ComLink.available();
inbuf = ComLink.readStringUntil(10);
delay(5); // wait for next char
// Evalueate data
if (inbuf[0] == 'A' && !haveConnected) {
haveConnected = true;
ComLink.write("K\n"); // ACK - Tell Proccing we are here
//ComLink.read(); // Flush inbuf
}
else if (inbuf[0] == 'E') { // Enable ACT Steppers
ACTDST.enableOutputs();
ACTBRG.enableOutputs();
}
else if (inbuf[0] == 'D') { // Disable ACT Steppers
ACTDST.disableOutputs();
ACTBRG.disableOutputs();
}
else if (inbuf[0] == 'P') {
ComLink.write("P\n"); //Ping response
//ComLink.read();
//Serial.println(pingCtr++);
}
else if (inbuf[0] == ':') {
//buf = ComLink.readStringUntil(10);
sDst = getValue(inbuf, ':', 1);
sBrg = getValue(inbuf, ':', 2);
dstA = long((sDst.toInt()) / DSTDIV); // Set direction
brgA = long((sBrg.toInt()) / BRGDIV);
prvBrgA > prvBrgA ? Bdir = 1 : Bdir = -1;
ComLink.write("K\n"); //Ack Msg Rcved
msgin = true;
}
return (msgin);
}
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}