Address single pixel in an array

ok so as per a suggestion, new thread as things have changed severely since starting last one.

i will post the code below to where i've got it so far but i have one, hopefully, simple question that i cannot find the answer to on google or within the example sketches.

how do i address a single pixel within an array that is suitable for the code setup and structure below?

to address some key points, the reason for multiple arrays on multiple pins with only 2 pixels per array/pin is because the project this is being installed into is a model of a pokemon called chandalure. it has 2 main arms, from which 2 smaller arms protrude. the 2 main arms come from the sides of its "head", a pumpkin shape. the model has already been wired up, assembled and painted with only 3 wires in each of the 4 smaller arms running down the 2 larger arms and into the head. 5v, gnd and signal there isnt enough room within the arms for 4 wires per smaller arm. hopefully that makes sense.

the code isnt the cleanest or most efficient but i can clearly see whats going on and make any changes to it at any stage without overloading my mind as my knowledge is limited and this is the simplest path for my knowledge level. i appreciate there are better ways to do this but i'd like to stick to what i've already got going on. I just need to learn how to address single pixels within an array and im hoping this will teach me that with your assistance.

heres the code:

//chandalure v5

#include <FastLED.h>

#define NUM_STRIPS 5
#define NUM_LEDS_PER_STRIP 2
#define brightness

CRGB leds[NUM_STRIPS] [NUM_LEDS_PER_STRIP];

// pin assignments
 
// states for the state machine
typedef enum
  {
  initialState,
  wantClearLEDS,
  wantVioletStrip1,
  wantBlueStrip1,
  wantVioletStrip2,
  wantBlueStrip2,
  wantVioletStrip3,
  wantBlueStrip3,
  wantVioletStrip4,
  wantBlueStrip4,
  wantVioletStrip5,
  wantBlueStrip5,
  wantDisplay,
  wantReset,
    
  } states;

// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;

