FastLED.show() causes Servo to jump and jitter

Status
Not open for further replies.

teamo

Member
I have the problem, that I want a non blocking Servo-movement. I want other code to be processed while the servos are moving. I found out, that FastLED.show() causes the servos to jump and jitter all the time. For example, this code runs perfectly well:
Code:
#include <Servo.h> 
#include <FastLED.h>
#define SERVO1_PIN 5

#define SERVO2_PIN 4
#define NUM_LEDS 10

#define LEDS_PIN 8
Servo servo1;
Servo servo2;
int pos=0;
int directionOfServo=1;
uint8_t clr=0;
int counter=0;
CRGB leds[NUM_LEDS];
void setup() { 
  servo1.attach(SERVO1_PIN);
  servo2.attach(SERVO2_PIN);
  FastLED.addLeds<WS2812, LEDS_PIN, RGB>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() 
{ 
      counter++;
  if(counter>=10000) {
    Serial.print("Counter:");Serial.println(counter);
    counter=0;

    pos=pos+(directionOfServo*10);
    if(pos==180 || pos==0) {
      directionOfServo=directionOfServo*-1;
    }
    servo1.write(pos);  
    
    
    
  }
  clr++;
  for(int i=0;i<NUM_LEDS;i++) {
      leds[i]=CHSV(clr,244,255);
    }
  //FastLED.show();
}
But if I comment in the last line (FastLED.show()) the entire Servo movement goes completely nuts. Even the servo2 (connected to another pin and only attached never moved) starts to jump forth and back as soon as FastLED.show() is commented in. Do you have any idea how that can be solved? Or: Can someone at least tell me the cause of this?
 
Your interrupts from Servo and FastLed will be clashing. Perhaps try upping the priority of the Servo interrupt?
 
What version of FastLED are you using? More recent versions of the library have the ability to allow interrupts to be handled on the teensy 3.x - though interrupts will be fully disabled for 30µs at a time. I don't know whether or not this is going to end up being too long for the server library and the way it does its interrupts (though, I'd like to think 30us is ok).

My general advice for people looking to mix things that use interrupts (like servos or the audio library) is to go to an led chipset that isn't quite so timing sensitive, like the APA102 or LPD8806.
 
Your interrupts from Servo and FastLed will be clashing. Perhaps try upping the priority of the Servo interrupt?
Thank you both for your reply. This solution seems more practical dor my current situation because i am unable to switch leds. Can you outline how to do it or point my nose to the right sources? I have no idea how to priorize the servo interrupt.
 
Have a look over at this thread.

The jist of what you want is:
Code:
NVIC_SET_PRIORITY(IRQ_PORTA +16, 20)

Assuming your interrupt pin is on PortA
 
FastLED's disabling of the interrupts for 30µs at a time is still going to be occurring, however (again, assuming that you're using the 3.1 release or master@HEAD - if not, then they will be disabled for about 300µs at a time. Increasing the priority doesn't override __disable_irq().
 
FastLED's disabling of the interrupts for 30µs at a time is still going to be occurring, however (again, assuming that you're using the 3.1 release or master@HEAD - if not, then they will be disabled for about 300µs at a time. Increasing the priority doesn't override __disable_irq().

Good point. Take out the noInterrupts();
 
Good point. Take out the noInterrupts();

WS2812 led timing requires interrupts be blocked for the 30µs of writing out 24 bits of data - which is why I had the recommendation to switch leds to something like the APA102/LPD8806 which don't have timing requirements.

Another possibility would be to use FastLED's OctoWS2811 controller (https://github.com/FastLED/FastLED/wiki/Parallel-Output ) which sidesteps this problem by using Paul's OctoWS2811 library to drive the 2812's via DMA.
 
Thats a good idea! I still have an octo flying around somewhere here so i should use that. Although I am using fastleds hsv library to create the color scheme. Is there a more comfortable way to connect fastled fast and direct to octows?
 
Thats a good idea! I still have an octo flying around somewhere here so i should use that. Although I am using fastleds hsv library to create the color scheme. Is there a more comfortable way to connect fastled fast and direct to octows?

The HSV function is portable, I actually use it it in a lot of things
 
Ah what is your expirience concerning speed with approx 1000 leds:
4strips with 144
1 with 480 leds

Each led might have a different value. Is the correct way to simply use the hsv object then get rgb and pass that to the octows led or is it possible to simply use octows as a chipset in fastled.add ?
 
Just to give you a quick update: Switching from the current config to OctoWS turned out to be more invasive than I initially thought. Instead I used SoftwareServo-Library to make one of my attiny85 move the servos. I did not yet attach them to the main circuit , but do you think this would work as well if I'd use it as a "co-processor"? Apart from that: I am not familiar with the communication how to realize communication between teensy and attiny85s. Would it be a good way to have a clock-line and a data-line and to send a bit each x microseconds? Do you think the whole construct might work or is it doomed to fail?
I'll keep you updated, but it'd be nice if you'd have some hints for me, since you saved me a lot of time with your previous hints.
 
Status
Not open for further replies.
Back
Top