Question about Hue and uint8_t wrapping.

Question about Hue and uint8_t wrapping. I don’t seem to be getting what I want, but I don’t know why. Basically, I do the following. Pass in a CHSV hue value, and then an upper and lower limit. The idea is that the hue should move back and forth randomly over time from low to high. However, this code just pegs it at the high value and it never changes if I start with 0 and set low -20 and the high +20. I can’t for the life of me see why. If I set the low value to be 0, equal to CHSV Red, it works and varies between 0 and the high value. All variables are uint8_t. Any thoughts on how to improve this?

Some code left out of the gist for brevity. See my github link if you want to see the whole thing.

The ‘u’ in uint8_t stands for unsigned, which means it can’t be negative. The ‘8’ means 8-bit, meaning it can only hold values from 0 to 255. So I think ‘uint8_t i = -1’ actually sets it to 255.

Yeah, that’s the point. I would expect the wrap behavior to be fine if the low value is 235 (CHSV::Red - 20). The logic would still hold. But it doesn’t work that way. I can’t figure out why.

Also, this is on a Gemma, so I don’t really have a way to debug values very easily. This weekend I’ll stick it on a real Arduino or similar to see if I can debug values.

Tried using int for m_hueDirection? This code:

m_candles[i].h += m_hueDirection;

with a uint8_t value of “-1” is actually:

m_candles[i].h += 255;

That doesn’t seem like what you want…

Ahh, that is it. Thank you. Dumb mistake. I appreciate the help.

OK, that didn’t do it. Same problem. I can do 0 and above, but not 235 to 0 for the low end. I never see it get bluer is my point. It just pegs to the the orangish end (0 + 20) and stays there.