void setup ()
  {
  FastLED.addLeds<NEOPIXEL, 3>(leds[0], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 5>(leds[1], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 6>(leds[2], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 9>(leds[3], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 10>(leds[4], NUM_LEDS_PER_STRIP);
  }  // end of setup
        
void doStateChange ()
  {
  lastStateChange = millis ();   
  timeInThisState = 200;

  switch (state)
   {
    case initialState:
         state = wantClearLEDS;
         break;

    case wantClearLEDS:
         leds[0] = CRGB::Black;
         leds[1] = CRGB::Black;
         leds[2] = CRGB::Black;
         leds[3] = CRGB::Black;
         leds[4] = CRGB::Black;
         FastLED.show();
         timeInThisState = 20;
         break;
         
    case wantVioletStrip1:
         FastLED.setBrightness(random(120)+135);
         leds [0][0], CRGB::Indigo;
         state = wantBlueStrip1;
         break;
         
    case wantBlueStrip1:
         FastLED.setBrightness(random(120)+135);
         leds[0][1], CRGB::Blue;
         state = wantVioletStrip2;
         break;
         
    case wantVioletStrip2:
         FastLED.setBrightness(random(120)+135);
         leds[1][0], CRGB::Indigo;
         state = wantBlueStrip2;
         break;
          
    case wantBlueStrip2:
         FastLED.setBrightness(random(120)+135);
         leds[1][1], CRGB::Blue;
         state = wantVioletStrip3;
         break;
         
    case wantVioletStrip3:
         FastLED.setBrightness(random(120)+135);
         leds[2][0], CRGB::Indigo;
         state = wantBlueStrip3;
         break;
         
    case wantBlueStrip3:
         FastLED.setBrightness(random(120)+135);
         leds[2][1], CRGB::Blue;
         state = wantVioletStrip4;
         
    case wantVioletStrip4:
         FastLED.setBrightness(random(120)+135);
         leds[3][0], CRGB::Indigo;
         state = wantBlueStrip4;
         break;
         
    case wantBlueStrip4:
         FastLED.setBrightness(random(120)+135);
         leds[3][1], CRGB::Blue;
         state = wantVioletStrip5;
         break;
         
   case wantVioletStrip5:
        FastLED.setBrightness(random(120)+135);
        leds[4][0], CRGB::Indigo;
        state = wantBlueStrip5;
        break;
        
   case wantBlueStrip5:
        FastLED.setBrightness(random(120)+135);
        leds[4][1], CRGB::Blue;
        state = wantReset;
        break;
        
   case wantDisplay:
    FastLED.show();
    state = wantReset;
    timeInThisState = (random(200));
    break;

     case wantReset:
     leds[0] = CRGB::Black;
         leds[1] = CRGB::Black;
         leds[2] = CRGB::Black;
         leds[3] = CRGB::Black;
         leds[4] = CRGB::Black;
     FastLED.show();   
        state = wantVioletStrip1;
        timeInThisState = 20;
        break;
        
    }  // end of switch on state
  }  // end of doStateChange


void loop ()
  {
   if (millis () - lastStateChange >= timeInThisState)
     doStateChange ();

  // other stuff here like testing switches
  }  // end of loop

Can you tell me what those code lines were intended to achieve? Have you tested it?

CRGB leds[NUM_STRIPS] [NUM_LEDS_PER_STRIP];

This is a 2 dimensional array. The first index is the strip number and the second the LED number in that strip

To address the second LED (ie number 1) in the third strip (ie number 2) you would use

leds[2][1]
1 Like

i forgot to add the show command line afterwards, sorry, let me add the command and update OP with correct line added.

helibob you answered that so simply. your a diamond.

As has been pointed out, your code is somewhat odd, but my explanation of how to address a single LED in a particular strip stands

yeah, its not the most efficient but like i say, it does allow me to make adjustments, see whats going on clearly step by step and if theres any problems when the nano arrives and its tested i can go through line by line and know exactly what each line is doing and when.

for some reason i never manage to get serial print to work so coding this way seems to help.

i will get to work adding in the missing lines of code defining pixel colors.

Ok, fine, but what about my question?

I asked the question because I suspect those code lines do not do what you think they do. If you had tested them, you would be asking why those code lines are not doing what you expected them to do.

sorry, i did go back and add the show command line.

can you elaborate on what you mean by not doing what i think they will do, if im not mistaken, and i likely am, they should set the arrays to black.

is the [#] not the array assignment?

but comma is not an assignment

You are mistaken. They don't do that.

You wrote

leds[0], CRGB::Black;

To set the LEDs to black, you need to write

leds[0] = CRGB::Black;

it's only only character that is different. But the meaning is completely different.

That's programming.

You may well ask why programming is not more like "natural" language.

Consider the following natural language:

"Time flies like an arrow"
"Fruit flies like a banana"

riiiight i get you, its just in another example, i've forgotten which one as i looked through many, it used a comma instead of =. hence the confusion.

i will again, repost my code with the modification

OP updated. both blocks.

Do not update previously posted code, ALWAYS post edited code as a new message.

1 Like

???

sorry got temporary ban. weak.

anyway, i uploaded the code to an uno and it powers up but nothing lights up. its all wired as per the code, but it just doesnt light up.

What code would that be and have you taken into account the replies previously made to this topic ?

//chandalure v5

#include <FastLED.h>

#define NUM_STRIPS 5
#define NUM_LEDS_PER_STRIP 2
#define brightness

CRGB leds[NUM_STRIPS] [NUM_LEDS_PER_STRIP];

// pin assignments
 
// states for the state machine
typedef enum
  {
  initialState,
  wantClearLEDS,
  wantVioletStrip1,
  wantBlueStrip1,
  wantVioletStrip2,
  wantBlueStrip2,
  wantVioletStrip3,
  wantBlueStrip3,
  wantVioletStrip4,
  wantBlueStrip4,
  wantVioletStrip5,
  wantBlueStrip5,
  wantDisplay,
  wantReset,
    
  } states;

// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;

void setup ()
  {
  FastLED.addLeds<NEOPIXEL, 3>(leds[0], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 5>(leds[1], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 6>(leds[2], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 9>(leds[3], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 10>(leds[4], NUM_LEDS_PER_STRIP);
  }  // end of setup
        
void doStateChange ()
  {
  lastStateChange = millis ();   
  timeInThisState = 200;

  switch (state)
   {
    case initialState:
         state = wantClearLEDS;
         break;

    case wantClearLEDS:
         leds[0] [0, 1] = CRGB::Black;
         leds[1] [0, 1] = CRGB::Black;
         leds[2] [0, 1] = CRGB::Black;
         leds[3] [0, 1] = CRGB::Black;
         leds[4] [0, 1] = CRGB::Black;
         FastLED.show();
         timeInThisState = 20;
         break;
         
    case wantVioletStrip1:
         FastLED.setBrightness(random(120)+135);
         leds [0][0]= CRGB::Indigo;
         state = wantBlueStrip1;
         break;
         
    case wantBlueStrip1:
         FastLED.setBrightness(random(90)+115);
         leds[0][1]= CRGB::Blue;
         state = wantVioletStrip2;
         break;
         
    case wantVioletStrip2:
         FastLED.setBrightness(random(120)+135);
         leds[1][0]= CRGB::Indigo;
         state = wantBlueStrip2;
         break;
          
    case wantBlueStrip2:
         FastLED.setBrightness(random(90)+115);
         leds[1][1]= CRGB::Blue;
         state = wantVioletStrip3;
         break;
         
    case wantVioletStrip3:
         FastLED.setBrightness(random(120)+135);
         leds[2][0]= CRGB::Indigo;
         state = wantBlueStrip3;
         break;
         
    case wantBlueStrip3:
         FastLED.setBrightness(random(90)+115);
         leds[2][1]= CRGB::Blue;
         state = wantVioletStrip4;
         
    case wantVioletStrip4:
         FastLED.setBrightness(random(120)+135);
         leds[3][0]= CRGB::Indigo;
         state = wantBlueStrip4;
         break;
         
    case wantBlueStrip4:
         FastLED.setBrightness(random(90)+115);
         leds[3][1]= CRGB::Blue;
         state = wantVioletStrip5;
         break;
         
   case wantVioletStrip5:
        FastLED.setBrightness(random(120)+135);
        leds[4][0]= CRGB::Indigo;
        state = wantBlueStrip5;
        break;
        
   case wantBlueStrip5:
        FastLED.setBrightness(random(90)+115);
        leds[4][1]= CRGB::Blue;
        state = wantReset;
        break;
        
   case wantDisplay:
    FastLED.show();
    state = wantReset;
    timeInThisState = (random(200));
    break;

     case wantReset:
         leds[0] [0, 1] = CRGB::Black;
         leds[1] [0, 1] = CRGB::Black;
         leds[2] [0, 1] = CRGB::Black;
         leds[3] [0, 1] = CRGB::Black;
         leds[4] [0, 1] = CRGB::Black;
     FastLED.show();   
        state = wantVioletStrip1;
        timeInThisState = 20;
        break;
        
    }  // end of switch on state
  }  // end of doStateChange


void loop ()
  {
   if (millis () - lastStateChange >= timeInThisState)
     doStateChange ();

  // other stuff here like testing switches
  }  // end of loop

the , was replaced for =

How does the code ever move on from the wantClearLEDS state after all of the LEDs have been set to black ?

im such an idiot. i'll make the change and report back