Timing question for making Neopixel and Servos work simultaneous

Dear community,

i hope you're all healthy in your homes.
Im using the time to understand Arduino better and started a little kinetic lamp project.

Everything worked out well so far, and i learned a lot.
But now I'm stuck with following problem:

My servos just move after each pixel move.
What means that the program just goes normally through the loop() section.

I want to let the servos and as well the Neopixel animation run in a multithread.

Think the solution is timer() and millis() but i have absolutely no idea of how to make it work.

Is anyone keen to explain to me how to use it in my code ?

  • I´m using 3 continuously servos and a normal one

Stay healthy and much love from Berlin


//positioning a continuous rotation servo
//using a delay to poistion the motor

#include <NeoPixelBus.h>
#include <NeoPixelAnimator.h>

#include <Servo.h>
Servo filthyServo1;
Servo NotSoFilthyServo2;
Servo OkayServo3;
//Servo CoolServo4;

const uint16_t PixelCount = 40; // make sure to set this to the number of pixels in your strip
const uint16_t PixelPin = 7; // make sure to set this to the correct pin, ignored for Esp8266
const uint16_t AnimCount = 1; // we only need one
const uint16_t TailLength = 5; // length of the tail, must be shorter than PixelCount
const float MaxLightness = 0.5f; // max lightness at the head of the tail (0.5f is full bright)

NeoGamma colorGamma; // for any fade animations, best to correct gamma
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object
long myTimer2 = 0;
long myTimeout2 = 7000;

void setup() {

// using Hsl as it makes it easy to pick from similiar saturated colors
float hue = (50) / 360.0f;
for (uint16_t index = 0; index < strip.PixelCount() && index <= TailLength; index++)
{
float lightness = index * MaxLightness / TailLength;
RgbColor color = HslColor(hue, 1.0f, lightness);

strip.SetPixelColor(index, colorGamma.Correct(color));
}

filthyServo1.attach(5);
filthyServo1.writeMicroseconds(2200); // Stop
NotSoFilthyServo2.attach(6);
NotSoFilthyServo2.writeMicroseconds(1500); // Stop
OkayServo3.attach(11);
OkayServo3.writeMicroseconds(1500); // Stop

// CoolServo4.attach(); // this is the only Servo that is not continuous
// CoolServo4.writeMicroseconds(1500; // Stop

}

void loop() {
filthyServo1.write(1520); //C-Clockwise
delay(1771);
filthyServo1.write(1500); //Stop
delay(800);
filthyServo1.detach();
delay(1000);

NotSoFilthyServo2.write(1526); //C-Clockwise
delay(1771);
NotSoFilthyServo2.write(1500); //Stop
delay(400);
NotSoFilthyServo2.detach();
delay(1000);

OkayServo3.write(1526); //C-Clockwise
delay(1771);
OkayServo3.write(1500); //Stop
delay(2000);
OkayServo3.detach();
delay(300);

// the only not continuous spinning servo
// CoolServo4.write(1526); //C-Clockwise
// delay(1771);
// CoolServo4.write(1500); //Stop
// delay(2000);
// CoolServo4.detach();
// delay(300);

filthyServo1.attach(11);
NotSoFilthyServo2.attach(6);
OkayServo3.attach(5);

}

2 issues. Firstly all the code can not be blocking as you made it. No more delay() look at the Using millis() for time in the tutorials of this forum page.
Secondly, If you are sending LED-strip signal you turn off interrupt, and the Servo.h library depends on them being turned on.

Have a look here: The Issue | Using NeoPixels and Servos Together | Adafruit Learning System