Coding help required - trying to blink pixels

I have a 9 led neopixel strip of which i need the first 4 pixels to display one color and the last 4 pixels to display another color, i get these colors from an array called colorsOnButtons. For this purpose I have created a function called showColors.

Question 1. - if there is a simpler way to show different colors on the strips for parts of the strip than what i have used

Question 2. - after setting the colors for first 4 pixels and last 4 pixels they need to blink 3 times before they are always showing , I also cannot use a delay function here because I will be running other functions while this part of the code executes. please help with this piece with an example of how todo it.

In the below code i was able to create the function and show colors on the strip parts and thats the end of it.

Thanks for your time

void showColors(int x,int y) {

  // show the colors for the strip and selecting the colors from the array defined 
  for(int i = 0; i < 4; i++) {
     leds_buttons[i] = colorsOnButtons[y,0];
  }
  FastLED.show();

   for(int i = 5 ; i < 9; i++) {
     leds_buttons[i] = colorsOnButtons[y,1];
  }
  FastLED.show();
  
  }

Please post the whole of your current sketch

You could use a for loop from 0 to 8 and if the LED being addressed is less than 4 set it to one colour, equal to 4 then set it to off else set it to the second colour. No need for an array of colours but it might still be useful for other effects that you have not mentioned

As to avoiding delay(), see Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

I think there's a gap in your for loops.

It's counting 0, 1, 2, 3 then 5, 6 and 7.

It:

for(int i = 5 ; i < 8; i++)

Should be:

for(int i = 4 ; i < 8; i++)

From the original description there are 9 LEDs so 4, gap, 4 would be the correct sequence but the second loop stops one LED short at the moment

in my code i have used 2 for loops to set the colors for the strip and you mentioned that we can use one for loop and an if else loop for the same result , will it make much of a difference in the 2 methods by way of performance?

As for the 2nd question i forgot to mention one small detail the blinking should happen in this sequence before always turned on
ledstrip part1 - ON , ledstrip part2 - OFF -- 500 ms
ledstrip part1 - OFF , ledstrip part2 - ON -- 500 ms
ledstrip part1 - ON , ledstrip part2 - OFF -- 500 ms
ledstrip part1 - OFF , ledstrip part2 - ON -- 500 ms
ledstrip part1 - ON , ledstrip part2 - OFF -- 500 ms
ledstrip part1 - OFF , ledstrip part2 - ON -- 500 ms
ledstrip part1 - ON , ledstrip part2 - ON -- indefinitely

Now the tricy part for me is if am setting the colors to the pixels from the for loops how do i control this ON and OFF for the blinking effect, request you to edit my code and explain to me.

Different view point. : )

For me the 9th is not handled.

The full code could help us.

No

When the time comes to turn off all of the LEDs use a for loop to do it. You could even use a single for loop to control the LEDs and a boolean variable to control whether they are turned on or off

Set the state of the boolean using the BlinlkWithoutDelay technique. You have read the links I suggested, or have you ?

for(int i = 4 ; i < 8; i++) this was wrong in my code , corrected it to for(int i = 4 ; i < 9; i++)

The code in your first post still wrong.

  for(int i = 5 ; i < 9; i++)

Please show us the full code.

The below is the full code for this function , why is it wrong , i have delibrately missed out on the 5th pixel because it should not light up at all. so the first loop turns on the pixel 1-4 and the second loop turns on pixel 6-9

void showColors(int x,int y) {

  // show the colors for the strip and selecting the colors from the array defined 
  for(int i = 0; i < 4; i++) {
     leds_buttons[i] = colorsOnButtons[y,0];
  }
  FastLED.show();

   for(int i = 5 ; i < 9; i++) {
     leds_buttons[i] = colorsOnButtons[y,1];
  }
  FastLED.show();
  
  }

going through the link you have shared gives me ideas to implement this , thank you so much

This is not the full code. Where's the setup, loop and other functions?

Not sure why you are asking as I only needed help with the code in this function showColors but here it is

#include <FastLED.h>

// How many leds in your strip?

#define BUTTON_LED     9
#define STATUS_LED     7
#define NUMOFLEDS_BUTTON    3
#define NUMOFLEDS_STRIP    9
#define BRIGHTNESS  50
//#define LED_TYPE    WS2811
//#define COLOR_ORDER GRB
CRGB leds_buttons[NUMOFLEDS_BUTTON];
CRGB leds_status[NUMOFLEDS_STRIP];

//unsigned long previousTime = millis();
//long combinationBlinkInterval = 500;

int gameSpeed = 1;
int gameLevel = 1;
//int buttonColors[];


// Define the array of leds
//CRGB leds[NUM_LEDS];

CRGB colorsOnButtons[3][3] = {
  {{ 255,  0,  0}, {0, 255, 0}, {0, 0, 255}},
  {{0, 255, 0},{0, 0, 255}, { 255,  0,  0}},
  {{0, 0, 255}, { 255,  0,  0},{0, 255, 0}}
};

