Help with 2d array for leds using arduino c++

Hi,
I’m having trouble with my first foray into 2D arrays for arduino c++

Basically, I am trying to create a 7 segment x 6 led digit using 42 ws2812b led strip and the fastLED library.
– strip is wired as per the classic a, b, c, d, e, f, g

I want to switch on the various groups of 7 leds to create a nominated digit (0-9).

it’s only about 100 lines all up… should I post all of it it here?
… or just the nested for loops I’ve got so far?
– or, not at all?

TIA,
Chris

Hi Christopher,

Please post all your code using the preformatted text option (</>)

What is the issue that you are having with the code you have written?

Hi Stephen,
Thanks for getting back to me.

Difficult to describe “issue”… I’ve jumped through so many hoops.
I go from a no leds lighting up to the 8th led speedily staying in one spot. At one stage I had serial monitor turned on and it was obvious I had permanent loop going on.
— At which point I initialise the strip to black and go hunting… got rid of that problem. I think.

Anyway here’s what I’ve got so so far… and no leds lighting up via the Nano.

oops … how do I use the </>
– OK, got it.

#include "FastLED.h"

#define DEBUG 0
/*
#ifdef DEBUG
Serial.flush(); 
Serial.print("digits[");
Serial.print(i);
Serial.print("] = ");
Serial.println("digits[i]");
#endif 
*/

#define DATA_PIN 9
#define NUM_LEDS_strip 42
#define NUM_LEDS_seg 6
#define NUM_segments 7

#define LED_TYPE WS2812B                                       // What kind of strip are you using (WS2801, WS2812B or APA102)?
#define COLOR_ORDER GRB                                     // It's GRB for WS2812B and BGR for APA102

struct CRGB leds[NUM_LEDS_strip];                                   // Initialize our LED array.

int cursor = 0;

/* not used here...
#define RED    0xFF0000
#define GREEN  0x00FF00
#define BLUE   0x0000FF
#define YELLOW 0xFFFF00
#define PINK   0xFF1088
#define ORANGE 0xE05800
#define WHITE  0xFFFFFF
#define OFF    0X000000
*/

int displayDigit = 6;

void cwmDigit ( const int [][ NUM_LEDS_seg ] ); // 7 segment led digits
const int cwmNUM = 10;
const int ledStatus = 7;
 int digits[ cwmNUM ][ ledStatus ] = {{0,1,1,1,1,1,1},  // Digit 0
                                     {0,1,0,0,0,0,1},   // Digit 1
                                     {1,1,1,0,1,1,0},   // Digit 2
                                     {1,1,1,0,0,1,1},   // Digit 3
                                     {1,1,0,1,0,0,1},   // Digit 4
                                     {1,0,1,1,0,1,1},   // Digit 5
                                     {1,0,1,1,1,1,1},   // Digit 6
                                     {0,1,1,0,0,0,1},   // Digit 7
                                     {1,1,1,1,1,1,1},   // Digit 8
                                     {1,1,1,1,0,1,1}};  // Digit 9 | 2D Array for numbers on 7 segments

void setup() {
 #ifdef DEBUG
Serial.begin(57600);
#endif
    
  LEDS.addLeds<WS2812B, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS_strip); // Set LED strip type
  FastLED.setBrightness(85); // Set initial brightness
   
}

void loop() {

    initLeds();
    
    int seg = 1;
    while (seg < NUM_segments) {        //  loop to go over the 7 groups of leds
        void  cwmDigit(uint8_t displayDigit);
    seg++;    
    }
  
    FastLED.show(); // Display leds array
     
}

void initLeds() {
   for ( int x = 0; x < NUM_LEDS_strip; ++x ) {
        leds[0] = CRGB::Black;  // initialise strip to black
        FastLED.show();
        break;
    }
 }

void cwmDigit( const int myDigit ) {
    
#ifdef DEBUG
// Serial.println("value of j = ");
// Serial.print(j);
#endif
    
    // loop through array's rows  to find myDigit
    for ( int i = 0; i < 10; ++i ) {
        if (i == myDigit){ 
          // loop through columns of current row
          cursor = 0;
          for ( int j = 0; j < 8; ++j ) {
             if (j == 1){  
               leds[i][j] = CRGB::Blue;    // if true set our current dot to blue 
         //      FastLED.show();                   
             } else {
                leds[i][j] = CRGB::Black;  // otherwise set our current dot to black
          //       FastLED.show();
             }
            cursor ++;
            } 
        }
    }  
}

Hi Christopher,

Have you run a strandtest or gridtest sketch to confirm all your LEDs are working as expected? I’m happy to help with code but I’ll need you to narrow it down to a more specific problem.

