Neopixel loop issue

Hi all

Got a small problem with 2 neopixel rings. So in the loop I want them to both run the sequence together. The problem I'm having is that one ring will do the sequence, pause, then the other will do the sequence and so on. Is the issue in calling the digital read? If the sensor reads high then the ring in question will respond but it'll take a run through of the sequence to register.

It works perfectly when it's one in its own which leads me to think the problem is in the digital read line, any help would be greatly appreciated. Code is attached.

//


#include "FastLED.h"                                                                                            // Include Neopixel library

#define SPOT_1      12                                                                                           // Defines pinouts for Neopixel rings
#define SPOT_2      9
#define SPOT_3      11

#define NUM_STRIPS 2
#define NUM_LEDS    16                                                                                          // Number of neopixels per ring.   
#define LED_TYPE    NEOPIXEL                                                                                    // Defines use of neopixel chipset

CRGB spot1[NUM_LEDS], spot2[NUM_LEDS];                                                                         // Creates neopixel arrays

int HE1 = 18;                                                                                                    // Sets Hall Effect Sensor pins
int HE2 = 21;


int HE1State = 0;                                                                                               // Sets initial sensor states to LOW
int HE2State = 0;





void setup() {
  delay(100);
  Serial.begin(9600);

  FastLED.addLeds<LED_TYPE, SPOT_1>(spot1, NUM_LEDS);                                                           // Links all Neopixel rings to FastLED library.
  FastLED.addLeds<LED_TYPE, SPOT_2>(spot2, NUM_LEDS);


  pinMode(HE1, INPUT);
  pinMode(HE2, INPUT);

}

void loop() {
  Serial.println(F("Reading Signals "));

  HE1State = digitalRead(HE1);              // Look at reading all of the Hall Effect sensor inputs during the loop.
  HE2State = digitalRead(HE2);

  Serial.println(F("Signals read "));



  if (HE1State == LOW) {                    //If sensor is LOW flash Red
    flash();
    Serial.println(F("HE1 Flash "));
  }


  if (HE1State == HIGH) {                    //If sensor is HIGH solid Blue
    solid();                        // Delay affects speed of circling light. Higher delay = slower speed.
    Serial.println(F("HE1 Solid "));
  }


  if (HE2State == LOW) {                    //If sensor is LOW flash Red
    flash2();
    Serial.println(F("HE2 Flash  "));
  }

  if (HE2State == HIGH) {                    //If sensor is HIGH solid Blue
    solid2();
    Serial.println(F("HE2 Solid "));
  }
}

void flash() {
  Serial.println(F("Sensor 1 Low "));

  for (int i = 0; i < NUM_LEDS; i++) {
    spot1[i] = CRGB::Red;
    FastLED.show();
    delay(30);                           // Delay affects speed of circling light. Higher delay = slower speed.

    for (int i = 0; i < NUM_LEDS; i++)
      spot1[i].fadeToBlackBy(50);
    FastLED.show();
  }
}

void solid() {

  Serial.println(F("Sensor 1 High "));
  fill_solid( spot1, NUM_LEDS, CRGB::Blue);
  FastLED.show();
  delay(1000);                           // Delay affects speed of circling light. Higher delay = slower speed.


}


void flash2() {
  Serial.println(F("Sensor 2 Low "));

  for (int i = 0; i < NUM_LEDS; i++) {
    spot2[i] = CRGB::Red;
    FastLED.show();
    delay(30);                           // Delay affects speed of circling light. Higher delay = slower speed.

    for (int i = 0; i < NUM_LEDS; i++)
      spot2[i].fadeToBlackBy(50);
    FastLED.show();
  }
}

void solid2() {

  Serial.println(F("Sensor 2 High "));
  fill_solid( spot2, NUM_LEDS, CRGB::Blue);
  FastLED.show();
  delay(1000);                           // Delay affects speed of circling light. Higher delay = slower speed.


}

Cheers

Delta_G is correct.

The problem I'm having is that one ring will do the sequence, pause, then the other will do the sequence and so on.

That is how you wrote your code. If the action you see was the action you wanted do you think the code would be different?

Now try and get into the mind set of getting two things to happen apparently at the same time. You can’t actually get two things at the same time but you can do things very quickly so that it looks like you are. But using delay will stop things happening quickly. Worst using delay in an for loop will make it a long time before the loop finishes.

So the soloution is to use a universal several things at once approach as advised. But if you can get your head round it merge the two animations. The latter approach might seem easer but it is a lot harder in practice if the anamation are not synchronised to each other.

Okay, i think i understand the concept. I just can't see where to structure the if statements for the button pushes. Do i need to take the conditions for low out and use that as the holding loop?

You need to reverse you way of thinking your code.

Now it's: check the sensors and run the animations accordingly

You need to think as : run the animations and check the sensors to control them