j3nsykes On Processing each pin corresponds correctly and reads fine but in P5 the pin readings seem mixed up in order or a reading that should be pin 23 is displaying as pin
I am sending some mock data:
100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000
and they are received sometimes correctly sometimes not. I assume it's because there is no guarantee they are received at once (i.e.: that all the data is passed to serialRead() at once). So you should be checking for line breaks in there and only reset the index when a new line character is detected.
Try this:
function parseSerialBuffer(buffer)
{
console.log("Parsing " + buffer);
try {
// Parse string to extract information
let inString = trim(buffer); // remove whitespaces from beginning/end
let values=[];
values = int(split(inString, " ")); // Split string using space as a delimiter and cast to int
for(let i = 0; i < values.length; i++) {
if(i < gNumSensors) {
gSensorReadings[i] = values[i];
//console.log( gSensorReadings[i])
}
}
}
catch(e) {
e.printStackTrace();
}
}
/// Read Serial
let inputBuffer = "";
function serialRead(data) {
// Read data from the serial buffer
for(let n = 0; n < data.length; ++n)
{
let c = data[n];
// copy data to temp buffer
inputBuffer += c;
if('\n' == c){
// when we find a newline, we process what we have so far
parseSerialBuffer(inputBuffer);
// and then start over
inputBuffer = "";
}
}
}
Also, please remove console.log(data) from the for (let i = 0; i < gNumSensors; i++) loop in visualiser(), or you'll be flooding the console with meaningless data and send the CPU spinning by printing 30 lines every time draw() is called.