CRGB colorsOnStrip[3][2] = {
  {{ 255,  0,  0}, {0, 255, 0}},
  {{0, 255, 0},{0, 0, 255}},
  {{0, 0, 255}, { 255,  0,  0}}
  
};

void setup() { 
    // Uncomment/edit one of the following lines for your leds arrangement.
    delay( 500 );
   
    FastLED.addLeds<NEOPIXEL, BUTTON_LED>(leds_buttons, NUMOFLEDS_BUTTON);  // GRB ordering is assumed
    FastLED.addLeds<NEOPIXEL, STATUS_LED>(leds_status, NUMOFLEDS_STRIP); 
    FastLED.setBrightness(  BRIGHTNESS );
    //start();
    //cycling through the led's on the strip and buttons on start of the game
    for( int i = 0; i < NUMOFLEDS_STRIP; i++) {
        leds_status[i] = ColorFromPalette( RainbowColors_p, 1, BRIGHTNESS, LINEARBLEND);     
    }
    for( int i = 0; i < NUMOFLEDS_BUTTON; i++) {
        leds_buttons[i] = ColorFromPalette( RainbowColors_p, 1, BRIGHTNESS, LINEARBLEND);       
    }
    FastLED.show();
    delay(3000);
    fill_solid(leds_buttons, NUMOFLEDS_BUTTON, CRGB::Black);
    fill_solid(leds_status, NUMOFLEDS_STRIP, CRGB::Black);
    FastLED.show();
}

void loop() { 
  int gameSpeed = 0;
int gameLevel = 0;

   unsigned long currentTime = millis();
   showColors(gameSpeed,gameLevel);
  }

void showColors(int x,int y) {
  //to show colors on the buttons and selecting the colors for the buttons from the array defined 
  for(int i = 0; i < 3; i++) {
  leds_buttons[i] = colorsOnButtons[y,i];
  }
  FastLED.show();

  //to show the colors for the strip and selecting the colors from the array defined 
  for(int i = 0; i < 4; i++) {
     leds_buttons[i] = colorsOnButtons[y,0];
  }
  FastLED.show();

   for(int i = 5 ; i < 8; i++) {
     leds_buttons[i] = colorsOnButtons[y,1];
  }
  FastLED.show();
  
  }
  

void start() {
  

  // play start music
  tone(10,300,400);
  delay(400);
  tone(10,400,400);
  delay(400);
  tone(10,600,400);
  delay(400);
  tone(10,800,400);
  delay(400);
  tone(10,900,400); 
  for( int i = 0; i < NUMOFLEDS_STRIP; i++) {
        leds_status[i] = ColorFromPalette( RainbowColors_p, 1, BRIGHTNESS, LINEARBLEND);
        
    }
    for( int i = 0; i < NUMOFLEDS_BUTTON; i++) {
        leds_buttons[i] = ColorFromPalette( RainbowColors_p, 1, BRIGHTNESS, LINEARBLEND);
        
    }
   
    
    FastLED.show();
     delay(3000);
}

To verify the context where this function is being used.

For example:

showColors(int x,int y)

Here you are passing two values but just Y is used.

Also gameSpeed and gameLevel are defined as global and local. It's correct?

you are right , my code is still in development and there are few issues with it.

I think you have to learn some basics concepts first as flags and millis.

I did an example that can be used as reference in your project.

The LED represents any instruction that do want apply to your LED strip.

#include <Arduino.h>

const int ledPin1 = 16;
const int ledPin2 = 17;

int step = 0;
unsigned long previousMillis = 0;
int count;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop()
{
  if (step == 0)
  {
    Serial.print("Step  = ");
    Serial.println(step);
    digitalWrite(ledPin1, HIGH);
    step = 1;
    previousMillis = millis();
  }
  else if (step == 1)
  {
    if ((millis() - previousMillis) > 1000)
    {
      Serial.print("Step  = ");
      Serial.println(step);
      digitalWrite(ledPin1, LOW);
      step = 2;
      count++;
      previousMillis = millis();

      if (count == 3)
      {
        count = 0;
        step = 3;
      }
    }
  }
  else if (step == 2)
  {
    if ((millis() - previousMillis) > 1000)
    {
      Serial.print("Step  = ");
      Serial.println(step);
      digitalWrite(ledPin1, HIGH);
      step = 1;
      previousMillis = millis();
    }
  }
  else if (step == 3)
  {
    Serial.println("The sequence has finished.");
    digitalWrite(ledPin2, HIGH);
    delay(500);
    digitalWrite(ledPin2, LOW);
    step = 0;
  }
}

Appreciate the help, will definitely learn from your example

Hi Fernando , I am using a Arduino Pro mini for this project and I have 3 push buttons mapped to various pins on the board. What I need to know is if I can set an interrupt on any digital pin or is it selective.
Also can I have a single interrupt defined to monitor the 3 push buttons to know which was pressed

Why do you need to use an interrupt in the first place ?

ok ill just give an idea of what is happening , I have 3 buttons and 3 leds showing different colors each button corresponding to a specific color , I need to know which button the user has pressed and then perform some code and if nothing is pressed do some other code.