Global to local variables

Can't figure out why moving "X" from global to local causes the LEDs to dim and turn off for a few cycles. Then when I try to troubleshoot it using the serial console just by adding "Serial.begin(115200);" to the setup loop the problem goes away? Then when I add "Serial.println(X);" to my loop to troubleshot, the whole program seems to crash?

My goal is to not have any serial code and make "X" a local variable. Feel like I'm missing something very basic here.

#include <FastLED.h>

#define LEDs 144
#define Pi 3.14159
CRGB leds[LEDs];
//float X; //<-- moving "X" from global to loop

void setup() {
  FastLED.addLeds<NEOPIXEL, 6>(leds, LEDs);
  //Serial.begin(115200); //<------ If I un-notate this problem goes away?
  FastLED.setMaxPowerInVoltsAndMilliamps(5,1000);}

void loop() {
  byte F;
  float X; //<-- moving it here cause my problem.
  for (byte G = 4; G <= 4; G++) {
    for (float I = 1; I < LEDs; I = I + .20) {
      FastLED.clear();
      for (X = 1; X <= G; X++) {
         //Serial.println(X); // <-- If I un-notate this it crashes with in a few seconds?
        for (byte K = 0; K <= 2; K++) {
          byte P = (LEDs / 2) + (LEDs / 2) * (sin(2 * (Pi / LEDs) * I + (K * .666 * Pi)) * X / G);
          byte D = 255 / 3 * K + F;
          leds[P] = CHSV(D, 255, 255);
      } }
    F++;
    FastLED.show(); } } }

You forgot to initialise X in loop().

Did you mean it to be static?

Edit; oops, no, there it is in the for loop.
But F isn't initialised either.

Have you bounds-checked P?

isn't X initialized in

did you do Serial.begin(9600) to initialize the Serial bit rate?

i ran the code w/o the LED stuff but with Serial.begin().

A variable declared global will have value of 0 (zero) by default. A variable declared locally will have whatever value was in memory (not necessarily 0). Where is F initialized?

It's not 1986. You can use multiple character variable names, that can include lowercase characters and some more...

2 Likes

Uncommenting the Serial.begin and the Serial.println, program runs forever here.

Prints 1.00, 2.00, 3.00, 4.00 over and over.

More: you use P when it is equal to 144, that's gonna cause just about anything you don't want to happen.

@TheMemberFormerlyKnownAsAWOL "Have you bounds-checked P?"

called it, I confirm it running the code here.

I find the mixture of floating point and integer maths to be very hard to read, almost like you shouldn't do that. :wink:

a7

You are correct P shouldn't ever be 144, cause 0 is a place holder and the most it should ever be is 143. the array is "CRGB leds[144];" This would cause my unforeseen side effects

Included Minus point five

          byte P = (LEDs / 2) + (LEDs / 2 - .5) * (sin(2 * (Pi / LEDs) * I + (K * .666 * Pi)) * X / G);

I do understand how it is hard to read but wanted the equation on one line without scrolling. if there is a tutorial on how to make it prettier, I'm all for it.

Thank you.

Which makes the calculation hard to read… do you get a prize for getting it all in one long expression?

Also, I wonder if you could think your way to not needing floating point and trigonometry at all. I use smaller slower processors at times, so using floating point is not something I do much.

Lookup tables might be a way to go; use a “real” computer to do the cackulations or most of the hard parts anyway once and for all.

a7

Wish I got a prize for getting it on one line. The way my screw up brain works; single line, single character variables, I can process that a lot easier. I know really nothing about programming of course, trying to understand.

When I started playing around with Arduino and addressable LEDs. Took knowledge I already had, trigonometry. spit balled it together. When working with look up tables and start using 12+ sine waves, think my little brain would collapse. When I look at the output of the LEDs I see the sine wave

When I look at other people's code that is over 100+. I get dizzy. But I don't want to form bad habits either.

The link below, when I took the output to a text files it was over 5MB That would be one heck of a lookup table for an Arduino

Thanks again

what exactly are you trying to calculate?

you only have 144 LEDs but with all the nested loops, you must be updating each led 10x in each iteration

edit: not 10, 115 times

1 Like

Or 14-15 times a second.

Nice!

Is that like or even close to the result of your code posted above? There is much to be said for algorithmic animation as you demonstrate here.

Nevertheless, the code could be easier to write, debug, maintain and modify if the loops were cast as integers;

for (float I = 1; I < LEDs; I = I + .20) {

after all is some fixed number of iterations.

I think also you are benefitting from the smart compiler we enjoy, at least I hope so. I further hope that the way you make your art is convenient and flexible for you as much as it may be hard to read and understand to me.

I never argue with success. Faced with recreating anything like your video I would undoubtedly take a radically different approach and almost as certainly fail, so.

I wlll, however, recommend that you regularly put some bounds checking into your code. It costs almost nothing and may serve to inform you that although the result is what you desire, what is actually being displayed (or screwing things up…) is not obvious.

a7

No, the video I posted is the longest loop, more to show that a look up table is not really doable. The code I posted is of as a video "Phan" on the same channel of course.

Have like 20 different effects and would like to make around 30 more and put them on one Arduino.

Do think putting some bounds in my code would help me. I am some what fascinated with pure-ish math. even though my education only goes to high school level.

Thanks for y'all's help

seems odd to use a function returning a float to determine an array index.

since P = LED/2 + (LED/2) * sin(x), P should only ever be LED, exceeding the array size at Pi/2, which can be checked for

   P = LED == P ? LED-1 : P;
1 Like

You never initialized 'F' before using it.

This line is equivalent to:

  byte G = 4;
  {

doesn't the compiler initialize all variables to zero?

No. Only global and static variables are initialized to zero. Local variables are just assigned space on the stack and that space is not initialized.

Didn't I initialized it at?

byte F;

Yes I know " for (byte G = 4; G <= 4; G++) " is the same as " byte G = 4; "

I know my code is dumb looking but there is a reason for this line. The output to the LEDs is three sine wave that have 4 pixels per- sine wave. When I first programmed this I started with one pixel per sine wave and went to 6 or 8 and settle at 4 but still wanted to keep that options to go back

Here is a video called "Phan" it reminds me of a Fan that has three blades and looking from the side.

No.
That would be

byte F = 0;
1 Like

Oh good to know, in this case I don't care what "F" starts at, just add one to it and rolls over at 255.