FastLED get value from other variable

Hi all.
can you help with my code.
If I replace the rightWall variable with

rightWall=pixelTrimmed;

the function doesn't work, but if I replace it with the number

rightWall=28 ;
//or 
rightWall=NUM_LEDS;

the function can work.
Can you help with this problem, because I want to change the pixelTrimmed value using potentio. Thank You.

#include <FastLED.h>
#include <EEPROM.h>
#define NUM_LEDS 32
#define BRIGHTNESS 125
#define LEDPIN  2
CRGB leds[NUM_LEDS];
int pixelTrimmed ;

void setup() {
  delay(1000);
  FastLED.addLeds<WS2812B, LEDPIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  pixelTrimmed = EEPROM.read(2); //28
}

void loop() {
  pattern1();
  LEDS.show();
}

int ball = 0;
int rightWall = pixelTrimmed;
int rightWallx = pixelTrimmed;
int leftWall = 0;
int leftWallx = 0;
int halfpixel = (pixelTrimmed / 2) ;
int ballDir = 1;

void pattern1() {

  uint8_t beatA = beatsin8(80, 5, 255);
  
  leds[ball] =   CHSV(beatA, 255, 255);
  leds[ball - 1] = CHSV(beatA, 255, 0);
  leds[ball + 1] =  CHSV(beatA, 255, 0);

  leds[rightWallx] =  CHSV(beatA, 255, 255);
  leds[leftWallx] =   CHSV(beatA, 255, 255);

  EVERY_N_MILLISECONDS(15) {

    ball += ballDir;
    if (ball == rightWall) {
      ballDir = -1;
      leftWall++;
      leftWallx = leftWall - 1;
      
      if (leftWall == halfpixel - 1) {
        leftWall = 0;
      }
      
    } else if (ball == leftWall) {
      ballDir = 1;
      rightWall--;
      rightWallx = rightWall + 1;
      
      if (rightWall == halfpixel + 1) {
        rightWall = pixelTrimmed;
      }
    }
  }
}

It looks like you’re initialising pixellTrimmed every time through loop()

Also, you’re probably going to be safer with EEPROM put & get
.read is only a single byte (nut a 2- or 4- byte integer.

The big error you are making is that you are assuming, I think, that the deceleration of global variables like the ones shown here:-

Are made after the setup function has run, they are not.

That is why declaration of global variables are normally placed before any function in the code. Global variables are always supplied an initial value of 0, and are integer types. So you need to store / retrieve them from two bytes and join them up. However, if you want the EEPROM library to do this for you then look at the other functions this library offers in the reference section of the Arduino documentation.

Writing this - are you assume that every time that the pixelTrimmed value has changed, the value of rightWall will automatically updated from pixelTrimmed ?
Only with this assumption you can expect, that your code will work correct.

Thank you for reply.

Int pixelTrimmer = EEPROM.read(2);
Void setup(){

}

I've taken pixelTrimmer out of setup. and you are right, the animation appears. but maybe the error is in the function logic. The value on the EEPROM is 28, but on the serial monitor, the end of the ball variable animation reaches 36.

I don't see what that has to do with anything.
Sure the value will change but as you never write anything back to the EEPROM the change will not register.

Have you seen the documentation I told you about?
Here is the link to it

That just sets it to the value at that one particular time. If pixelTrimmed changes after that it's not going to magically change rightWall.

Try making that assignment every loop.

In loop

rightWall = pixelTrimmed;

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