Hi folks! I'm relatively new to Arduino and very new to FastLED.

Hi folks! I’m relatively new to Arduino and very new to FastLED. Very exciting library! I’ve been able to get the demos to run using an UNO and a 3 pin WS2812 led strip.

I’m currently trying to get some external control of the LED strip using OSC messaging. In the following code I’m able to get the OSC message to light the pin13 LED on the UNO using the /LED message. My problem is that my attempt to change the number of lit leds via the /fader message is not working. I am probably missing something basic about variables…

Any advice is greatly appreciated!

My Arduino Sketch:

#include <OSCMessage.h>
#include <SLIPEncodedSerial.h>
// these two includes are enough for our purpose
// we don’t have to import the whole OSC library

#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 3

#define BAUDRATE 57600
// 9600 is too slow

SLIPEncodedSerial SLIPSerial(Serial);

int analogValue = 0;
long oldTime = 0;
long newTime = 0;

CRGB leds[NUM_LEDS];

void setup()
{
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
pinMode(13, OUTPUT);
SLIPSerial.begin(BAUDRATE);

}

void loop()
{
int val = 0;
int sendval = 0;
val = sendval;
int numLedsToLight = map(val, 0, 1023, 0, NUM_LEDS);

//first clear the existing led values
FastLED.clear();
for(int led = 0; led < numLedsToLight; led++)
{
leds[led] = CRGB::Blue;
}

// read analog pin 0 and send an OSC message every 100 ms
newTime = millis();
if (newTime-oldTime > 100){
analogValue = analogRead(0);
// create OSC message and give it an address
OSCMessage msg("/analog");
// add data to the OSC message
msg.add(analogValue);
// make and send a SLIP packet
SLIPSerial.beginPacket();
msg.send(SLIPSerial);
SLIPSerial.endPacket();

 oldTime = newTime;

}

// look for incoming OSC messages:

// first check if bytes are available
while(SLIPSerial.available()){
// create empty OSC Message
OSCMessage msg;
// fill OSC message with incoming bytes till you reach end of p
while(!SLIPSerial.endofPacket()){
int size = SLIPSerial.available();
while (size–){
msg.fill(SLIPSerial.read());
}
}

 // now check the address 
 if (msg.fullMatch("/LED", 0)){
   // then check if first item is an integer
   if (msg.isInt(0)){
     // use the integer argument for controlling the LED 
     digitalWrite(13, msg.getInt(0));
   }
   
   // now check the address
 if (msg.fullMatch("/fader", 0)){
   // then check if first item is an integer
   if (msg.isInt(0)){
     // use the integer argument for controlling the number of lit leds 
     sendval = (msg.getInt(0));
   }
 }  

}

}

}

Change

int sendval = 0;

to

static int sendval = 0;

With the first definition, every time the loop function begins, sendval’s value is set to 0. The static modifier on the local variable says “keep the value each time we come into this function” - so now, when you set sendval from the msg, and exit the loop function, and come back into it - you will have the value you’re expecting.

Thanks so much for taking a look at this. I understand the use of “static” now. I made the change you suggested and still had no response from the strip. When I load another demo like blink or firstlight etc… .everything works. Then I load the following and the preloaded light behavior freezes (but does not go dark) and I still get no change in the strip as I change values coming in to /fader from 0-1 and 0-1023.

I’ve probably made some other simple mistake!

#include <OSCMessage.h>
#include <SLIPEncodedSerial.h>
// these two includes are enough for our purpose
// we don’t have to import the whole OSC library

#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 3

#define BAUDRATE 57600
// 9600 is too slow

SLIPEncodedSerial SLIPSerial(Serial);

int analogValue = 0;
long oldTime = 0;
long newTime = 0;

CRGB leds[NUM_LEDS];

void setup()
{
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
pinMode(13, OUTPUT);
SLIPSerial.begin(BAUDRATE);
}

void loop()
{
//int val = 0;
static int sendval = 0;
//val = sendval;
int numLedsToLight = map(sendval, 0, 1023, 0, NUM_LEDS);

//first clear the existing led values
FastLED.clear();
for(int led = 0; led < numLedsToLight; led++)
{
leds[led] = CRGB::Blue;
}

// read analog pin 0 and send an OSC message every 100 ms //
newTime = millis();
if (newTime-oldTime > 100){
analogValue = analogRead(0);
// create OSC message and give it an address
OSCMessage msg("/analog");
// add data to the OSC message
msg.add(analogValue);
// make and send a SLIP packet
SLIPSerial.beginPacket();
msg.send(SLIPSerial);
SLIPSerial.endPacket();

 oldTime = newTime;

}

// look for incoming OSC messages:

// first check if bytes are available
while(SLIPSerial.available()){
// create empty OSC Message
OSCMessage msg;
// fill OSC message with incoming bytes till you reach end of packet //
while(!SLIPSerial.endofPacket()){
int size = SLIPSerial.available();
while (size–){
msg.fill(SLIPSerial.read());
}
}

 // now check the address (to compare the full adress, set the second argument to 0) //
 if (msg.fullMatch("/LED", 0)){
   // then check if first item is an integer
   if (msg.isInt(0)){
     // use the integer argument for controlling the LED (zero = LOW, non-zero = HIGH) //
     digitalWrite(13, msg.getInt(0));
   }
    
 if (msg.fullMatch("/fader", 0)){
   // then check if first item is an integer
   if (msg.isInt(0)){
     // use the integer argument for controlling the number of lit LEDs 
     sendval = (msg.getInt(0));
   }
   
 }  

}

}

}

