Serial Com between Arduino and Processing

After a long hiatus I’m back to working on a communication protocol for linking Arduino and Processing.

I’ve hit a rather interesting snag…

On the Arduino side:

int demoVal[] = {20,30,40,50};
int demoValPrev[] = {10,10,10,10};
int checkSum;


void setup(){
  Serial.begin(9600);
}


void loop(){
  demo();
  sendBeerVals();
}


void sendBeerVals(){
  for(int e=0; e<4; e++){      /////////////Make sure beerVal[e] != a trigger value (1-9)
    if(demoVal[e]<10){
      demoVal[e]=10;
    }
  }
                           /////////// prepare the checksum
  checkSum = 0;
  for(int e=0; e<4; e++){
    checkSum = checkSum + demoVal[e];
  }
  
  Serial.write(1);           ///////send beerVal triggers (1,2,3)
 
  for(int e=0; e<4; e++){              ////////send beerVals
    Serial.write(demoVal[e]);
  }
  Serial.write(checkSum);              /////send checksum
  delay(100);
}
 
void demo(){
  for(int e=0;e<4;e++){
    if (demoVal[e]>=100) {
      demoVal[e] = 100-2;
    }
    if (demoVal[e]<=10) {
      demoVal[e] = 12;
    }
    if (demoVal[e]<demoValPrev[e]) {
      demoValPrev[e] = demoVal[e];
      demoVal[e]--;
    }
    if (demoVal[e]>demoValPrev[e]) {
      demoValPrev[e] = demoVal[e];
      demoVal[e]++;
    }
  }
}

I have:

  1. a simple function, demo(), that cycles four values between 10-100
  2. a function, sendBeerVals(), that:
    a) adds up my four values from demo and saves them in the variable checkSum
    b) writes a “1” (trigger value), the four demo values, and the checkSum value to serial.

Everything seems to be running fine on the Arduino side (until I looked at the data…)

On the processing side:

import processing.serial.*;
Serial beerPort;
int checkSumIn;
int checkSum;
int _b1Val;
int _b2Val;
int _b3Val;
int _b4Val;
int b1Val;
int b2Val;
int b3Val;
int b4Val;
long time = 0;


void setup(){
  size(100,100);
  beerPort = new Serial(this, "COM6", 9600);
}

void draw(){
  serialCheck();
  }


void serialCheck(){
  int trigger = beerPort.read();
  if(trigger == 1){
   // int trigger2 = beerPort.read();
   // if(trigger2 == 2){
    //  int trigger3 = beerPort.read();
    //  if(trigger3 == 3){
        _b1Val = beerPort.read();
        _b2Val = beerPort.read();
        _b3Val = beerPort.read();
        _b4Val = beerPort.read();
        checkSumIn = beerPort.read();
        checkSum = 0;
        checkSum = _b1Val + _b2Val + _b3Val + _b4Val;
        if(checkSum == checkSumIn){
          b1Val = _b1Val;
          b2Val = _b2Val;
          b3Val = _b3Val;
          b4Val = _b4Val;
          println(millis()-time);
          println(b1Val + "    " + b2Val + "    " + b3Val + "    " + b4Val + "    " + checkSum);
          time=millis();
        }

     // }
   // }
   }
  checkSum = 0;
  checkSum = _b1Val + _b2Val + _b3Val + _b4Val;
  if(checkSum == checkSumIn){
    b1Val = _b1Val;
    b2Val = _b2Val;
    b3Val = _b3Val;
    b4Val = _b4Val;
  }
}

I have the program check the serial port for the trigger value, 1.

If it finds a 1, it reads the serial port 5 more times, saving the demo values I sent from the Arduino into temporary variables and the checksum value I sent from the Arduino into the checkSumIn variable

It then adds the four temporary values together into checkSum, compares them to the checkSumIn, and if they match it passes the temporary variables into their respective variables, and prints the four values and checksum value to the monitor. It only passes on the values and prints to the monitor if the checksums match

I also added a line for a time stamp so that I can see how long it was between printing the values.

Here are the results

