Running dual functions at the same call time

Unsure if this has been addressed (as I have looked, albeit not deeply, into similar topics. I have two separate things I am trying to do from the same call time. When the PIR is activated, I am wanting the Servo process to start (and the red and blue LEDs to activate at the appropriated times) and also the NeoPixel LED strip to start. They both work, just the Strip will start after the red LED goes off. When I try to rearrange the coding to accommodate both, I get definition errors.
As I am fairly new to coding terms, is there a way to have both the Servo/DigitalWrite process and the ColorWipe/Fade of the Strip to both activate at the same time when the PIR Sensor state ==1?

If I have posted the code wrong, please be gentle as I thought I followed protocol by using the appropriate item above.

// C++ code
//
#include <Servo.h>
#include <Adafruit_NeoPixel.h>

//Which pin is the Servo connected
Servo servo_9;

// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN   11

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 20

// Fading interval
#define ledFadeTime 5

//Initial PIR Sensor state
int pirState = 0;

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  pinMode(4, INPUT); //PIR Sensor
  pinMode(5, OUTPUT); // Blue LED
  pinMode(6, OUTPUT); // Red LED
  servo_9.attach(9, 0, 180); // Servo motor
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
}

void loop()
{
  pirState = digitalRead(4); //PIR is idle
  digitalWrite(5, LOW); //Blue LED off
  digitalWrite(6, LOW); //Red LED off
  if (pirState == 1) // PIR is triggered
  {
    servo_9.write(180); //servo goes in motion CW
    digitalWrite(6, HIGH); //Blue LED is on
    delay(2000); // Wait for 2000 millisecond(s)
    servo_9.write(0); //servo goes in motion CCW
    delay(2000); // Wait for 2000 millisecond(s)
    servo_9.write(180); //servo goes in motion CW
    delay(2000); // Wait for 2000 millisecond(s)
    digitalWrite(6, LOW); // Blue LED is OFF
    digitalWrite(5, HIGH); // Red LED is ON
    servo_9.write(0); //servo goes in motion CCW
    delay(2000); // Wait for 2000 millisecond(s)
    digitalWrite(5, LOW); // Red LED is OFF
    strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
    // Fill along the length of the strip in various colors...
    colorWipe(strip.Color(255,   100,   0), 100); // Amber
    delay(500);                           //  Pause for a moment
    rgbFadeInAndOut(0, 160, 255, ledFadeTime); // Blue
    //colorWipe(strip.Color(  0, 165,   255), 50); // Light Blue
  }
 
}
void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}
void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
  for (uint8_t b = 0; b < 255; b++) {
    for (uint8_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
    }
    strip.show();
    delay(wait);
  };
  for (uint8_t b = 255; b > 0; b--) {
    for (uint8_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
    }
    strip.show();
    delay(wait);
  };
  for (uint8_t b = 0; b < 255; b++) {
    for (uint8_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
    }
    strip.show();
    delay(wait);
  };
  for (uint8_t b = 255; b > 0; b--) {
    for (uint8_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
    }
    strip.show();
    delay(wait);
  };
  strip.setBrightness(200);
  for (uint8_t b = 0; b < 255; b++) {
    for (uint8_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
    }
    strip.show();
    delay(wait);
  };
};

you got the code tags wrong (you have to paste your code in between the three back ticks). I fixed that for you.


regarding your question, the code does exactly what you say....

    servo_9.write(180); //servo goes in motion CW
    digitalWrite(6, HIGH); //Blue LED is on
    delay(2000); // Wait for 2000 millisecond(s)
    servo_9.write(0); //servo goes in motion CCW
...

things happen in sequence, with the delays in between

you need to get familiar with final finite state machine and using millis to handle time

For extra information and examples look at

Hi @chadwick1234 this is one of the most commonly asked questions on the forum. It's basically the same question as "how do I get the Arduino to do 2 things at once". There's a topic that covers it at the top of the Project Guidance section of the forum.

Finite

indeed.. excuse my French !

presumably you're already starting to realize that while one "thing" you're doing is simply waiting, using a delay(), many other things can be done. The trick is to capture what that "thing" is doing as a state(s) and a time so that when the time is expired it can continue doing stuff based on the state(s).

this requires reorganizing how things are done and what the state(s) are.

if you have several different things to do, it would help to have separate functions for each that can be called when a timer expires and which know what to do based on the state(s)

i'm fond of data driven approaches, which in this case may be a table where each element captures the next step(s) for some "thing" and possible a time to wait. the state(s) may simply be the index into the table.

this allows steps to change by only changing data, not logic

i'm reluctant to say process (or task) because they have a more specific meaning with multi-tasking operating systems (OS). but there is an enormous # of things that can be done without an OS, often more efficiently

Thank you GCJR, J-M-L and PaulRB for the timely and congenial responses. I will be researching your advice, try-fail-try and will update when all goes according to plan. Be Blessed!

Yes, problem is use of delay..
has to be removed completely..

did some Async Leds..

should get you started..

good luck.. ~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.