Two codes work perfectly when separate but not when combined.

I have two sets of codes.

  • One has alternating LEDs blinking
  • One has a NeoPixel ring spinning

When running separately, they work nicely. When I try to combine them, the results are not as expected. The sets of codes are pretty simple, but I must be making a mistake. I was hoping someone could help me out

Alternating LEDs Blinking

void setup()
{
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  delay(250); // Wait for 250 millisecond(s)
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  delay(250); // Wait for 250 millisecond(s)
}

NeoPixel Ring Spinning

#include <Adafruit_NeoPixel.h>

#define PixelPin 6
#define PixelCount 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
uint32_t colour = 0x00FF00; // Default to Green

int pos = 0;

void setup()
{
  pixels.begin();
  pixels.setBrightness(85); // 1/3 brightness
}

void loop()
{
  int start = (millis()%1000) * PixelCount / 1000L;   // one rev per second

  for(int i = 0; i<5; i++) {
    pixels.setPixelColor((i+start) % PixelCount, colour);   
  }
  for(int i = 5; i<PixelCount; i++) {
    pixels.setPixelColor((i+start) % PixelCount, 0);
  }
  pixels.show();

}

Combined

#include <Adafruit_NeoPixel.h>

#define PixelPin 6
#define PixelCount 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
uint32_t colour = 0x00FF00; // Default to Green

int pos = 0;

void setup()
{
  pixels.begin();
  pixels.setBrightness(85); // 1/3 brightness
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{

  {
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    delay(250); // Wait for 250 millisecond(s)
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    delay(250); // Wait for 250 millisecond(s)
  }

  int start = (millis() % 1000) * PixelCount / 1000L; // one rev per second

  for (int i = 0; i < 5; i++) {
    pixels.setPixelColor((i + start) % PixelCount, colour);
  }
  for (int i = 5; i < PixelCount; i++) {
    pixels.setPixelColor((i + start) % PixelCount, 0);
  }
  pixels.show();

}

the results are not as expected

What did you expect ?
What did you get ?

Are the delay()s causing problem I wonder ?

Expected:

I expected that the LEDs would continue to blink alternating and the NeoPixel Ring would spin one revolution per second.

Actual:

The LEDs still blink, but the NeoPixel Ring blinks back and forth - Like roughly pixels 1-4 blink then pixels 9-13 blink alternating. There is no spinning.

You have 2 lots of blocking code, first the LED flashes, taking half a second, during which the NeoPixels do nothing, then the reverse, the NeoPixels do their thing and the LEDs do nothing.

I get the feeling you have not learnt the basics of C/C++, if so please do the tutorials. I am certain you don’t understand how to write non-blocking code and how to use millis properly. These 2 tutorials will help:
Using millis for timing
Demonstration for several things at the same time

Enjoy :slight_smile:

the neopixel makes direct use of the function millis() which counts milliseconds.
If you do a delay of 500 milliseconds for the LED-blink

the actual value of millis() is 500 higher than one loop before.
This really can work together as long as you use the command delay()

The loop must run down as fast as possible. if you wat to keep the coded logic for the neopixel.
So this means the LED-blink must make use of millis() too. That's the reason why you have to learn how to use non-blocking timers based on the function mi.llis().

best regards Stefan

Use the "BlinkWithoutDelay" example to handle the LEDs and the NeoPixels will then be able to share the processor time with the LEDs.

PerryBebbington:
You have 2 lots of blocking code, first the LED flashes, taking half a second, during which the NeoPixels do nothing, then the reverse, the NeoPixels do their thing and the LEDs do nothing.

I get the feeling you have not learnt the basics of C/C++, if so please do the tutorials. I am certain you don't understand how to write non-blocking code and how to use millis properly. These 2 tutorials will help:
Using millis for timing
Demonstration for several things at the same time

Enjoy :slight_smile:

To say I have haven't learned the basics is an understatement. I barely know anything. I will review the resources you provided. Thank you.

ikevinax:
To say I have haven't learned the basics is an understatement. I barely know anything. I will review the resources you provided. Thank you.

It's important that you use the examples in the IDE to learn the basics before trying to understand the tutorials I gave links to. Without understanding the basics first the tutorials probably won't make much sense.

I hope you enjoy this hobby.