Math (pow) function problem

Greetings; haven't posted in forever.

I'm trying to do something more complex than it needs to be - 8 bit pwm on an 8 bit 595 shift register. I can shift quite well. Shifting I can do.

Trouble is to calculate 'when' during the numerous updates each individual led should be lit.

Problem arises as such:
I feed the pow function 2 variables, an int and 2
as in, value = pow(2,i);
it returns:
0 = 1
1 = 2
2 = 3
3 = 7
4 = 15...etc
Everything after '2^2' returns its correct value minus one.
Am I crazy? Should I be not feeding it directly 2,i, rather 2 variables? The reference states it will take any float value.

Ultra confusion: Serial.print(value); prints 1.00, 2.00, 3.00, etc
serial.print(value,DEC); prints 1 2 4 8 16...the correct numbers.
value is declared a double.

If you're happy with shifts, try just using bit masks to test your values.
i.e.
val & (1 << x)
It'll be a lot simpler than fiddling with floating point.

even simpler, if you want integers raised to powers of two you can use the arduino bit() function: http://www.arduino.cc/en/Reference/Bit

bit(0) is equal to 1;
bit(1) is equal to 2;
bit(2) is equql to 4;
?
bit(7) is equal to 128

you can use the arduino bit() function

Wow! They think of everything! :smiley:

Thanks for the great quick replies:

My setup (can't copy paste as I'm using separate laptop from work computer to program during night shift) :wink:

pseudocode:

array[8] of leds, intensity from 0-255

for loop 1-255
shiftoutvalue = 0
for led 0-7
if(led[led] > loop, then shitoutvalue = shiftoutvalue + 2^loop
next led

shiftout(shiftoutvalue)
//(so if led 1 needs turning on, its 2^0 added, or 1)
//2^7 = 128, bit 7, yay.

next loop

Basically, count to 255, if the led's value is bright enough, show it during 'this' update

Trouble is the damn pow function is crapping the bed on me.

edit>> my pow function is to SET a bit, not to find out what a bit is.

my pow function is to SET a bit, not to find out what a bit is

val |= bit(1) to set bit 1 in "val"

val &= ~bit(1) to reset bit 1 in "val"

val ^= bit(1) to toggle bit 1 in "val"

Thanks again - so thats like bitset(), bitclear() in short form?

Thanks again - so thats like bitset(), bitclear() in short form?

Probably.

(I'm sorry - I'm old-school, program across a variety of platforms, and prefer to program explicitly, rather than depend on locally-defined abstractions. I must get around to reading the reference pages sometime ;D

AWOL, that looks like bitSet and bitClear in long form :wink:

bitSet(val, n); // sets the bit in position n in the variable val

bitSet( val,1); // to set bit 1 in "val" (bit 0 is the first bit)

AWOL, that looks like bitSet and bitClear in long form

Yeah, I know, but I can type that stuff faster than I can reach for the platform reference manual, check that I've got the case right, and the operands in the right order...
;D

I admit I do the same in my code. But people new to programming have the advantage of not carrying that baggage with them :wink:

Thanks AGAIN everyone for awesome info...

I'm gonna try banging those bits around tonight. Seems that bitwise operand would run faster than a math pow function also :smiley:

Lastly: Any thoughts why the pow function is regurgitating goofy numbers?

Yay! Dubble post! (you need it when you're as noob as me on this forum!)

Good news, it works! Problem at 8 bit x 8 leds is its only about 10-20 hz updates, so it has obvious flicker. Gonna reduce to 4 bit and clobber it! :smiley:

Thanks again!