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));
}
}
}
}
}