Arduino loop update of a variable is incorrect -


i've got following code snippet in setup() function:

... unsigned int a0val; unsigned int a0total = 0; ... (i = 0; < 1000; i++) {     a0val = analogread(a0);     serial.println(a0val);     a0total += a0val; } serial.println(a0total); ... 

this done baseline analog value @ startup account different types of sensors being used. 1 type may read 0 , may read non-zero value. point have starting point reference averaging 1000 readings @ startup time. 1000 overkill, i'll cut later.

now, 1000 readings somewhere between 128 , 130, expect a0total around 129,000. however, total consistently comes out less half number, 63,722 in 1 example. it's not half, it's less that.

another example: add first 500 readings when around 350-352, , total came out 43614. looks wrap-around, i'm using unsigned int both values can't happening.

so me looks "a0total += a0val" not updating every loop, doesn't make sense either.

what missing?

thanks, ron

you missing size of unsigned int on platform. 16bits , therefore maximum value 65535.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -