Transfering data between Arduino and Processing sketches

I’m working on a project that is primarily on the Arduino, but uses a Processing app to configure the Arduino. I’ve devised a simple protocol to transfer data between the Arduino and the computer. There are 3 main parts to the protocol.

1: the computer sends data to the Arduino to request the current config data.
2: the Arduino replies with the current config data.
3: the computer can send new config data back to the Arduino

The problem I’m having is that none of this is really working. When the Arduino replies with the current config, the values the computer receives are not the same ones that are actually in the Arduino, and actually seem to be completely random. When the computer sends new config data to the Arduino, the Arduino doesn’t seem to receive the proper data, and can’t parse it into the relevant variables. Aditionally, the length of the data “packets” the computer sees coming from the Arduino seem to be completely random, despite having fixed-length arrays in every serial.write() line.

In addition to all of this, the buttons attached to the arduino, each of which sends a different midi command, send commands that don’t make any sense, and shouldn’t even be possible given my code. Bytes of the command which are pulled from a single variable for all 6 buttons are different depending on which button is pushed. That shouldn’t even be possible.

There’s so much wrong here I don’t even know where to start. Every time I fix one thing, something else breaks.

The code is a bit long to put in this post, so I’ve attached the files.

Archive.zip (14.3 KB)

I don't know Processing. The examples in Serial Input Basics may be useful on the Arduino side. The system in the 3rd example will be most reliable.

I'm sure you can implement a similar concept in Processing.

...R

I don't believe the problem I'm having is with the Processing side. The issue seems to be that the data the Arduino sends is different every time, and has no relation to what is in the variables it's pulling the data from. Calling the same function to send the same data 10 times results in 10 different sets of data. There's nothing writing to any of the config variables on the Arduino, so I have no idea what is going on.

If the Processing sketch asks the Arduino to send its config data back to the computer 5 times, the data returned will be different every time. It appears that some bytes are correct, but there is extra data randomly inserted in between the data from the array that the Arduino is sending.

The data that should be sent by the Arduino in a test I ran was a byte array with contents {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, -10}. As you can see below, the even bytes in the received data match up with the first 6 from the transmit buffer, but there is random data in every odd byte received. I don't know where this extra data is coming from, and have no idea how to stop it.

[0] 0   [0] 0   [0] 0   [0] 0   [0] 0
[1] 1   [1] 1   [1] 1   [1] 1   [1] 1
[2] 1   [2] 1   [2] 1   [2] 1   [2] 1
[3] 0   [3] 0   [3] -88 [3] -12 [3] 27
[4] 2   [4] 2   [4] 2   [4] 2   [4] 2
[5] -12 [5] -75 [5] 10  [5] 0   [5] 0
[6] 3   [6] 3   [6] 3   [6] 3   [6] 3
[7] 0   [7] 15  [7] 0   [7] -75 [7] 45
[8] 4   [8] 4   [8] 4   [8] 4   [8] 4
[9] -1  [9] -1  [9] 3   [9] 15  [9] 64
[10] 5  [10] 5  [10] 5  [10] 5  [10] 5
[11] -96    [11] -96    [11] 32 [11] -1 [11] -122
[12] 6  [12] 6  [12] 6  [12] 6  [12] 6
[13] 6  [13] 6  [13] 0  [13] -96    [13] -128
[14] -10    [14] -10    [14] -10    [14] -10    [14] -10

Can you post your Arduino code here so I don't have to download and extract a zip file that may contain stuff I don't like?

...R

Of course the solution was something blindingly obvious. I never actually cleared the receive buffer after a completed message. That fixed the whole thing.

vockleya: Of course the solution was something blindingly obvious.

Welcome to the club :)

...R