Running Processing code locally on an Arduino

Hoping this isn't too much of a FAQ...I'm brand new to Arduino and not really sure what I'm doing yet.

Here's my situation: I've purchased an Arduino Nano with a USB host shield, and I'm hoping to use it to control a few sets of Triklits (TRIKLITS) via the USB4 interface. However, the only sample code I've been able to find for controlling Triklits is written in Processing as opposed to Wiring (http://www.3waylabs.com/triklits/trikout.pde). I know that I can use Firmata to establish communication between the Arduino and a PC running Processing, but for this application, it's not practical to have a PC in the mix. Short of rewriting the sample code in Wiring (which I highly doubt I have the chops to do just yet), what are my options? Any advice would be greatly appreciated!

Ethan

There isn't anything short of writing in Wiring (C/C++) if the Arduino is going to be standalone.

Luckily porting code from processing to wiring is quite easy as many commands and most syntax is pretty much identical. I was able to learn processing in just a couple weeks after having a pretty good understanding of processing and I think would be easier in the other direction as wiring is actually simpler. Let us know if you get stuck on a specific part of your code though

Thanks, AWOL, Kctess5. Right now I'm getting a bunch of errors when I try to run the sample code under Wiring:

triklits:-1: error: variable or field 'frame_gen' declared void
triklits:-1: error: 'color' was not declared in this scope
triklits:1: error: 'color' was not declared in this scope
triklits:1: error: expected primary-expression before 'int'
triklits:1: error: initializer expression list treated as compound expression
triklits:4: error: 'import' does not name a type
triklits:6: error: 'Serial' does not name a type
triklits:17: error: 'color' does not name a type
triklits:20: error: initializer fails to determine size of 'ftx'
triklits:24: error: initializer fails to determine size of 'frame'
triklits:25: error: initializer fails to determine size of 'hframe'
triklits:29: error: variable or field 'frame_gen' declared void
triklits:29: error: 'color' was not declared in this scope

So it looks as though I have my work cut out for me, but I'll give it the old college try and hope for the best.

Post the processing code so we have a frame of reference here. Depending on what it does it may not even be possible or useful in any way to run on an arduino

It's at that link I posted above: http://www.3waylabs.com/triklits/trikout.pde

FWIW, it runs just fine on my Mac, but Wiring clearly doesn't like the way it's trying to handle the serial communication (which is most of what it does!)

Wow thats a bit more complex that I was expecting. So looking at the links it seems like what you are doing is trying to control those lights from the arduino instead of a computer. This should be possible once you know how to use the communication protocol though it wont be simple. Luckily that information should be available by carefully going over the processing code and knowing exactly what it does and how. Most of that bitwise stuff should be pretty easy to port to wiring because the syntax looks the same. Also you will need to develop a way to control what the arduino tells it to do. If you just want one effect such as fading colors it will be really easy but if you want to have a list of effects that you can choose from you might want use a few buttons and maybe even an LCD. I don't know how much user input it requires though. Sorry I don't know many specifics on what the code does because deciphering that much complicated code would take a while.

Looks like they provide a lot of resources which will be nice for hacking it like this. Check the first one out for how to wire in the arduino and the second for info on the communication protocol

http://www.3waylabs.com/triklits/controlbox.html
http://www.3waylabs.com/triklits/protocol.html

After a quick look through, unless you're running the code on a Mega, you've almost certainly not got enough RAM to run the sketch as written.

Ohhhh drat. Maybe that's why I can't seem to find any Wiring examples. (The Triklits designer mentions that he made the software protocol more complex in order to simplify the hardware, which makes perfect sense but may end up killing me here...)

Well, back to the drawing board, I guess. If I have to add a cheapo laptop, so be it. They don't mind extreme heat and dusty conditions, right? :wink:

If you're going to need a PC, consider a Mini ITX or similar with some flavour of Linux running off a USB stick.

Aside from needing nearly 6000 bytes to hold the arrays, that code is not really that complex. I think it would be easy to port that to the Arduino, on a Mega.

Using PROGMEM is not feasible, because the arrays all need to be writable.

Acquiring a Mega is certainly cheaper that acquiring a PC that might not like the conditions where you plan to put it.

Is it really 6000 bytes for the arrays? I get 32243 + 324 + 776 + 388 = 3596, which it turns out I can easily reduce by at least 2824*3 = 2016, since I only need to control 4 strings of 24 lights (not 32). I'm wondering if that would get me at least within striking distance of the available memory on my ATmega328 Nano (32K flash, 1K EEPROM, 2K SRAM). Otherwise, yeah, a Mega might be my best bet, though I'm not clear on how hard it would be to interface with my existing USB shield (http://www.amazon.com/gp/product/B005BZITBQ).

Really appreciate all the insights.

You also need free RAM for your stack and all the other variables and function calls.

I get 32243 + 32*4 + 776 + 388 = 3596

32243 ints + 776 bytes + 388 bytes = 3596 whats?

though I'm not clear on how hard it would be to interface with my existing USB shield

Do you really have that shield controlling a USB device?

There is a similar shield, SparkFun USB-C Host Shield - DEV-21247 - SparkFun Electronics, that will fit the Mega.

PaulS:
32243 ints + 776 bytes + 388 bytes = 3596 whats?

My understanding was that the color type was three bytes, so it seems like it should be

color v[][] = new color[32][24]; (32243 = 2304 bytes)
int ftx[] = new int[32]; (32*4 = 128 bytes)
byte frame[] = new byte[776]; (776 bytes)
byte hframe[] = new byte[388]; (388 bytes)

for a total of 3596 bytes, reducible to 1580. Though as Senso points out, that's not all I need to squeeze in. (Am I right that this is the 2KB of SRAM that we're talking about here?)

I do have the Gravitech shield controlling the Triklits via USB with the Nano. I was looking at that Sparkfun shield, and it could definitely be an option. The only catch is that I need to have this working in 3 days or not at all, so the less hardware hacking I can get away with on top of the software hacking, the better!

My understanding was that the color type was three bytes, so it seems like it should be

color v[][] = new color[32][24]; (32243 = 2304 bytes)

I assumed that color was a collection of ints. So, I looked at the documentation:

Syntax

color(gray)
color(gray, alpha)
color(value1, value2, value3)
color(value1, value2, value3, alpha)
color(hex)
color(hex, alpha)

Parameters
gray int or float: number specifying value between white and black
alpha int or float: relative to current color range
value1 int or float: red or hue values relative to the current color range
value2 int or float: green or saturation values relative to the current color range
value3 int or float: blue or brightness values relative to the current color range

A little more looking:

From a technical standpoint, colors are 32 bits of information ordered as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB where the A's contain the alpha value, the R's are the red value, G's are green, and B's are blue. Each component is 8 bits (a number between 0 and 255). These values can be manipulated with bit shifting.

So, it turns out that neither one of us was right. A color is 4 bytes, not 3 as you assumed or 6 as I assumed. So, v takes 32 * 24 * 4 = 3072 bytes.

Ah, thanks...I must have been looking at the wrong docs. Figure I can swap out the types to avoid all that wasted space for the alpha channel.

Depending on your budget for the project it may be worth it to go on ebay for a used netbook. Considering that you can get a new one for 300 dollars you can probably find a workable one in the 100 dollar range. Thats about twice as much as a mega but you wouldn't have to port the code and it would probably be easier to use