For some reason, after about a minute of running our Arduino program that controls our lights they start to go haywire. It’s as if the update rate of the main loop() has stopped updating regularly. The LEDs don’t appear to recieve values that are not sent by the program, only the timing is wrong. I have tried removing the delay; the problem persisted.
Oddly enough, the problem resolves itself if a new value is sent over Serial by the RIO. Not immediately, but within a second or two. And the problem isn’t permanently fixed, it will come back eventually.
#include "FastLED.h"
#define COLOR_ORDER GRB
#define MAX_BRIGHTNESS 255
//Tell it how many leds are in the strip. AndyMark's 2.5 meter strip has 150 leds
#define NUM_LEDS 265
// This is an array of leds. One item for each led in your strip
CRGB leds[NUM_LEDS];
//CSK 3/17/2014 I moved this to a pin that doesn't conflict with Ethernet functions in case you want to control LEDs via Ethernet
#define DATA_PIN 6 //White wire from the http://www.andymark.com/product-p/am-2917.htm power connector
int mode = 0;
//This function is used to setup things like pins, Serial ports etc.
//Here we specify which chipset our LEDs run off of by our choice of config function
void setup()
{
// Uncomment one of the following lines for your leds arrangement.
// FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
//FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
//CSK 2/12/2016 This is the correct chipset for the am-2916 LED strip
FastLED.addLeds<WS2812B, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
// FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
//FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
//***This is the chipset in the AM-2640 LED strip***
//CSK 3/17/2013 Changed to this function to allow direct data and clock pin specification
//FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
delay(250);
//clear() turns all LEDs off
FastLED.clear();
FastLED.setBrightness(MAX_BRIGHTNESS);
fill_solid( leds, NUM_LEDS /*number of leds*/, CRGB( 125, 125, 125) );
FastLED.show();
// start serial port at 9600 bps:
Serial.begin(9600);
makelookuptable();
}
void loop()
{
FastLED.show();
delay( 50 );
FastLED.clear();
int curTime = millis();
for( int led_number = 0; led_number < NUM_LEDS; led_number++ ){
switch( mode ){
case 0: //PULSE_RED
leds[led_number] = CRGB( 125 * (fastSin( curTime * 0.005 ) + 1), 0, 0 );
break;
case 1: //PULSE_GREEN
leds[led_number] = CRGB( 0, 125 * (fastSin( curTime * 0.005 ) + 1), 0 );
break;
case 2: //PULSE_BLUE
leds[led_number] = CRGB( 0, 0, 125 * (fastSin( curTime * 0.005 ) + 1) );
break;
case 3: // SOLID_RED
leds[led_number] = CRGB( 255, 0, 0 );
break;
case 4: // SOLID_GREEN
leds[led_number] = CRGB( 0, 255, 0 );
break;
case 5: // SOLID_BLUE
leds[led_number] = CRGB( 0, 0, 255 );
break;
case 6: // MOVING_RED
leds[led_number] = CRGB( 125 * (fastSin(curTime * 0.005 + led_number * 0.5) + 1), 0, 0 );
break;
case 7: // MOVING_GREEN
leds[led_number] = CRGB( 0, 125 * (fastSin(curTime * 0.005 + led_number * 0.5) + 1), 0 );
break;
case 8: //MOVING_BLUE
leds[led_number] = CRGB( 0, 0, 125 * (fastSin(curTime * 0.005 + led_number * 0.5) + 1) );
break;
}
}
}
String last = "";
void serialEvent() {
while (Serial.available()) {
char c = Serial.read();
if( c == '
' ){
mode = last.toInt();
last = "";
}else{
last += c;
}
}
}
const int approx_2pi = 63;
double sin_lookup[approx_2pi];
void makelookuptable(){
for( int x = 0; x < approx_2pi; x++ ){
sin_lookup x ] = sin( x * 0.1 );
}
}
double fastSin( double input ){
int places3 = (int)(input * 10);
int approximation = (places3 % approx_2pi );
return sin_lookup approximation ];
}