Oh .. went a second time through your code
and, 230/1.669 = 137.8 which can be a comp. factor.
You can't use a compensation factor like that, because the voltage drop on diodes in the rectifier remains constant, while the transformer output varies. You're not taking that 1.4v (or around that value) into consideration.
It's just a bit silly to use the 5v reference, because then your maximum adc value will probably be always under 4-500, so a 9 bit value out of 10 bits. You're wasting precision.. it's much better to use 2.048v or 4.096v because you get more precision, which matters if you actually want more accurate results.
In addition, when you have 2mV per bit or 4mV per bit, you can do some math tricks , like saying 2 / 1000 or 4 / 1000 instead of 0.002 or 0.004 and that may help you a lot if you need to do lots of measurements in a second because multiplying by two is basically shifting a bit to the left, multiplying by 4 is shifting 2 bits to the left and so on.
Floating point multiplications will waste quite a lot of cycles.
So if you have (v * 0.002 * 7.66 + 1.4) x 0.707 x 24.2 you could write it like this :
convert 1.4v to 0.18276 x 7.66 -> (v x 0.002 * 7.66 +0.18276 * 7.66) * 0.707 * 24.2
convert 0.002 to 2/1000 -> (v x 2/1000 * 7.66 + 0.18276 * 7.66) * 0.707 * 24.2
multiply that 1.4 with 1000 to get both to be /1000 - > (v * 2/1000 * 7.66 + 182.76 * 7.66/1000 ) * 0.707 * 24.2
now that both have 7.66/1000 you can add them - > (v * 2 * 7.66/1000 + 182.76 * 7.66/1000 ) * 0.707 * 24.2
=>[( v * 2 * 7.66 + 182.76*7.66) / 1000 ]* 0.707 * 24.2
=> now all those floating point numbers are constants so you can pre-multiply them => [(v*2 +1820 ) * 7.66 * 0.707 * 24.2 ] / 1000
=>(v * 2 + 182.76) * 131.05 / 1000
=>(v << 1 + 182.76 ) * 0.13105
so 400 =>
(400x2 +182.76) * 0.13105 = 128.79v
(400*2 + 183 ) * 0.13105 = 128.82v
and you can further round up 182.76 to 183 because it has minimal effect on the results.
If you use 4.096 v as reference, it's basically just shifting two bits to the left (v<<2), that's all that changes.
You reduced that whole thing to a shift to left and a multiplication, saving lots of cpu cycles.
If the voltage became 3v, what will be the output?
Your input pin accepts voltages between 0 and 5v and tolerates a bit more for short periods of time. The ADC converts a voltage into a binary value, based on a voltage reference. If you use 2.048 as voltage reference, then 1023 will be 2.048v.
If you put 3v or more than 2.048v, the adc will report 1023, the maximum it can write in 10 bits.
you mentioned 2.048v ref voltage will have about 15.7v because of the divider... this thing helped me. 15.7v is 10.11v rms at secondary windings which equals 244.66v rms at primary... Is that correct?
The voltage divider is basically (100k + 15 k ) / 15k .. so about 7.66. So anything before the voltage divider, is divided by 7.66 ... that's why 15.68 becomes 2.048 (15.68 / 7.66 = 2.048)...
Just the same 25v would become 25 / 7.66 = 3.26v and since the value is higher than the 2.048 voltage reference you set for the ADC, the adc will give you the value 1023. It's like seeing "overload" on digital multimeters, if you put the multimeter on a small range.
15.7v is 10.11v rms at secondary windings which equals 244.66v rms at primary... Is that correct?
I think you forget to add the voltage lost in the bridge rectifier. Don't forget that the voltage before the voltage divider is less than the voltage before the bridge rectifier, by 2 times the drop on a diode.
---[0]--> [primary] <----> [secondary ] -[1]---- > [bridge rectifier+capacitor] -[2]------> [voltage divider] ---[3]------->
If [3] is 2.048v, then [2] is 2.048 x 7.66 = 15.68v,
[1] is [2] + 1.4v , 15.68v + 1.4v = 17.08v which is the peak voltage.
Then the Vrms is 0.707 x [1] , 0.707 x 17.08 = 12.07v
So if your transformer ratio is 24.2, then Vrms at [ 0 ] is 24.2 x [1] = 24.2 x 12.07 = 292v