Hello guys.I have been on this forum for some time now and i am learning the programming doing different sketches.I found some lines of code in the free code of the Multiwii copter that i am interested in and i do not understand what is going on there.

what i cant understand is after getting the value from the analogRead,what is the >>4 doing there? I know it should be shift right with 4 elements but where?inside the value?The other part of the code i get it all.Thank youDaniel

I am still having trouble understanding the equation.So bare with me please and correct me where i am wrong.Suppose i have the analogRead to 1023(5V to the input pin).First time i get vbatRaw=0so ((0 x 15)+(1023 x 16) >>4 that makes (16368)>>4 3so the shift what does here? it takes the point 4 times to the left? Is the result 1.6368 ?Thank you for patience and understanding.Daniel

ok i understood it is divided again by 16.But with every cycle the value of vbarRaw increase,even if i have constant analogRead of my analog pin.Is it suppose to be like this or i am still missing something?

I'm not sure why the current reading is multiplied by 16. It doesn't look like it should be. (15 times the previous value + the current value) divided by 16 would yield a new average. Looks like a flaw in that code.

well there still is something spookie in this formula.The real thing is that in testing it works.I have a 5V power supply connected to A0 pin.I defined the VBATSCALE 50 (as it say in 0.1V steps). i can see the analogRead function reading correctly the pin.I have initialized the led on pin 13 and VBATLEVEL1_3S to 45.With the potmeter if i turn down the supply to less than 4.5Volts on the analog pin the led start blinking.i tryed different voltage levels and it works perfectly.I just dont understand the math under that formula.Still must be something with that shift.

Here is the complete code i am using.Using Analog3 as input on Arduino Pro mini,16Mhz and 5V supply.Digital 8 as output to my led(the code is using a buzzer but i dont have one).The entire code is working,just i still not understand the math of how it retrieves the voltage in 0.1V steps.I perfectly understand the map function of the arduino but i dont get this.ANy help is appreciated.

vbat = vbatRaw / VBATSCALE; Is not going to work given that VBATSCALE = 50, and you have a 5V reference voltage and no potential divider on the input.However is one of those things is different we need to know so we can explain where it goes right.

At the moment suppose you had an input voltage of 2.5V then the reading (smoothed or not) would be 512. If you then do an integer division by 50 you get 10 not the 25 you say you are getting.

You're taking your current average and taking fifteen sixteenths of it, then adding in one sixteenth of your most recent reading.

That would make perfect sense. But actually, it looks like you're taking your current average and taking fifteen sixteenths of it, then adding in sixteen sixteenths of your most recent reading. The raw reading will continuously increase (until it overflows)- looks like a bug.

You are perfectly right Myke.The original code is using a resistor divider to get the voltage of a 12V battery reading.It divides so that at 12.6Volts it gets 5V at analog pin.I just took the part of the code so i could read 5V directly in input.However the code using the divider it works perfectly fine.The VBATSCALE is defined 126 and i use VBATLEVEL_1 to define the voltage i want to monitor,for example 90 if the voltage drop below 9Volt.But in my case i want to use the same code to measure a real 5V input,and trigger the led if it drops below 3V for example.I know i could already use the map function or something more simple,but i just wanted to understand how is this math used in this equation,especially the sift to the right.I hope i dont make anyone mad about this,but making all the math into paper get me always to the overflow.

It works like this:-The analogue to digital converter has a reference voltage which defines the full scale reading. By default this is the 5V supply of the arduino. If you put 5V into the analogue input you get a reading of 1023 "units" therefore each of these units corresponds to :-5 / 1024 = 4.88 mV (note 1024 because there are 1024 intervals between readings of 0 to 1023)So if you want to convert this number into a voltage in volts then multiply the reading by 0.00488 or divide by 204.8However that code uses integer division and that truncates the values.Supposed you want the units to be 0.1V then divide by 20.48, in practice this will be dividing by 20 and getting a value roughly in 0.1V units.