Incorrect Calculations

Hello,

I am using the Arduino MEGA 2560. I have two pots wired up, to the A0 and A1 pins. They are to control the brightness of two separate LED Strips of different types. One is a WS2812 strip, which I am using the FastLED Library to drive. They are working just fine. The other strip are just generic 5050 package LEDs. I am driving them through just a simple transistor. They are also working fine when they are being driven. Everything was working beautifully, the physical side of everything is still working perfectly. But all of a sudden I was getting unexpected behavior on the WS2812s. So I use serial to debug and I can see that analogRead on the pots are coming back as expected between 0-1023 and fully responsive, but to drive the LEDs I need a value between 0-255 so I am just using the binary operator >> 2 to scale it. My issue is that for one of the pots I am getting a value around 10000 after the binary shift instead of between 0-255. When putting this into the LEDs, this is what was causing the issues with WS2812. I tied the brightness to the other pot signal and it is working beautifully. Is this a common issue? I can't seem to change the value from the 10000 range, not even by defining it as zero right before the Serial Print line. I would post the code, but it is all in a LONG file, where I am doing many other things. I am just hoping someone has had this issue before and can help me debug.

Thanks for any help that can be provided,

Zack

Did you forget something ?
Like code, and line breaks?

1 Like

It would be easier to provide help if you had not posted un the Uncategorised category of the forum and if you had posted your code

Your topic has been moved to the Programming category of the forum. Please post your sketch in a new reply, using code tags when you do

I'm going to guess you are mixing & matching data types and somehow some bits are getting shifted-in from the left.

Normally you can shift (left or right) until there is nothing but zeros and if you keep shifting every bit is still zero.

Have you tried dividing by 4? The compiler might be smart enough to do a bit-shift (or whatever is the most efficient).

And "just for fun" you might want to open the Windows calculator in programmer mode and type-in the exact number that's near 10,000 to see what it is in binary.

Error in code.

Because you are likely saving the analog read in a SIGNED integer. Check your program.

Run a Neopixel RGB sketch on one. Might be WS2812, WS2811, SK?64? When the colors are as expected, you have the right chip.

Is your runaway pot upside down?

Try using map(A0, 0, 1023, 0, 255); on each pot.

I was thinking that for a moment too but a + sign is a zero and there shouldn't be any negative values.

My thoughts, also. But if his storage int is defined in a function with no beginning value, the beginning value could be anything. Still waiting for code!

Is A0 or A1 a 16-bit ADC? (2/3 of 65535) 40000 >> 2 = 10000.

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