[SOLVED] Can you see what I am doing wrong trying to address arrays?

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// Released under the GPLv3 license to match the rest of the
// Adafruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#define PIN 6 // connected pin on the Arduino
#define NUMPIXELS 16 // Number of NeoPixels
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char two[]   = {1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1};
char three[] = {1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1};
char choose=two;
void setup() 
{
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}
void loop() 
{
  pixels.clear(); // Set all pixel colors to 'off'
  // The first NeoPixel in a strand is #0, second is 1, all the way up
  // to the count of pixels minus one.
  for(int i=0; i<NUMPIXELS; i++) 
  { // For each pixel...
    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a moderately bright green color:
    //if (two[i] == 1)
    if (choose[i] == 1)
    {
      pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    }
    else
    {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    }
       }
    //pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    pixels.show();   // Send the updated pixel colors to the hardware.
    delay(1000); // Pause before next pass through loop
    if (choose == two)
    choose=three;
    else
    choose=two;
  //}
}

I am at the early stage of programming a neopixel to give me a choice of numbers (and colours) but I am stuck with the correct protocol.
If I make the first 'if' statement
if (choose[i] == 1)
trying to get it to choose between array two or three I get
'Compilation error: invalid types 'char[int]' for array subscript'
If I comment it out and uncomment out the line above
if (two[i] == 1)
so I just get array two
the compile works?
What do I need to use to make the sketch allow me to choose the array I want?

char choose=two;

choose is a char variable that can hold a single character

two is an array of chars, hence the error

If you declare choose as an array of chars then you could do this

memcpy(choose, two, sizeof(choose));

This will copy the two array to the choose array

There are other ways of making one array the same as another using pointers but try memcpy() first

What do you expect choose to be? That assignment makes no sense. Do you want it to be a copy of the array? A pointer to the array?

Choose should be:

char* choose=two;

Or maybe choice instead of choose...

This is about driving that 16-segment display from your last topic?

I would suggest you use a 2-D array, one index being the character/digit and the other dimension being the segment:

const char pattern[][16]   = {
//Segments:
// A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P
  ...
  {1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1}, // 2
  {1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1}, // 3
  ...
};

Personally, I would encode them more efficiently, like this:

const uint16_t pattern[] = {
//Segments:
//  ABCDEFGHIJKLMNOP
  ...
  0b1110000110000111, // 2
  0b1110000111000011, // 3
  ...
};

and use bitRead() to extract each bit as needed.

bitRead(pattern[d], s);

You need a pointer.

char *choose = two;

or

char *choose = &two[0];

Go no further until you understand both those changes.

Later, you can

  choose = three;  // or whatever

if you want.

a7

Also, this won't work as intended, ie green, since you declared

green, red, blue. By your code, you're setting red as a little more than half brightness.

EDIT: don't listen to me, really. I am incorrect about changing NEO_GRB to anything else for these versions of the Neopixel products. Leave it as is.

Probably a good thing if all 16 neopixels are being powered through the Arduino from USB power.

1 Like

Since you may want to do different things with the Neopixels later on and not have to rebuild the device, I'd just use a +5V supply.
tl/dr
Adafruit recommends external +5V supply in most use cases.
Each Neopixel can draw about 60mA, so let's say that 150 value on green alone is 15mA (I don't know, could be more but 3 leds inside to max of 60mA means about 20mA per led at 1/2-ish on green let's call it 15mA) * 16 (pixels in the ring) = 240mA
This is assuming you never use color mix or animate and if you don't, what's the point of Neopixels? That's where the fun is.


Adafruits explains:

Each individual NeoPixel draws up to 60 milliamps at maximum brightness white (red + green + blue). In actual use though, it’s rare for all pixels to be turned on that way. When mixing colors and displaying animations, the current draw will be much less. It’s impossible to estimate a single number for all circumstances, but we’ve been using 1/3 this (20 mA per pixel) as a gross rule of thumb with no ill effects. But if you know for a fact that you need every pixel on at maximum brightness, use the full 60 mA figure.

https://learn.adafruit.com/adafruit-neopixel-uberguide/powering-neopixels

No. The object defines the particular strip order RGB BGR GBR whatever, but colours specifications are always good old RGB.