G’day Stephen,

Yep all LEDs confirmed.

I’m just having another crack at it…

The following is compiling OK. but not lighting up LED’s
– sorry, but quite a lot has changed since my previous post.

Just wondering why, given that it is compiling, leds may not be lighting up.

For example, do I have FastLED.show(); in the correct spot?

Thanks again

#include "FastLED.h"

#define DEBUG 0

#define DATA_PIN 9
#define NUM_LEDS_strip 42
#define NUM_LEDS_seg 6

#define LED_TYPE WS2812B                                       // What kind of strip are you using (WS2801, WS2812B or APA102)?
#define COLOR_ORDER GRB                                     // It's GRB for WS2812B and BGR for APA102

// #define LedSegmentMap[][]
// #define SegmentsActive[][]
// int LedSegmentMap[7][6];

struct CRGB leds[NUM_LEDS_strip];                                   // Initialize our LED array.

int value = 0;
int displayDigit = 6;
int ledFill = 0;

#define RED    0xFF0000
#define GREEN  0x00FF00
#define BLUE   0x0000FF
#define YELLOW 0xFFFF00
#define PINK   0xFF1088
#define ORANGE 0xE05800
#define WHITE  0xFFFFFF
#define OFF    0X000000

/* 
 ***  original
 ***  from: https://forum.pjrc.com/threads/24927-7-segement-with-rgb-led-strip
#include <OctoWS2811.h>

const int ledsPerStrip = 56;

DMAMEM int displayMemory[ledsPerStrip*6];
int drawingMemory[ledsPerStrip*6];

const int config = WS2811_GRB | WS2811_800kHz;

OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config);
*/
const int active = 10;
const int state = 7;
    int SegementsActive[active][state] =  {
                            {1,1,1,1,1,1,0}, //digit 0
                            {0,1,1,0,0,0,0}, //digit 1
                            {1,1,0,1,1,0,1}, //digit 2
                            {1,1,1,1,0,0,1}, //digit 3
                            {0,1,1,0,0,1,1}, //digit 4
                            {1,0,1,1,0,1,1}, //digit 5
                            {1,0,1,1,1,1,1}, //digit 6
                            {1,1,1,0,0,0,0}, //digit 7
                            {1,1,1,1,1,1,1}, //digit 8
                            {1,1,1,0,0,1,1}  //digit 9
                          };

const int seg = 7;
const int led = 6;                         
   int LedSegmentMap[seg][led] =  { 
                            {0,1,2,3,4,5,6},        
                            {7,8,9,10,11,12},       
                            {13,14,15,16,17,18},  
                            {19,20,21,22,23,24}, 
                            {25,26,27,28,29,30},
                            {31,32,33,34,35,36},
                            {37,38,39,40,41,42} 
                          };

void setup() {
#ifdef DEBUG
Serial.begin(57600);
#endif 
    
    
  LEDS.addLeds<WS2812B, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS_strip); // Set LED strip type
  FastLED.setBrightness(85); // Set initial brightness
   
}

void loop() {
 
    // initLeds();
    
    setLedStatus(displayDigit, BLUE, LedSegmentMap[seg][led]);
         
    FastLED.show(); 
  
}

void setLedStatus(int myDigit, int color) {
  int ledsPerSeg = 6;
  int ledFill =  SegmentsActive[active][state];

    for (int seg = 0; seg < 6; seg++) {
        for (int led = 0; led < ledsPerSeg; led++) {
         // orig for OctoWS2811 ::   leds.setPixel(LedSegmentMap[digit][seg][led],SegmentsActive[value][seg] ? color : OFF );
         //   fill_solid( LedSegmentMap[seg][led], ledFill, CRGB::Blue);
         
         // how to use fastled function to light the LED(s)
         
        }
    }
  // orig octoWS2811::  leds.show(); 

    
}

void initLeds() {
   for ( int x = 0; x < NUM_LEDS_strip; ++x ) {
        leds[0] = CRGB::Black;  // initialise strip to black
        FastLED.show();
        break;
    }
 }

Hello again Stephen,

sorry, I deleted that previous nonsense.

Hi Chris,

Shouldleds[0] be leds[i]?

I find your array usage very hard to follow. Not saying that there is anything wrong with it. I just don’t get it.

Hi Stephen,

Sorry about that.

I still don’t get the 2D array thing and how to use them to light my led strip.

The two (newly edited) arrays for LED layout in previous post were “borrowed”
from here:
https://forum.pjrc.com/threads/24927-7-segement-with-rgb-led-strip

