B4R Question AnalogRead ac voltage

Discussion in 'B4R Questions' started by takhmin77, Jun 26, 2018.

  1. takhmin77

    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 :
    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You cannot change AnalogRead speed.
    Code:
    temp_read_avg=temp_read_avg/5
    Should be:
    Code:
    temp_read_avg=temp_read_avg/10
    Why do add a delay in your code? Read as fast as possible:
    Code:
    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)
     
    MarkusR and BillMeyer like this.
  3. takhmin77

    takhmin77 Member

    thanks , but AnalogRead have not any delay for reading voltage ?
     
  4. tigrot

    tigrot Well-Known Member Licensed User

    Yes it takes a sampling time, which is different in each device. A condenser is charged from dc source and is discharged by a resistor. The time to discharge is directly proportional to voltage. A normal micro is capable of tens of thousands samplings per seconds.
     
    Peter Simpson, Erel and BillMeyer like this.
  5. takhmin77

    takhmin77 Member

    thanks you so much indeed
     
    tigrot likes this.
  6. tigrot

    tigrot Well-Known Member Licensed User

    For "normal micro" I meant "general purpouse":)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice