Help With Controlling Servos & Led Strips Using Arduino

Hi everyone, I am trying to make a project with 4 servos and WS2812 Led strips. I managed to get the Led strips to work, though when I add the servo code, the servos tend to jitter like crazy. When I have the servo code alone, they work just fine. I did some research and it seems like it's something with the timing of WS2812 chips. Though, as I am just starting out with programming, I have no idea what I should do to fix this. Any help would be appreciated.

#include "ServoCos.h"
#include "FastLED.h"
#define LED_PIN     5
#define COLOR_ORDER GRB
#define CHIPSET     WS2812
#define NUM_LEDS    37
#define BRIGHTNESS  200
#define FRAMES_PER_SECOND 60

bool gReverseDirection = false;

CRGB leds[NUM_LEDS];

CRGBPalette16 gPal;

ServoCos HeadServo;
ServoCos HeadServo2;
int SweepDirection=1;

void setup() {

delay(3000); // sanity delay
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );

  // This first palette is the basic 'black body radiation' colors,
  // which run from black to red to bright yellow to white.
  //gPal = HeatColors_p;
  
  // These are other ways to set up the color palette for the 'fire'.
  // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
  //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
  
  // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
     gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  
  // Third, here's a simpler, three-step gradient, from black to red to white
  //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);

  HeadServo.attach(9);
  HeadServo2.attach(10);
  HeadServo2.attach(11);
  HeadServo2.attach(12);

}

void loop() {
    
    servo1();
 // servo2();
     fire();
}

void servo1() {
  
  if(HeadServo.hasStopped()){// there is also .isMoving()
    if(SweepDirection==1){
      SweepDirection=-1;
      HeadServo.setupEase(160,1500);
    }
    else
    {
      SweepDirection=1;
      HeadServo.setupEase(0,1500);
    }
  }
HeadServo.updatePosition();
}

Second Tab

void fire()
  {
    // Add entropy to random number generator; we use a lot of it.
    random16_add_entropy( random());

// run simulation frame, using palette colors

Fire2012WithPalette();

    FastLED.show(); // display this frame
    FastLED.delay(1000 / FRAMES_PER_SECOND);
  }

  // Fire2012 by Mark Kriegsman, July 2012
  // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  // Default 55, suggested range 20-100
#define COOLING  55

  // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  // Higher chance = more roaring fire.  Lower chance = more flickery fire.
  // Default 120, suggested range 50-200.
#define SPARKING 120

void Fire2012WithPalette()
{
// Array of temperature readings at each simulation cell
  static byte heat[NUM_LEDS];

  // Step 1.  Cool down every cell a little
    for( int i = 0; i < NUM_LEDS; i++) {
      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= NUM_LEDS - 1; k >= 2; k--) {
      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
      int y = random8(7);
      heat[y] = qadd8( heat[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = 0; j < NUM_LEDS; j++) {
      // Scale the heat value from 0-255 down to 0-240
      // for best results with color palettes.
      byte colorindex = scale8( heat[j], 240);
      CRGB color = ColorFromPalette( gPal, colorindex);
      int pixelnumber;
      if( gReverseDirection ) {
        pixelnumber = (NUM_LEDS-1) - j;
      } else {
        pixelnumber = j;
      }
      leds[pixelnumber] = color;
    }
}
#include "ServoCos.h"

That's a non-standard library. You need to provide a link AND explain why you are not using the standard servo library.

Servos need a frequent pulse stream to make them move/hold still properly. LED strips need frequent data updates to make them operate properly.

There may simply not be sufficient resources on whichever Arduino you are using to do both.

1 Like