But I think I only need the

const int seg = 7;
const int led = 6;                         
   int LedSegmentMap[seg][led] =  { 
                            {0,1,2,3,4,5,6},        
                            {7,8,9,10,11,12},       
                            {13,14,15,16,17,18},  
                            {19,20,21,22,23,24}, 
                            {25,26,27,28,29,30},
                            {31,32,33,34,35,36},
                            {37,38,39,40,41,42} 
                          };

so now… the function
void setLedStatus(int myDigit, int color) { …}
is the problem

Maybe I should switch to Neopixel library and try using their… strip.setPixelColor()

Thanks anyway.
Sorry for my rantings.

@Clinton can you take a look at this please?

I have added a few comments to this section of code that hopefully might point you in the right direction. Multi-dimensional arrays can be a little tricky at first but you will get the hang of them with a little practice.

const int seg = 7;
const int led = 7;      // your array below is 7x7 but the declaration was 6x7 this will cause oddness as it will cause the points to be mapped strangely               
   int LedSegmentMap[seg][led] =  { 
...
                         };

void loop() {
 
    // initLeds(); // this will not run as it is commented out
    
    setLedStatus(displayDigit, BLUE, LedSegmentMap[seg][led]); // decleration of this function only has two inputs this is called with three
         
    FastLED.show(); 
  
}

void setLedStatus(int myDigit, int color) {
  int ledsPerSeg = 6;
  int ledFill =  SegmentsActive[active][state];

    for (int seg = 0; seg < 6; seg++) {
        for (int led = 0; led < ledsPerSeg; led++) {
         //with the following lines commented out this loop just loops and does nothing 
         // orig for OctoWS2811 ::   leds.setPixel(LedSegmentMap[digit][seg][led],SegmentsActive[value][seg] ? color : OFF );
         //   fill_solid( LedSegmentMap[seg][led], ledFill, CRGB::Blue);
         
         // how to use fastled function to light the LED(s)
         
        }
    }
  // orig octoWS2811::  leds.show(); 

    
}
1 Like

Thanks Clinton,

initLeds() was meant to be commented out… :grin:

The second array was wrong… it is 7 segments of 6 leds each

const int seg = 7;
const int led = 6;                         
        int LedSegmentMap[seg][led] =  { 
                                {0,1,2,3,4,5},        
                                {6,7,8,9,10,11},
                                {12,13,14,15,16,17},  
                                {18,19,20,21,22,23}, 
                                {24,25,26,27,28,29},
                                {30,31,32,33,34,35},
                                {36,37,38,39,40,41} 
                              };

I’m kind of getting the actual array part as a matrix.

But my problem is in the nested loops where I need to relate the loop iteration to the LedSegmentMap [ ][ ] array
– in order to turn an individual led on

e.g. so when I call: testingLoops( displayDigit, BLUE );

void testingLoops( int myDigit, int color ) {
    // *** TESTING  
    // 1. select each segment from the full length
      for (int x = 0; x < NUM_LEDS_strip; x+7) {
            // if on a segment for my required digit (myDigit) ...
            if ( x = myDigit ) {
                // 2. loop through this segment of 6 LEDs
      //          delay(1000);
       
for (int myLed = 0; myLed < 7; myLed++) {

// *** I have no clue what to do here ***        
       if (SegementsActive[active][state] ==1 ) {
                        // switch this LED on
                       leds[myLed] = CRGB::Blue;    
                        //***  I know, not using the BLUE param !
                        //*** just trying to get some leds lit!                   
                    }  
//***
                 }
                
              }
  
      } 
   
    } //end testingLoops( int myDigit, color ) 

can SegementsActive[active][state]
look like SegementsActive[x][myLed]
or do I have to stick to actual array definition: [active] [state] – but then what?

I have been unable to find a clear description of how to use the array to light a led (or a segment).

Do you think I would be better off with the neopixel library instead of fastled ?

In the original I found… using octoWS2811
they simply had:

for (int seg = 0; seg < 7; seg++) {
                for (int led = 0; led < ledsPerSeg; led++) {
                        leds.setPixel(LedSegmentMap[seg][led], SegmentsActive[value] ? color : OFF);
                }
        }
        leds.show();

TIA, for any input Clinton,
Chris

BTW, my test strip looks like this:

The active and state are constant variable and are always equivalent to the value assigned to them higher in the code. They will also be pointing at a location in memory just after the the 2d array. You will need to change SegementsActive[active][state] to SegementsActive[x][myLed]

1 Like

AaaHaa,
Will give a go tomorrow.

Thanks Clinton.

1 Like