Coding Neopixel Library

Hi there,

I’m trying to build a wordclock so basically I need to write some functions like WordTwo = light pixel number 123.

Anyways I don’t understand why my code is not working…

I tried to open/close my function void thanks to {} and tried to remove it but in both cases I receive an error message (in red below in the extract)

Here is my code:

#include <Adafruit_NeoPixel.h>

#define RGBLEDPIN 6
#define N_LEDS 114 // 11 x 10 grid + 4 aux coins

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(N_LEDS, RGBLEDPIN, NEO_GRB + NEO_KHZ800);

uint32_t WHITE = pixels.Color(255, 255, 255);

////

void setup(void) {
Serial.begin(115200);

pixels.begin(); // Activation des neopixels
}

void loop() {

void showPLUSTROIS()
pixels.setPixelColor(101, WHITE);

Error : expected initializer before ‘pixels’

pixels.setPixelColor(101, WHITE);

^

void showPLUSQUATRE() {
pixels.setPixelColor(113, WHITE);
}

Error: a function-definition is not allowed here before ‘{’ token

void showPLUSQUATRE(){

^

}

I have this message too at the end :

exit status 1
a function-definition is not allowed here before ‘{’ token

So I don’t get what’s wrong…

Please help me !

Hello,

When writing code it is preferred to use code tags “</>” But the problems seem to be that you were using uint32_t, instead of int and your loop braces were encompassing your voids.

I made a couple changes to get it to work on my 12 pixel, Neopixel. When you change the number of LEDs back to your purpose, remember that 11X10 = 110 :slight_smile:

#include <Adafruit_NeoPixel.h>

#define RGBLEDPIN    22
#define N_LEDS 12 // An 11 X 10 grid would be 110

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(N_LEDS, RGBLEDPIN, NEO_GRB + NEO_KHZ800);

int RED = pixels.Color(255, 0, 0);    // ints work better for me
int GREEN = pixels.Color(0, 255, 0);
int BLUE = pixels.Color(0, 0, 255);


void setup(void)
{
  pixels.begin(); // Activation des neopixels
}

void loop()
{
  showPLUSTROIS();      // Took the liberty to make them blink back and forth
  delay(250);
  clearPLUSTROIS();
  delay(250);
  showPLUSQUATRE();
  delay(250);
  clearPLUSQUATRE();
  delay(250);
}

void showPLUSTROIS()
{
  pixels.setPixelColor(3, RED);
  pixels.show();                // Have to show them...turn them on
}

void clearPLUSTROIS()
{
  pixels.setPixelColor(3, 0, 0, 0); // 0,0,0 turns them off
  pixels.show();
}

void showPLUSQUATRE()
{
  pixels.setPixelColor(4, GREEN);
  pixels.show();
}

void clearPLUSQUATRE()
{
  pixels.setPixelColor(4, 0, 0, 0);
  pixels.show();
}

~Dennis

Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]

Where did you put the library?

.

The problem is that you are trying to define a function inside another function. The void key word defines the type of variable returned by a function and is the start of defining a function. You are trying to do this with your void showPLUSTROIS function which is inside your loop function.

All functions must be defined outside existing functions.

Sorry I did not know the code tags.

To LarryD, I put the library in the libraries folder in C/Arduino.

All functions must be defined outside existing functions.

It seems you are right because I move it to the beginning and it seems working. Thank you

To digidenny, thanks for the information regarding my code. I'll keep in mind the int aspect for the colors. To let you know, 114 is correct because it's a grid of 11x10 + 4 leds.

You wrote something interesting about the fact to switch off the led between two animations.

In my case I do not need to light them in the function because it's part of cases that will be linked to different minutes and hours so I'll write the function pixels.show at the end for all cases.

Anyways I'm wondering if I need to create a function switch all led off (or switch all leds except some).

So my question is : If I'm telling to the arduino light leds 1234 at minute 1 and after light leds 5678 at minute 2, Do the leds 1234 will still be switch on as 5678 ?

If yes I'll be very interested to have some help to create a function to switch off all led without leds 12456

Is this code correct ?

void ALLWAYSLIGHTON()
  {
    pixels.setPixelColor(1, WHITE);
    pixels.setPixelColor(2, WHITE);
    pixels.setPixelColor(4, WHITE);
    pixels.setPixelColor(5, WHITE);
    pixels.setPixelColor(6, WHITE);
}

void LIGHTOFF()
  {
    pixels.setPixelColor(n=all - ALLWAYSLIGHTON(), 0, 0, 0)
  }

I don't know how to define all led = a letter. I'm not sure if i can substract ALLWAYSLIGHTON directly.

Thanks for your help :)

Sorry I did not know the code tags.

Then RTFM
How to use this forum

It seems you are right

SEEMS? I am right.

If I’m telling to the arduino light leds 1234 at minute 1 and after light leds 5678 at minute 2, Do the leds 1234 will still be switch on as 5678 ?

The libiary works by setting the bits in a memory buffer. Unless you specifically wipe that buffer or change those LEDs then they will stay on as you add more calls to pixels.setPixelColor for different LEDs.

Is this code correct ?

No.

pixels.setPixelColor(n=all - ALLWAYSLIGHTON(), 0, 0, 0)

is a nonsense.

To turn off all LEDs use this:-

void wipe(){
    for(int i=0;i<NUMPIXELS;i++){
       pixels.setPixelColor(i, pixels.Color(0,0,0)); 
       }
}

Thank you very much Mike for your time and usefull solution.

I understand more and more the logic of C++ thanks to you.

I'll try now to continue by getting the time in my arduino and to use it to light the leds.

See you !

Louison:
To digidenny, thanks for the information regarding my code.

No problem, I’m still learning myself.

Louison:
I’ll keep in mind the int aspect for the colors.

I may have given some bad advice on that one Keep what you had for the colors.

Louison:
To let you know, 114 is correct because it’s a grid of 11x10 + 4 leds.

So that’s what 4 aux coins meant, srry. :slight_smile:

Louison:
You wrote something interesting about the fact to switch off the led between two animations.

In my case I do not need to light them in the function because it’s part of cases that will be linked to different minutes and hours so I’ll write the function pixels.show at the end for all cases.

Anyways I’m wondering if I need to create a function switch all led off (or switch all leds except some).

So my question is : If I’m telling to the arduino light leds 1234 at minute 1 and after light leds 5678 at minute 2, Do the leds 1234 will still be switch on as 5678 ?

If yes I’ll be very interested to have some help to create a function to switch off all led without leds 12456

Is this code correct ?

I don’t know how to define all led = a letter.
I’m not sure if i can substract ALLWAYSLIGHTON directly.

Thanks for your help :slight_smile:

If its the first six you want to keep on just change the “i=0” in the code Grumpy Mike gave you to 6 and it will just wipe the rest of the pixels.

void wipe() {
  for (uint16_t i = 6; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    pixels.show();
  }
}

~Dennis

That example would be much better if you passed into it the point where you wanted the wipe to occur:-

void wipe(int start) {
  for (int i = start; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    pixels.show();
  }
}

Or even make it more general and pass into it the range of LEDs you want to wipe:-

void wipe(int start, int end) {
  for (int i = start; i < end; i++) {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    pixels.show();
  }
}

Oooo, Denny Likey the last one. It works like a dream! I am going to use it, in the future! So if he wanted to wipe everything but the first 6 pixels he would just call wipe(6,144); Sweet!

~Dennis

Thanks a lot that's very interesting !