I'm using an HC-12 module with the SoftwareSerial library as a way to control LEDs (overkill, I'm aware :D), but whenever I use FastLED.show() or FastLED.delay() anywhere in my code around 30% of the serial data recieved by the arduino is corrupted. If i remove these terms the problem does not persist, but the LEDs are obviously not driven.
It seems to me that this is caused by FastLED's interrupts occuring in the middle of serial transfer. I have read the FastLED page for intterupts, and tried #define FASTLED_ALLOW_INTERRUPTS 0 and #define FASTLED_INTERRUPT_RETRY_COUNT 1, but these seem to have no effect.
I know the sent data is correct, as it's recieved perfectly without any fastLED code. The data used to be much larger, but right now it is only 1 character, and the corruption still occurs.
Any ideas? I'm very stuck!
The relevant code (with irrelevant parts cut out):
#include <SoftwareSerial.h>
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
SoftwareSerial HC12(5, 6); // HC-12 TX Pin, HC-12 RX Pin
CRGB leds[96];
CRGBPalette16 currentPalette = RainbowColors_p;
TBlendType currentBlending;
void setup() {
//Serial Setup
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
//FastLED Setup
FastLED.addLeds<WS2811, A5, GRB>(leds, 96).setCorrection( TypicalLEDStrip );
FastLED.setBrightness(64);
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
void loop() {
//Get character from hc-12
static char dataRecord = '0';
noInterrupts(); //Disable interrupts while getting character
while (HC12.available()) { // If HC-12 has data
dataRecord = HC12.read();
}
interrupts(); //Re-enable interrupts
//String prints to terminal and stores to prevent repeats
static char printChar = '0';
if(dataRecord != printChar){
printChar = dataRecord;
Serial.println(printChar);
}
static uint8_t startIndex = 0;
if(dataRecord == '0' || dataRecord == '1' || dataRecord == '2' || dataRecord == '3' ||dataRecord == '4' || dataRecord == '5'){ //stops refresh when erronious data is recieved
startIndex = startIndex + 1; //Changes motion speed
FillLEDs(startIndex);
FastLED.delay(10);
}
//Mode-dependant loop
if(dataRecord == '0'){
currentPalette = CRGBPalette16(CRGB::Black);
}
else if(dataRecord == '1'){
(...)
}
void FillLEDs(uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 0; i < 96; i++) {
leds[i] = ColorFromPalette(currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3;
}
}