B4R Question AnalogRead ac voltage

takhmin77

Member
hi
i want read peak of ac 50 hz analog voltage .
i read pin voltage 500 times and get maximum readed vlotage

i think 50 hz means that in 20 ms i can read all of the voltage from 0 to maximum

i want to know how i can set analogread speed , that in 500 times reading pin voltage , read real maximum voltage ?

my code :
B4X:
    temp_read_max=0
    Dim  VTmp As Float
    Dim Voltage,amps As Float
    For avg = 1 To 10
        For y = 1 To 500
            VTmp = V_Pin_A0.AnalogRead
            If VTmp > temp_read_max Then
                temp_read_max = VTmp
            End If
        Next
        temp_read_avg=temp_read_avg+temp_read_max
        Delay(1)
    Next
    temp_read_avg=temp_read_avg/5
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot change AnalogRead speed.
B4X:
temp_read_avg=temp_read_avg/5
Should be:
B4X:
temp_read_avg=temp_read_avg/10

Why do add a delay in your code? Read as fast as possible:
B4X:
Dim p As Pin
p.Initialize(p.A0, p.MODE_INPUT)
Dim NumberOfTests As Int = 10
Dim MaxRead As UInt
Dim TotalRead As ULong = 0
Dim Interval As Int = 20 'ms
Dim StartTime As ULong
For i = 1 To NumberOfTests
   MaxRead = 0
   StartTime = Millis
   Do Until Millis > StartTime + Interval
       MaxRead = Max(MaxRead, p.AnalogRead)
   Loop
   Log(MaxRead)
   TotalRead = TotalRead + MaxRead
Next
Dim avg As Float = TotalRead / NumberOfTests
Log(avg)
 
Upvote 0
Top