[SOLVED] Brightness value with two trailing zeroes results in no LEDs lighting

Hi, I’m using a range to increase/decrease the brightness of my LEDs on the Photon with steps of 5. I’m controlling this by calling a function on the Photon:

value is a string sent from the web-app.

uint8_t val8 = strtol(value, NULL, 16);

uint8_t changeBrightness(uint8_t level) {
    brightValue = level;
    return brightValue;

In my loop() I have FastLED.setBrightness(constrain(brightValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS));

When I pass “100” or “200” to value then the LEDs turn off. I’m not confident in C++ so I have no clue why this is happening. Is there anything obviously wrong with my conversion from string to uint8_t?

Also slightly related: I publish the brightness Particle.variable("brightness", brightValue); but the return value is 134635069 and not 255 as I want. How can I return a uint8_t value as a string with the correct value?

It’s not clear why you would use strtol, which returns a long, when you’re typing the result as uint8_t. Have you tried using atoi instead to see if that works?

I believe the problem is the last parameter to strtol, 16. It means value is represented in base 16, hexadecimal. You probably want 10 for a decimal number.

As a hexadecimal number, 100, or 0x100, is a decimal value of 256, which is too large to fit in a uint8_t.


atoi() fixed the issue with the LEDs not lighting with two trailing zeroes. I think I used strol because I was also doing some conversion of a uint32_t, and it just seemed to work. I’m really new to C++.

May I also ask how you’d return the uint8_t as a value between 0-255 in the publish? I’m really confused as to why a value I know is 255 in uint8_t would return 134635069 when used in Particle.variable.

As we can’t see your exact code, I’ll have a guess what’s causing that:

Since you are dealing with a 32bit controller every variable is placed on a 4-byte boundary, so when you have a variable declared as uint8_t you will have the one byte set as you expect it, but the three remaining “padding bytes” will be uninitialized.
And since Particle.variable() doesn’t have an overload that takes a uint8_t but only a int also the uninitialized bytes will be taken as value to expose.
In order to have Particle.variable() expose the correct value, you should declare your brightValue as int - you won’t save any memory space by declaring it as uint8_t anyway.

Another theory would be, that your exposed brightValue isn’t a global variable - which it has to be in order to work with Particle.variable().

Seeing your code would help tho’

Thanks for the great explanation, after changing the variable to an int it worked perfectly! As I’m new to coding on the Photon/Arduino I’m trying to mimic others and I see that values that fall in the range of 0-255 is usually uint8_t. Which makes sense since uint8_t can’t be negative and can have a maximum value of 255. But if there is no negative memory implications of storing the value as int then that’s way easier to work with.

Thanks again for the detailed explanation and quick reply.

1 Like

I haven’t tried, but explicit type-casting might also have helped

uint8_t brightValue;

void setup() {
  Paeticle.variable("brightness", (int)brightValue);