Code optimization Neopixel led blink paterns

Hello guys,
I am using adafruit Neopixel library to blink led, i want to create different color patterns (blinks) to indicate statuses … I did that below …and it work … but I think there are more efficient ways to do the same.
will be great if any of you can give me hint how I can achieve that in more efficient way .
below is a piece of code of how I am doing that now.

#include "Particle.h"
#include "neopixel.h"
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 1   //  for now just one led (I may require  use 2 led is necessary)
#define PIXEL_TYPE WS2811
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int red;
int green;
int blue;
int white;
int clear_led;
void setup()
 {
  strip.begin();                         // Initialize strip
	  strip.show();        // (R,  G,  B)    // Update all LEDs (= turn OFF, since none of them have been set yet!)
	  red          = strip.Color(255, 0, 0);    // Red
	  green      = strip.Color(0, 255, 0);    // Green
	  blue        = strip.Color(0, 0, 255);    // Blue
	  white       = strip.Color(243, 63, 193); // White  was (0,255,100) 
	  clear_led = strip.Color(0, 0, 0);      // clear
   led_test();  // test leds
}

void led_test(){
   strip.setPixelColor(0, clear_led);  //clear led 0
    strip.show();
  strip.setPixelColor(0, red); strip.show();
   delay(80);
  strip.setPixelColor(0, clear_led); strip.show();
    delay(30);
  strip.setPixelColor(0, green); strip.show();
   delay(80);
  strip.setPixelColor(0, clear_led); strip.show();
   delay(30);
  strip.setPixelColor(0, blue); strip.show();
   delay(80);
  strip.setPixelColor(0, clear_led); strip.show();
   delay(30);
  strip.setPixelColor(0, white); strip.show();
   delay(80);
  strip.setPixelColor(0, clear_led); strip.show();



Put your colors in an array and just manipulate the index into the array over time (e.g. with a Software Timer or some millis() logic).

1 Like

thanks @ScruffR
I did like this… but I don’t know how to insert a delay using millis…

int color_patern1 [9];
void setup() {
	delay(2000);
	  strip.begin();                         // Initialize strip
	  strip.show();        // (R,  G,  B)    // Update all LEDs (= turn OFF, since none of them have been set yet!)
	  red       = strip.Color(255, 0, 0);    // Red
	  green     = strip.Color(0, 255, 0);    // Green
	  blue      = strip.Color(0, 0, 255);    // Blue
	  white     = strip.Color(243, 63, 193); // White  was (0,255,100) \\\violeta
	  clear_led = strip.Color(0, 0, 0);      // clear
		//patern1
		color_patern1 [0]=clear_led;
		color_patern1 [1]=red;
		color_patern1 [2]=clear_led;
		color_patern1 [3]=green;
		color_patern1 [4]=clear_led;
		color_patern1 [5]=blue;
		color_patern1 [6]=clear_led;
		color_patern1 [7]=white;
		color_patern1 [8]=clear_led;

   //led_test();
	 led_patern1 () ;
}
void led_patern1 () {
	uint8_t m;

for (m = 0; m < 9; m++) {
	//unsigned long currentMillis = millis();
  //if (currentMillis - previousMillis2 >= 200) {
	strip.setPixelColor(0, color_patern1 [m]);
	strip.show();
	//previousMillis2 = currentMillis;
	delay(200);
  //}
	//previousMillis2 = currentMillis;
 }
}

const uint32_t onTime = 80;
const uint32_t offTIme = 30;

const int color_off = strip.Color(0, 0, 0);
const int color_pattern[] = { strip.Color(255,   0,   0) // red
                            , strip.Color(  0, 255,   0) // green
                            , strip.Color(  0,   0, 255) // blue
                            , strip.Color(243,  63, 193) // white
                            };
const int countPattern = sizeof(color_pattern) / sizeof(color_pattern[0]);

void setup() {
  strip.begin();                                 // Initialize strip
  strip.setPixelColor(0, color_off);             // switch off
  strip.show();                                  // Update all LEDs 
}

void loop() {
  led_patter();
}

void led_pattern() {
  static uint32_t _ms = millis();                // to keep track of the timing
  static bool     _on = false;                   // to keep track of on or off state 
  static int      _color = 0;                    // color to display 

  
  if (millis() - _ms < (_on ? onTime : offTime)) // not time to switch
    return;  

  // now it's time, so lets go
  _ms = millis;                                 // restart timing

  if (_on = !_on) {                             // toggle state and check current state
    strip.setPixelColor(0, color_pattern[_color++]);
    if(_color >= countPattern)                  // if required wrap round
      _color = 0;                 
  }
  else {
    strip.setPixelColor(0, color_off);
  }
  strip.show();
}
1 Like

thanks @ScruffR for your time and dedication helping rookies like me.

1 Like