» arduino04
This site relies heavily on Javascript. You should enable it if you want the full experience. Learn more.

arduino04

Reading multiple signals from Arduino serial.

Here I'll show a simple way to extend Mr Benefit's excellent serial reader patch to work with multiple signals, so that if you have a couple of light and proximity sensors and some pots all hooked up in your arduino you can work with all those inputs at once in vvvv.

In a nutshell:

Download the tiki-download_file.php?fileId=955 | the arduino code] and tiki-download_file.php?fileId=954 | the vvvv patch]. Run both and feed something into arduino's analog inputs. Finito.

The circuit:

I'm going to assume you can wire up multiple analog inputs. Look at the arduino tutorials (like this one on reading a potentiometer).

The arduino code:

tiki-download_file.php?fileId=955 | Download the arduino code]

The arduino program looks as follows. Load it into your board.

//Read and send all analog inputs out over serial (e.g. to vvvv).
//Sends a string with the format "an bn cn dn en fn\r\n"
//where:
// n is a non-negative decimal number (arbitrary number of digits)
// \r\n is carriage-return and a newline (ascii chars 10, 13)
//example: a812 b20 c3 d1017 e233 f96

void setup()
{

  Serial.begin(9600);

}

void loop()
{

  Serial.print("a");
  Serial.print(analogRead(0));
  Serial.print(" ");
  Serial.print("b");
  Serial.print(analogRead(1));
  Serial.print(" ");
  
  Serial.print("c");
  Serial.print(analogRead(2));
  Serial.print(" ");
  
  Serial.print("d");
  Serial.print(analogRead(3));
  Serial.print(" ");
  
  Serial.print("e");
  Serial.print(analogRead(4));
  Serial.print(" ");
  
  Serial.print("f");
  Serial.print(analogRead(5));
  Serial.print(" ");
  
  //print carriage return and newline
  Serial.println();
  delay(20);

}

Arduino program notes:

  • In contrast with Mr Benefit's code, I have used the new arduino serial library (Serial.begin(baud) and Serial.print()). The older serialWrite() functions are deprecated will eventually be phased out. See the arduino reference
  • Until you have the given system working between arduino and vvvv, don't be tempted to change anything in the code. Debugging this stuff isn't much fun. Once you know you've got a basic program that works, hack away!
  • Key to understanding (but not to using) the pair of programs is the fact that when you call Serial.print() (or the old serialWrite()) and pass it an integer, it generates a string representation of that number, and sends those characters. So Serial.print(203) will send the ascii characters '2', '0', and '3' in 3 consecutive bytes, rather than sending one byte having a literal value of 203. If you wanted to do the latter, you could do Serial.print(203, BYTE). Perhaps a future tutorial will show how do work like that, but not this one.

The vvvv patch:

tiki-download_file.php?fileId=954 | Download the vvvv patch].
I have simply added a regular expression to Mr Benefit's tokenizer-based patch, to split out the various inputs.

Be sure to change the com port on the RS232 (Devices) to match that which your arduino is found on, and the baud rate to 9600 or whatever your arduino code specifies in setup() via Serial.begin(baudRate).

We expect an input string in the format "an bn cn dn en fn\r\n"
where

  • n is a non-negative decimal number (arbitrary number of digits)
  • \r\n is carriage-return and a newline (ascii chars 10, 13)

An example would be : a34 b200 c1024 d1 e0 f331

One must use an inspector to gain full access to the RegExpr (String). In particular, the pattern itself is only available this way.
The regexp I used was taken almost directly from the vvv regexp faq:

a( \d+ ) \s b( \d+ )\s c( \d+ )\s d( \d+ )\s e( \d+ )\s f( \d+ )

Problems, limitations:

  • In this first version, it is very strict. If it doesn't receive text in the expected format, you will not get ANY of your values from the communication, even if some of them were ok up to a certain point. Be careful when adding or removing inputs.
  • To send less or more, one has to edit the regexp (see regexp faq for a better way)
  • executing a regexp every frame is probably expensive. We could just use a Separate (String) node, and use a separator character, dropping the a, b, c, etc.
  • this is much less efficient than sending values in the bytes. I've

