Stuck at combining 2 sketches into 1

Hi there!
I have 2 seperate sketches which work fine individually, but when I try to combine them the NeoPixels won't work. Does anyone know how to combine these sketches?

The NeoPixel sketch:

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define LED_PIN    6
#define LED_COUNT 12

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

unsigned long previousMillis = 0;
const unsigned long interval1 = 6900;
const unsigned long interval2 = 1440;
const unsigned long interval3 = 13940;
const unsigned long interval4 = 17620;
const unsigned long interval5 = 11600;
unsigned long intervalStartTime = 0;
int state = 0;

void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  strip.begin();
  strip.show();
  strip.setBrightness(200);
}




void loop() {
  unsigned long currentMillis = millis();
/*shot 1*/
  switch (state) {
    case 0:
      if (currentMillis - previousMillis >= interval1) {
        previousMillis = currentMillis;
        state = 1;
      }
      break;

    case 1:
      colorWipe(strip.Color(0, 0, 255), 20);
      state = 2;
      break;

    case 2:
      colorWipe(strip.Color(0, 0, 0), 20);
      state = 3;
      intervalStartTime = currentMillis;
      break;

    case 3:
      if (currentMillis - intervalStartTime >= interval2) {
        state = 4;
      }
      break;

    case 4:
      theaterChase(strip.Color(0, 0, 255), 20);
      state = 5;
      break;

    case 5:
      theaterChase(strip.Color(0, 0, 150), 20);
      state = 6;
      break;

    case 6:
      theaterChase(strip.Color(0, 0, 30), 20);
      state = 7;
      break;

    case 7:
      theaterChase(strip.Color(0, 0, 0), 20);
      state = 8;
      intervalStartTime = currentMillis;
      break;


/* shot 2 */
    case 8:
      if (currentMillis - intervalStartTime >= interval2) {
        state = 9;
      }
      break;

    case 9:
      if (currentMillis - previousMillis >= interval3) {
        previousMillis = currentMillis;
        state = 10;
      }
      break;

    case 10:
      colorWipe(strip.Color(0, 0, 255), 20);
      state = 11;
      break;

    case 11:
      colorWipe(strip.Color(0, 0, 0), 20);
      state = 12;
      intervalStartTime = currentMillis;
      break;

    case 12:
      if (currentMillis - intervalStartTime >= interval2) {
        state = 13;
      }
      break;

    case 13:
      theaterChase(strip.Color(0, 0, 255), 20);
      state = 14;
      break;

    case 14:
      theaterChase(strip.Color(0, 0, 150), 20);
      state = 15;
      break;

    case 15:
      theaterChase(strip.Color(0, 0, 100), 20);
      state = 16;
      break;

    case 16:
      theaterChase(strip.Color(0, 0, 50), 20);
      state = 17;
      intervalStartTime = currentMillis;
      break;

       case 17:
      theaterChase(strip.Color(0, 0, 25), 20);
      state = 18;
      break;

    case 18:
      theaterChase(strip.Color(0, 0, 0), 20);
      state = 19;
      intervalStartTime = currentMillis;
      break;
/* shot 3 */
    case 19:
      if (currentMillis - intervalStartTime >= interval4) {
        state = 20; // Reset to the initial state
      }
      break;


 case 20:
      colorWipe(strip.Color(0, 0, 255), 20);
      state = 21;
      break;

    case 21:
      colorWipe(strip.Color(0, 0, 0), 20);
      state = 22;
      intervalStartTime = currentMillis;
      break;

    case 22:
      if (currentMillis - intervalStartTime >= interval2) {
        state = 23;
      }
      break;

    case 23:
      theaterChase(strip.Color(0, 0, 255), 20);
      state = 24;
      break;

    case 24:
      theaterChase(strip.Color(0, 0, 150), 20);
      state = 25;
      break;

    case 25:
      theaterChase(strip.Color(0, 0, 30), 20);
      state = 26;
      break;

    case 26:
      theaterChase(strip.Color(0, 0, 0), 20);
      state = 27;
      intervalStartTime = currentMillis;
      break;


case 27:
      if (currentMillis - intervalStartTime >= interval5) {
        state = 0;
      }
      break;
      
  }
}

void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, color);
    strip.show();
    delay(wait);
  }
}

void theaterChase(uint32_t color, int wait) {
  for (int a = 0; a < 5; a++) {
    for (int b = 0; b < 3; b++) {
      strip.clear();
      for (int c = b; c < strip.numPixels(); c += 3) {
        strip.setPixelColor(c, color);
      }
      strip.show();
      delay(wait);
    }
  }
}

The servo sketch:

#include <Servo.h>

Servo servo_2;
Servo servo_3;
Servo servo_4;

uint32_t previousMillis = 0;
const uint32_t minDelay = 4000; // Minimum delay in milliseconds
const uint32_t maxDelay = 10000; // Maximum delay in milliseconds

void blockingMillisDelayRandom() {
  uint32_t randomDelay = random(minDelay, maxDelay + 1); // Random delay between minDelay and maxDelay milliseconds
  uint32_t currentMillis = millis();
  while (millis() - currentMillis < randomDelay) {
    // Wait for the randomly generated delay
  }
}

void setup() {
  servo_2.attach(2, 500, 2500);
  servo_3.attach(3, 500, 2500);
  servo_4.attach(4, 500, 2500);

  servo_2.write(90);
  servo_3.write(90);
  servo_4.write(90);
}

void loop() {
  // Wait for a random period of time between 4000 and 10000 milliseconds
  blockingMillisDelayRandom();

  servo_2.write(random(75, 105 + 1));

  // Wait for a random period of time between 4000 and 10000 milliseconds
  blockingMillisDelayRandom();

  servo_3.write(random(20, 160 + 1));

  // Wait for a random period of time between 4000 and 10000 milliseconds
  blockingMillisDelayRandom();

  servo_4.write(random(70, 110 + 1));
}

Start with a new main loop, then give each of the original loop functions a name such as void loop (){} change to void pixel(){} then in your new loop call it with pixel(); You will have to sort out the setup sections and be sure the labels are unique.

You will have problems running the Servo library at the same time as Adafruit_Neopixel. The Servo library relies on interrupts, the Neopixel library has to disable interrupts while updating the led strip.

The name of the function says it all. Nothing else will happen for four to ten seconds. Literally. Nothing. Else. Will. Happen.

Similarly, the two effects you exploit use delay() to make the timing happen. During the loops that make the effect, nothing else will get any attention.

This needs a complete re-write using an entirely different approach.

You can start to learn about it, settle in and google

arduino blink without delay

and

arduino two things at once 

and

arduino finite state machine

and

arduino finite state machine traffic lights

for a look at this essential method for getting the most out of these small machines we play with.

HTH

a7

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