When the colors are messed up (I wanted half bright green!) you only need to figure out which kind of strip to tell it that you are working with, the rest of the code does not change.

I usually put in the setup() to light up

pixel 1 red
pixel 2 green
pixel 3 blue

pixel N - 1 (last pixel on the strip) white

and show() that and delay for oh, say, 777 milliseconds.

Strip is working, colours are correct, strip installed right way around. Or not. :expressionless:

a7

1 Like
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

Well that's confusing. And here I've been fussing about that detail all this time...
(code snippet from Adafruit library example sketch 'buttoncycler')

I altered the line above setup to this and it worked, thank you build 1971.

Yes it is about the neo pixel.
Thanks Paul I will look into doing it this way.

If you read the other thread Paul you will see that the display is powered from my desk power supply.

can you be a bit more explanatory about this line? I can't seem to get it to compile no matter what I do?

I didn't get a notification that you had updated that topic.

The display looks cool. Reminds me of one I built myself!

You would use it like this:

for(int i=0; i<NUMPIXELS; i++) 
  { // For each pixel...
  if (bitRead(pattern[d], i) == 1)
    {
      pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    }
    else
    {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    }

Thanks for the prompt reply.
I am sorry Paul it wasn't the first post it was the last post explaining how I connected the display that I pointed out the display was connected to the bench supply.
I am entering your suggestion and will get back to you.

#include <Adafruit_NeoPixel.h>
#define PIN 6 // connected pin on the Arduino
#define NUMPIXELS 16 // Number of NeoPixels
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//char two[]   = {1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1};
//char three[] = {1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1};
//char four[]  = {0,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0};
//char* choose=two;
int a=1;
int s;
const char pattern[][16] =
{
  {1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1}, // 2
  {1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1}, // 3
};
void setup() 
{
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}
void loop() 
{
  pixels.clear(); // Set all pixel colors to 'off'
  // The first NeoPixel in a strand is #0, second is 1, all the way up
  // to the count of pixels minus one.
  for(int i=0; i<NUMPIXELS; i++) 
  { // For each pixel...
    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a moderately bright green color:
    //if (two[i] == 1)
    //if (choose[i] == 1)
    if (bitRead(pattern[a],i) == 1)
    //if (a==1)
    {
      pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    }
    else
    {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    }
    }
    //pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    pixels.show();   // Send the updated pixel colors to the hardware.
    delay(1000); // Pause before next pass through loop
    //if (choose == two)
   //choose=four;
    //else
    //choose=two;
  //}
}

For some reason it seems to fail for me?
I get ' compilation error: exit status 1
it looks as if it doesn't like the bitRead line but gives no other indication?

bitRead() should be used with the more efficient binary encoding of the patterns I suggested, not the 2-D array suggestion.

#include <Adafruit_NeoPixel.h>
#define PIN 6 // connected pin on the Arduino
#define NUMPIXELS 16 // Number of NeoPixels
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//char two[]   = {1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1};
//char three[] = {1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1};
//char four[]  = {0,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0};
//char* choose=two;
int a=1;
int s;
const char pattern[][16] =
{
  {1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1}, // 2
  {1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1}, // 3
};
void setup() 
{
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}
void loop() 
{
  pixels.clear(); // Set all pixel colors to 'off'
  // The first NeoPixel in a strand is #0, second is 1, all the way up
  // to the count of pixels minus one.
  for(int i=0; i<NUMPIXELS; i++) 
  { // For each pixel...
    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a moderately bright green color:
    //if (two[i] == 1)
    //if (choose[i] == 1)
    if (pattern[a][i] == 1)
    //if (a==1)
    {
      pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    }
    else
    {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    }
    }
    //pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    pixels.show();   // Send the updated pixel colors to the hardware.
    delay(1000); // Pause before next pass through loop
    //if (choose == two)
   //choose=four;
    //else
    //choose=two;
  //}
}
if (bitRead(pattern[a], i) == 1)

pattern is a 2 dimensional array. Did you deliberately only use 1 dimension to access one of its values ?

J:\My Drive\Arduino files\neopixel_display\neopixel_display.ino:36:9: note: in expansion of macro 'bitRead'
if (bitRead(pattern[a],i) == 1)