done single-signal comms based on raw values - rather than ascii - from arduino to vvvv but haven't quite figured how to split the signals up again using the available vvvv nodes. If I could just write it in c or whatever, I'd be done already. Using human-readable characters does aid debugging, however.

Update - adding the digital outs

Expanding the communication to include arduino's digital inputs prove to be very simple. I have done this in a naive way, with the upside that it's very easy to follow. Later I have some notes on how it might better be done.

In a nutshell:

get the version 0.2 code -
Here's the arduino sender v0.2] and the tiki-download_file.php?fileId=959 | vvvv receiver v0.2]. Install on arduino and vvvv, adjust your comport as necessary, and start making use of the arduino's digital inputs.

v0.2 Notes

  • The format becomes "an bn cn dn en fn #BBBBBBBBBBBB\r\n" where:
    • n is a non-negative decimal number (arbitrary number of digits)
    • B is a binary digit, 1 or 0
    • \r\n is carriage-return and a newline (ascii chars 10, 13)
    • example:
a812 b20 c3 d1017 e233 f96 #101100100111
  • I naively used a regular expression again where we could instead have split the binary characters with a

Split (String) node, as that section of the string is fixed-width. That would have yielded a spread. There's no need for the '#' character either, other than making it easier for the programmer to tell where the digital stuff starts.

  • having chosen to use RegExpr (String), I hit a limitation of that node - no more than 14 matches. As we have 6 analog inputs and 12 digital inputs (18 signals in the string) it was necessary to take the substring holding the binary inputs, and send it to another regexpr node for splitting. I couldn't find this limitation documented anywhere.
  • we could have the arduino encode the 12 binary values as a single number in decimal from 0 - 2^12 (0-4095) (saving 8 bytes in of comms) and then decode it in vvvv (render the number as a binary string, then split the binary string as normal).
  • I haven't given much thought yet to how you, in the real world, would work with a subset of the arduino's inputs (you'll be using some of the arduino digital pins as outputs, I expect). In the absence of a super-wonderful arduino vvvv node, you can start with this patch and cut out what inputs you don't need, remembering to cut them simultaneously from transmitter (arduino) and receiver (vvvv) to avoid tears.

Debugging.

Some ideas for debugging / understanding these serial-comms patches:

  • Add a 2000ms delay to your arduino loop.
  • Create a MainLoop (VVVV) (unconnected) and set the max foreground fps to something lower. Note that the UI will also become unresponsive as you do this. Note that the rs232 will capture more per frame as you lower the frame rate (given a fixed arduino transmission rate).
  • for more complex regexps, use something like kodos regular expression debugger to develop the regexp, then convert into vvvv's syntax as necessary.

References

Download tiki-download_file.php?fileId=963 | a zip of all the vvvv-arduino patches] up to this point in this series of tutorials.

Good luck,

neillzero

back to toc

anonymous user login

Shoutbox

~19h ago

drehwurm: @udo I feel old at 36 with him too ;)

~20h ago

mrboni: @idwyr - try elliotwoods. He spends a good bit of time there

~22h ago

idwyr: Any tips on visiting Seoul? (vvvv/new media exibitions/interesting spots)?

~22h ago

tekcor: btw 3d printed bateries with graphene http://www.3ders.org/articles/20141024-graphene-3d-lab-unveils-first-3d-printed-graphene-battery.html There are also DIY instcruction how to produce graphene

~22h ago

tekcor: since vvvvjs can run any shadertoy code I will get the nvidia tk1 and see what i can do with it here with raytracing etc

~23h ago

metrowave: Checkout this 3D printer in full HP!

~24h ago

metrowave: @tekcor that's cool...future of online 3D gaming is WebGL: http://tinyurl.com/legzlu2

~1d ago