2707ms
b1:21 b2:31 b3:41 b4:51 checksum:144
100ms
b1:22 b2:32 b3:42 b4:52 checksum:148
100ms
b1:23 b2:33 b3:43 b4:53 checksum:152
100ms
b1:24 b2:34 b3:44 b4:54 checksum:156
100ms
b1:25 b2:35 b3:45 b4:55 checksum:160
100ms
b1:26 b2:36 b3:46 b4:56 checksum:164
100ms
b1:27 b2:37 b3:47 b4:57 checksum:168
100ms
b1:28 b2:38 b3:48 b4:58 checksum:172
100ms
b1:29 b2:39 b3:49 b4:59 checksum:176
100ms
b1:30 b2:40 b3:50 b4:60 checksum:180
100ms
b1:31 b2:41 b3:51 b4:61 checksum:184
100ms
b1:32 b2:42 b3:52 b4:62 checksum:188
100ms
b1:33 b2:43 b3:53 b4:63 checksum:192
100ms
b1:34 b2:44 b3:54 b4:64 checksum:196
100ms
b1:35 b2:45 b3:55 b4:65 checksum:200
100ms
b1:36 b2:46 b3:56 b4:66 checksum:204
100ms
b1:37 b2:47 b3:57 b4:67 checksum:208
100ms
b1:38 b2:48 b3:58 b4:68 checksum:212
100ms
b1:39 b2:49 b3:59 b4:69 checksum:216
100ms
b1:40 b2:50 b3:60 b4:70 checksum:220
100ms
b1:41 b2:51 b3:61 b4:71 checksum:224
100ms
b1:42 b2:52 b3:62 b4:72 checksum:228
100ms
b1:43 b2:53 b3:63 b4:73 checksum:232
99ms
b1:44 b2:54 b3:64 b4:74 checksum:236
100ms
b1:45 b2:55 b3:65 b4:75 checksum:240
100ms
b1:46 b2:56 b3:66 b4:76 checksum:244
100ms
b1:47 b2:57 b3:67 b4:77 checksum:248
100ms
b1:48 b2:58 b3:68 b4:78 checksum:252
7201ms
b1:78 b2:68 b3:58 b4:48 checksum:252
100ms
b1:77 b2:67 b3:57 b4:47 checksum:248
100ms
b1:76 b2:66 b3:56 b4:46 checksum:244
100ms
b1:75 b2:65 b3:55 b4:45 checksum:240
100ms
b1:74 b2:64 b3:54 b4:44 checksum:236
100ms
b1:73 b2:63 b3:53 b4:43 checksum:232
100ms
b1:72 b2:62 b3:52 b4:42 checksum:228
100ms
b1:71 b2:61 b3:51 b4:41 checksum:224
84ms
b1:70 b2:60 b3:50 b4:40 checksum:220
116ms
b1:69 b2:59 b3:49 b4:39 checksum:216
84ms
b1:68 b2:58 b3:48 b4:38 checksum:212
100ms
b1:67 b2:57 b3:47 b4:37 checksum:208
99ms
b1:66 b2:56 b3:46 b4:36 checksum:204
98ms
b1:65 b2:55 b3:45 b4:35 checksum:200
“I cut out the working part bc 9000 character limit”
100ms
b1:39 b2:29 b3:19 b4:13 checksum:100
100ms
b1:38 b2:28 b3:18 b4:14 checksum:98
101ms
b1:37 b2:27 b3:17 b4:15 checksum:96
98ms
b1:36 b2:26 b3:16 b4:16 checksum:94
100ms
b1:35 b2:25 b3:15 b4:17 checksum:92
100ms
b1:34 b2:24 b3:14 b4:18 checksum:90
100ms
b1:33 b2:23 b3:13 b4:19 checksum:88
101ms
b1:32 b2:22 b3:12 b4:20 checksum:86
99ms
b1:31 b2:21 b3:11 b4:21 checksum:84
100ms
b1:30 b2:20 b3:10 b4:22 checksum:82
100ms
b1:29 b2:19 b3:13 b4:23 checksum:84
100ms
b1:28 b2:18 b3:14 b4:24 checksum:84
99ms
b1:27 b2:17 b3:15 b4:25 checksum:84
99ms
b1:26 b2:16 b3:16 b4:26 checksum:84
100ms
b1:25 b2:15 b3:17 b4:27 checksum:84
100ms
b1:24 b2:14 b3:18 b4:28 checksum:84
100ms
b1:23 b2:13 b3:19 b4:29 checksum:84
100ms
b1:22 b2:12 b3:20 b4:30 checksum:84
100ms
b1:21 b2:11 b3:21 b4:31 checksum:84
100ms
b1:20 b2:10 b3:22 b4:32 checksum:84
100ms
b1:19 b2:13 b3:23 b4:33 checksum:88
100ms

For the most part it works great, each value changes by 1 with each iteration, and the time stamp shows 100ms between iterations until…

b1:47 b2:57 b3:67 b4:77 checksum:248
100ms
b1:48 b2:58 b3:68 b4:78 checksum:252
7185ms
b1:78 b2:68 b3:58 b4:48 checksum:252
100ms
b1:77 b2:67 b3:57 b4:47 checksum:248

it gets to this point, then it stops for 7+ seconds, and resumes again.

What does this have to do with Arduino???
What I believe is happening is that the Arduino is not sending any value higher than 255, once the checksum gets above 255, it no longer matches with the checksum calculated on the processing side.
The arduino program seems to still be sending values, as evidenced by 7 sec gap while it waits for checksum to get down lower than 255. I can only think that it stops at 255 because the Arduino is only sending one byte (but that doesn’t seem right)
I’m probably wrong, but it’s the only explanation I can come up with after looking at the data.

I was under the impression that I could use “Serial.write(int/long/float/etc.);” and it would automatically send two or four bytes,
and on the other end I could use “int/long/float/etc. SomeVariable = SerialPort.read();” and it would automatically read two or four bytes into that variable.
But that doesn’t seem to be the case.

Is my analysis correct? (hope not)
And how could I fix this?

Given that the values range from 10 to 100, why are you storing the values in ints?

What does Serial.write() do with an int? The function is defined only for bytes.

I was under the impression that I could use "Serial.write(int/long/float/etc.);" and it would automatically send two or four bytes,

No.

But that doesn't seem to be the case.

What a surprise.

void serialCheck(){
  int trigger = beerPort.read();
  if(trigger == 1){
   // int trigger2 = beerPort.read();
   // if(trigger2 == 2){
    //  int trigger3 = beerPort.read();
    //  if(trigger3 == 3){
        _b1Val = beerPort.read();
        _b2Val = beerPort.read();

Just because 1 byte has arrived does not mean that all the bytes you sent have arrived!

Mark