We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I have code, which prints out an 8x8 array of doubles into the serial port in Arduino (live updated from sensor). How do I get processing to read these into its own array? I have an array globally defined as
float[][] array = new float[r][c]
where r and c are how many rows and columns there are. How do I turn this into an array that gets live updated from the serial port? If any more info is needed I will be happy to provide it, thanks!.
Answers
https://Forum.Processing.org/two/discussion/12757/transmission-of-a-float-through-a-serial-link
From your arduino, you send every cell in your array in certain order. For instance, you can read your 2D array taking one row at the time and sending each element in that row in order. In the Processing side, every time you receive an element, you put it back into your 2D array. This time, you put the items using the same idea, you fill by columns starting from the first row. When you are done filling out a row, you move to the next one until you fill each and all the rows in your array.
You should share your ino code as it will help... it will be easier to provide better feedback. For the time being, check this post: https://forum.processing.org/two/discussion/22677/issue-when-displaying-data-from-a-text-file-to-a-8x8-led-matrix
Kf
Thanks for the quick replies!
kfrajer, my arduino code has
Serial.print(pixels[i-1]);
to send the code to the serial monitor, where pixels[i-1] is a float, so it sends 1 float at a time. and Processing should be able to read that float 1 at a time, then I use a nested for loop to fill my 8x8 array, 1 reading at a time, right?
I guess my question is as basic as what is the command to read a float from the serial port?
My goal is to display the 8x8 array once it is filled and have it live update from the sensor.
I'm being dumb aren't I, I don't know any processing, only a little arduino.
So your floats are being converted to ASCII text: https://www.arduino.cc/en/Serial/Print
You should separate each float token with a comma (",") and then the end of the matrix should be signaled with an end of line character. Then in Processing, you detect the comma character to retrieve each number in your array and then you use
readBytesUntil()
to read data of your whole matrix. In other words, you first retrieve all the data of your matrix using:https://processing.org/reference/libraries/serial/Serial_readBytesUntil_.html
and then you split the received string into array elements. To do that, you can use either split or splitTokens. Check the reference to see which one is more convenient for you:
https://processing.org/reference/split_.html
https://processing.org/reference/splitTokens_.html
Check one of the many examples in the forum that could help you on this: https://forum.processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1
Kf
Thanks for the response again.
Looking at the readBytesUntil page, the one thing that confuses me is how many bytes can I expect if I'm sending 64 floats with 3 decimal accuracy through the serial port? Thank you
That's 3 questions about the same piece of code. How do we know which one to answer?
The best thing you can do first is to use
prinltn()
in your Processing code so you understand what you are getting. Now, from the link above:So a float number will be made of at least 4 chars (aka. 4bytes assuming its value is less than 10: first byte is the whole part of the number, one char for the decimal and 2 numbers corresponding to the decimal fraction... but remember floats can be bigger than 10 so a float size can be at least 4 chars).
If you are sending 64 floats, then you will be receiving (64) *(4+1)=250 chars aka 250 bytes. Notice I use (4+1) as I am assuming all numbers sent have 4 chars and I am adding one char extra for the comma separator.
However, you do not need to do this calculation that I just did. Instead, what you do is to check the size returned by
readBytesUntil()
function and you process your data stream only after a'\n'
end of line character is found:https://processing.org/reference/libraries/serial/Serial_readBytesUntil_.html
(.... Notice there is this `https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html so you should definitely check it out for your own good)
If you are trying to do the bilinearInterpolation, it is better first not to do it but instead, create a small program that allows you to extract data between your devices. Only after you understand what you are getting from your device, then you add the interpolation code.
Kf