(Please use pastbin or gist to post code - trying to read code in g+ comments is a nightmare)

(I’ll take a look at your code in a few minutes when I get back)

You never call FastLED.show() which is how the LED values you set go out to the leds.

Ah… a fatal error indeed. including FastLED.show() definitely helped. OSC messaging seems to be connected but not exactly “working”. Describing new behavior:

I’ve gone back to the original last basic usage example and tried to more closely work with it.

In this pastebin code:
http://pastebin.com/DuZpXbjd

I use the analogRead(2) to set val. I then try to add to val using OSC messages changing the static int sendval;

I would expect sending integers 0-511 to sendval would result in the val+sendval sum to exceed 511 and thus light all of the lights on the strip.

Instead the # of leds lit hovers between 6-7 when not inputing any values.

When I send values via OSC (0-1023) I get between 8-11 leds to light but I can’t discern any relationship between the value I send and the # of leds lit.

Also, when I change the upper limit of the map function to values other than 511 or 1023 the entire code freezes. Is this a bitwise math problem?

Thanks for your advice!

So - have you tried printing out the values you’re receiving from OSC to make sure you’re getting them properly? Also - I’d say take out the analogRead for a moment, and just focus on trying to get the value sent via OSC to control the number of leds lit - that whole “walk before you run” thing.

Also - you have a logic problem in your code – the

if (msg.fullMatch("/fad", 0))

check is inside the true block for the

 if (msg.fullMatch("/LD", 0))

check - which means you’re never going to read the fader value - because if the msg matches “/fad” it’s, by definition, not going to match “/LD”, right?

THANKS! That was definitely it. I made a separate true block for /fad and /LD and now everything works as expected (with or without the analogRead). Once I get the project completed I’ll post a little video demo.

for anyone interested here is a pastebin of working code for bringing OSC messages into Arduino to control # of lit leds on WS2812: http://pastebin.com/YRuhvave

Still trying to solve this OSC communication with the fastLED library…

I have sketch working as expected that accepts integers from 0-60 to turn on the leds on the strip in succession (adapted from the “external control” example): http://pastebin.com/M1P9iqcW

I then created a sketch which I expected to allow me to turn on and off the first three leds using 1 and 0 messages via OSC.
http://pastebin.com/d8a4zFc6

However, the behavior is unexpected. Basically I send a 1 and most of the time get the result I would expect from 0.

Maybe I am screwing up the variables/if statements?? Any advice is greatly appreciated!

Below is a log of my OSC message, the Serial.println, and the leds result:

action: serial print: LEDs:

“/ld0 1” —
“/ld0 0” 1 0 goes pink
0

“/ld0 1” 1 0 goes black
“/ld0 0” 0 0 goes pink

“/ld0 1” 1 0 goes black
“/ld0 0” 0 0 goes pink

“/ld1 1” - 0 goes black
“/ld1 0” 1010 1 goes green

“/ld1 1” 1 1 goes black
“/ld1 0” 0 1 goes green

“/ld1 1” 1 1 goes black
“/ld1 0” 0 1 goes green

“/ld2 1” 1 1 goes black
“/ld2 0” 0 2 goes blue

“/ld2 1” 1 2 goes black
“/ld2 0” 0 2 goes blue

“/ld2 1” 1 2 goes black
“/ld2 0” 0 2 goes blue

It’s the order that you’re doing things - you are setting the LED values based on the values of led0, led1, and led2 - then you are waiting for the next message - then, only after you get the next message do you call FastLED.show(). So it seems like everything is one step behind.

I moved the OSC messaging ahead of the if statements but kept the
FastLED.clear at the very beginning of the void loop and the FastLED.show at the end. This seems to work! Is that the optimal placement for clear and show?? Thanks again!

I am getting very close to perfect working order here. Only one small problem (I think).

I have divided the led strip into 4 groups (evens 0-28, odds 1-29, evens 30-58, odds 31-59) I have incoming messaging allowing the use of faders to control hue and value for each of these 4 groups (full saturation).

My initial problem was:
When I tried to manipulate 2 of the VALUE faders simultaneously I got occasional 255/0 VALUE flickers in the simultaneously manipulated leds (not the static leds).

I added the delay(3); to the loop as I found in one of the forums and this got rid of the flickering when moving two VALUE faders at once.

However, now when I manipulate all 4 groups’ VALUE faders at once the flickering returns.

Manipulating all 4 of the HUE faders simultaneously causes no problems.

Manipulating individual faders to change hue or brightness causes no problems.

This is the arduino code: http://pastebin.com/1WqEfWYS

I can upload a video if that helps somehow.

Any advice is greatly appreciated!