I am currently busy with a project that requires me to use an Arduino and 3 potentiometers (to change background in RGB format) to write values through the serial port and read and separate them in processing.
What I want to do is to be able to use my Arduino to write multiple values to a processing program. I am quite new to coding and its been a while since I worked with my Arduino. I know how to read and write one value, but not multiple values.
Also is there a way to write 20 values?
Thanks in advance!
-
something like: "value1,value2,value3,value4,value5,value6.." might do?aaa– aaa2016年03月24日 12:54:27 +00:00Commented Mar 24, 2016 at 12:54
1 Answer 1
The simplest way is to just print them as a single line and separate them with "something". That "something" could be as simple as a space.
Serial.print(val1);
Serial.print(" ");
Serial.print(val2);
...
Serial.print(" ");
Serial.println(val20);
743 234 34 599 2 ... 427 1013 48 2 409
Then in processing you just read the entire line into a String and split it on the spaces into an array.
-
What would I use to split the values up? Is there a comand or is the spacing interpreted as a type of break?Dewan– Dewan2016年03月20日 21:34:48 +00:00Commented Mar 20, 2016 at 21:34
-
Well, Processing is Java. So maybe
String[] bits = myIncomingString.split(" ");
would do the trick...?Majenko– Majenko2016年03月20日 21:35:29 +00:00Commented Mar 20, 2016 at 21:35 -
Thanks for your help! I finally got it to work and am now able to write up to 18 values (only had 3 potentiometers close so i did a little division to distinguish the values). Aparently when using myPort.read() the diffrent bytes automatically get split up, so all you have to do is read and assign them. Of course after spending all thia time did I realise its basically excactly what the one example in Arduino's IDE does :/Dewan– Dewan2016年03月25日 18:26:16 +00:00Commented Mar 25, 2016 at 18:26