Hereby an example implementation to control FastLED with 11 byte commands.

Hereby an example implementation to control FastLED with 11 byte commands. A lightweight protocol, so it should be also easy to use with something like Bluetooth.

I’ve implemented the suggestions from @Daniel_Garcia done in this discussion topic: https://plus.google.com/+KasperKamperman/posts/X8WMCHVE5Jh

It’s not an attempt from me to make a standard, because I think everyone makes their own effects. I hope this is a good starting point for people to experiment with controllable parameters over the Serial port. Suggestions and improvements are welcome of course.

Command structure. Based on my own needs:
https://drive.google.com/file/d/0B3Tn6oRmh71oS3FNQ2hsODVwS1E

Processing code. Sliders to change different parameters. You’ll need the ControlP5 library. Its tested with the newest Processing 3.0b4.
https://gist.github.com/kasperkamperman/6c7c256f7042a3ca5118

Arduino code. Just replies the received command. I didn’t implement FastLED.
https://gist.github.com/kasperkamperman/7e827bb481dfcdb4cb52

Cool! Thanks for sharing, and that’s a very simple and broad-based way to go… lots of uses I bet.

Thanks. My current transmission is pretty limited. This could open up a whole new range of possibilities.

This is sort of motivating me to share the simple param passing protocol that I sometimes use for debugging and config changes! I’ll add that to the queue… but what you have here looks pretty solid and ‘industrial strength’.

@Kasper_Kamperman What micro controller are you using? I just tried compiling the Arduino code (tried Arduino 1.0.6 and also 1.6.5) for a Teensy 3.0 and it’s giving this error for code line 32:
invalid conversion from ‘uint8_t* {aka unsigned char*}’ to ‘char*’ [-fpermissive]

@marmil I tested with an Arduino Uno on 1.6.5. I added the uint8_t cast because otherwise I got this error: conversion for argument 1 from ‘cmd*’ to ‘uint8_t* {aka unsigned char*}’

Does (byte*) work? That seems better (since the function is readBytes):

Serial.readBytes((byte*) &aCmd,sizeof(struct cmd));

Any thoughts about a closing byte? Maybe it’s smart to see if you truly have a complete message, otherwise it might be smart to clean the buffer.

I thought adding a 12th byte that is the inverse of the type (so the 1th byte). So if byte#1 = 3 then byte#12 = 252.

I think the speed impact is pretty low:

if(aCmd.type+aCmd.closing==255) {
// we have a complete message
}
else {
// flush the buffer
}

I found an UNO and that worked. Changing Serial.readBytes((uint8_t*) to Serial.readBytes((byte*) didn’t work on the Teensy 3.0. Anyone know the format needed to make it compile on the Teensy?

I like the idea of a check, give it a try. Your summation comparison check is an interesting idea. That’s getting into coding ideas/practices I’ve never looked into. Is there any benefit to doing something like that vs just checking for a specific character you send at end (like a # sign for example, byte value 35)?

@marmil Serial.readBytes((char*) &aCmd,sizeof(struct cmd));

This compiles on the Teensy and Arduino.

I think the benefit is that you check also the first byte indirectly. I think some protocols use the inverse of a specific byte, thats how I got that idea. But I’m not an expert in this field either. I did a quick search for some best practises, but I couldn’t really find something.

That worked. I just tried out the Processing interface and like it. Cool. I think it has good potential. Thanks for sharing.

If read something more on CRC, checksums and packing messages in an envelope. However that makes things more complicated, although the message size is fixed. I think protocols like Bluetooth already have error checking build in, so no need to do this on this level.

“If it ain’t broke, don’t fix it”.

Some resources:

http://eli.thegreenplace.net/2009/08/12/framing-in-serial-communications/

That is perfect for my web-based ESP8266/Arduino hybrid! Thanks.

I’ve updated the protocol with an extra padding byte. Explanation from @Daniel_Garcia : The reason is because compilers on 32 bit systems like to align data - so 16 bit items like to be on 2 byte boundaries, 4 byte items on 4 bytes, etc…

So it will be 12 bytes now, urls mentioned in the original post stay the same.

Hi Kasper, What kind of performance are you able to get with this implementation? I’ve been trying to get something working with Pure Data and Arduino/FastLED but as soon as I get three faders moving simultaneously the flickering starts. I’ve tried OSC and am now using raw slip packages… but I’m still getting the flickering when I change 3+ commands at the same time. I’m happy to share the code with anyone interested.

I didn’t test with sending multiple values at the same time. In order to test if values are received well, I would check if you receive them with the serial command window. Or you could use a dummy command. For example connected the three faders directly to three different leds.

It’s difficult to say where the problem is. Keep in mind that Arduino is single thread. So receiving and processing the commands takes time… So you have to be smart with timing (I’ve posted an interpolation example somewhere, that’s how I deal with this).

When I Serial.println(); for a particular variable stream I will frequently get things like this when trying to “fade” from 0-100:
0
1
2
3
4
5
67
8
9
10
11
1213
14
15
16
etc…

I would be very interested to see your interpolation example!

Maybe you can share the code where this serial.println happens on http://gist.github.com. The interpolation example: https://gist.github.com/kasperkamperman/59438e86914ac74ed01d

Thanks for sending! I’ll check it out.

here is the code I’ve been using which is giving flickers when changing 3 or more values simultaneously:
https://gist.github.com/z1snow/ec1e9b170cbd6ce77d570a0bdbba6689

I use a pd patch which is sending 2 part messages (tag# value) encoded using the [slipenc] object from the mrpeach library. When printing from within PD everything looks perfect. It is only when printing from the arduino IDE that I get what seem to be the glitches described above.

Any advice is welcome!

The problem is that this blocks your code. So you might get glitches. Serial.println also takes time. If you send a lot of packages, the while loop keeps running (and takes longer).

In my method I just read one command from the buffer. Nothing more. The next command will be read the next time (since the buffer can hold some data). For my it doesn’t matter if one packet is one frame later (you won’t notice it).

Forgive me… the constrain function in the above code made things much worse. Here is the original: https://gist.github.com/z1snow/092a8902ca0620901a38bbd978c404b1