How to alter a #define value

Hi,
I'm trying to set the number of pixels by using a 4 way dil switch to indicate the number of LEDs.
I am using both Neopixel and fastLed libraries and both use #define to set the number of pixels, but I can't find a way of adjusting the nPixels and numLeds value.
I understand that #define is a preprocessor directive and I have tried various methods of putting the #define into an 'if' statement, but it still doesn't alter it.
I also tried to do away with the #define and use a variable. I tried setting it as an int, uint16_t, uint32_t, byte and various other iterations, but then the line which reads CRGB leds[numLeds] throws up a compilation error. I've tried utilising the #ifdef statement without fully understanding how it works, but at least it doesn't give me a syntax error.

Sorry, not sure how to put the code into a scrollable box on here!!

//*********************************************Set Pixel Number************************************

#define nPixels 100
#define numLeds 100
#define nPixelsHalf (nPixels/2)
#define ledHalf  numLeds/2

#ifdef n_Pixels
if (digitalRead(dilSwitch41) == LOW)#define nPixels = 100; #define numLeds = 100;
if (digitalRead(dilSwitch42) == LOW)#define nPixels = 99; #define numLeds = 99;
if (digitalRead(dilSwitch41) == LOW)#define nPixels = 94; #define numLeds = 94;
if (digitalRead(dilSwitch43) == LOW)#define nPixels = 70; #define numLeds = 70;

#endif

//*************************************************************************************************

#define dcOffset  0  // DC offset in mic signal
#define noise     10 // noise/hum/interference in mic signal
#define samples   60  // Length of buffer for dynamic level adjustment
#define top (nPixels + 2) // Allow dot to go slightly off scale
#define waitTime 2
#define waits 20

#define COLOR_ORDER GRB
#define CHIPSET     WS2812
#define BRIGHTNESS  255
#define FRAMES_PER_SECOND 60

CRGB leds[numLeds];

I'm hoping someone will be able to help and steer me in the right direction. Please go easy on me as I'm at the beginning of learning this coding stuff and have written a few programs which work ok.
Apologies to the forum if I've posted in the wrong area, but again I'm not sure how to use the messaging system fully yet.

If you need to adjust the value on runtime, you can't use a #define, you need a variable instead.

Please show your code variant where you using a variable.

It simple.
In the IDE click on Edit, then Copy for Forum, that will copy your code for the forum. Then come back here and do a simple paste.

Any reason for that? A possible solution is that you can set it to 100 as you do (without the #if stuff) and later only loop over e.g. the first 70 if dilSwitch43 equals low.

I have done it for you for this time.

For the future do yourself a favour and please read How to get the best out of this forum and post accordingly (including code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).


To your question don’t statically instantiate the strip object - do that dynamically in setup after checking your configuration

Post your complete code, not a snippet!!!

the #define statements are like placeholders in the code - they are a fixed alias that makes it easier to read and change one line vs all the lines that might reference it.

If you simply need to change the value to something else (ie you aren't setting it in code by user input), just edit the value to what you want it to be.

BUT if you want the ability to define this in running code you want to use a variable rather than a #define. You will need to know what type of variable and it's size when you create it. You might be able to get a hint what type of variable you need by looking at the functions calling it.

See this link to learn about using variables:
Arduino IDE: Variables - STEMpedia Education

1 Like

Why would you use both?

How you do that depends on the library. With FastLED you can statically define the array size to the maximum number of LEDs possible and then tell the library how many LEDs to actually use via the addLeds() function.

It's trickier using NeoPixel library as it both allocates storage and sets the number of LEDs at the same time via the constructor. So, you'd have to use that constructor to create the Adafruit_NeoPixel object dynamically.

This was the idea; maybe not explained good enough.

// How many leds in your strip?
#define NUM_LEDS 100
// desired number of LEDs; smaller than or equal to 100
uint8_t numLeds = 100;

#define DATA_PIN 3
#define CLOCK_PIN 13

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

// dipswitches
uint8_t dilSwitch43 = 43;

void setup()
{
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  if (digitalRead(dilSwitch43) == LOW) numLeds = 70;
}

void loop()
{
  static uint8_t currentLed;
  // Turn the LED on, then pause
  leds[currentLed] = CRGB::Red;
  FastLED.show();
  delay(500);
  // Now turn the LED off, then pause
  leds[currentLed] = CRGB::Black;
  FastLED.show();
  delay(500);
  // next LED
  currentLed++;
  if (currentLed == numLeds)
  {
    currentLed = 0;
  }
}

No.

#include <FastLED.h>

// How many leds in your strip?
#define MAX_LEDS 100
// desired number of LEDs; smaller than or equal to 100
size_t numLeds {MAX_LEDS};

#define DATA_PIN 3
#define CLOCK_PIN 13

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

// dipswitches
constexpr uint8_t dilSwitch43 {43};

void setup() {
  if (digitalRead(dilSwitch43) == LOW) numLeds = 70;
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, numLeds);  // GRB ordering is typical
}

void loop() {
  static uint8_t currentLed;
  // Turn the LED on, then pause
  leds[currentLed] = CRGB::Red;
  FastLED.show();
  delay(500);
  // Now turn the LED off, then pause
  leds[currentLed] = CRGB::Black;
  FastLED.show();
  delay(500);
  // next LED
  currentLed++;
  if (currentLed == numLeds)
  {
    currentLed = 0;
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.