Share My Creation Talking Frequency Counter

The Arduino generates an accurate 1 second time base for the counter by cascading timer0 and timer2. The link between digital inputs 3 and 4 connects the output of timer2, 250 Hz, to input of timer0. Timer1 is a 16 bits timer, it overflows at the count of 2 power of 16, that in turn advances over-flow register. At the end of the 1 second the 16 bit register is recorded. The Arduino sends via OTG cable or adaptor 4 bytes that contain the measured frequency. The specs recommend input up to 6 MHz, I tested it at 2 MHz.
Speach is in the default language of the device, speed is set to 0.75.
B4R:
Sub Process_Globals
    Private usbserial As felUsbSerial
    Private manager As UsbManager
    Private TTS1 As TTS
    Dim timer1 As Timer

End Sub

Sub Globals
    Private btnConnect As Button
    Private Label1 As Label
    Private analog1 As EditText  
    Private changeBar As SeekBar
    Private freq="1000" As String
    Private lblTime As Label
    Private sec As String
    Private speakEn=False, dis=True As Boolean
    Private btnTalk1 As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        manager.Initialize
    End If
    Activity.LoadLayout("main")  
    TTS1.Initialize("TTS1")
    TTS1.SpeechRate = 0.75
    timer1.Initialize("timer1", 10000)
    timer1.Enabled=False
    analog1.Text="1000"
End Sub

Private Sub serial_DataAvailable (Buffer() As Byte)        ' bytes received
    Dim fb1, fb2, fb3, kh, mh, hz As Int
    Dim df As Float
   
    If Buffer.Length > 3 Then
        If Buffer(0)=50 Then
            fb1=Bit.And(0xFF,Buffer(1))        'convert to unsigned bytes
            fb2=Bit.And(0xFF,Buffer(2))
            fb3=Bit.And(0xFF,Buffer(3))
            freq = fb1*65536 + fb2*256 + fb3
            df=freq
            analog1.Text=NumberFormat(df,0,0)
           
            If speakEn And dis Then
                'TTS1.Speak(freq & "hertz", True)
                mh=freq/1000000
                kh=(freq Mod 1000000) / 1000
                hz=freq Mod 1000
                If mh > 0  Then TTS1.Speak(mh & "meg", True)
                If kh > 0 Then TTS1.Speak(kh & "k", False)
                If hz > 0 Then TTS1.Speak(hz , False)
                If freq < 1000 Then TTS1.Speak("hertz" , False)
                speakEn=False
            End If

        End If
    End If
End Sub

Sub btnTalk1_CheckedChange(Checked As Boolean)
    If Checked Then
        changeBar.Enabled=True
        lblTime.Enabled=True
        talkTime
        dis=True
    Else
        dis=False
        changeBar.Enabled=False
        lblTime.Enabled=False
    End If
End Sub

Sub Timer1_Tick
    speakEn=True
End Sub

Sub changeBar_ValueChanged (Value As Int, UserChanged As Boolean)
    If UserChanged Then
        talkTime
    End If
End Sub

Sub talkTime
    sec = changeBar.Value + 5
    timer1.Interval = 1000 * sec
    lblTime.Text = "Talk every " & sec & " seconds"
End Sub

Sub btnConnect_Click
    If manager.GetDevices.Length = 0 Then
        Label1.Text = "USB device not found"
    Else
        Dim device As UsbDevice = manager.GetDevices(0) 'the device
        If manager.HasPermission(device) = False Then
            manager.RequestPermission(device)
        Else
            usbserial.Initialize("serial", device, -1)
            usbserial.BaudRate = 9600
            usbserial.DataBits = usbserial.DATA_BITS_8
            usbserial.StartReading
            Label1.Text = "USB Connected"
            timer1.Enabled=True
            talkTime
        End If
    End If
End Sub

Sub TTS1_Ready (Success As Boolean)
    If Success Then
        speakEn=True
    Else
        ToastMessageShow("Error initializing TTS engine.", "")
    End If
End Sub

Sub Activity_Resume
    If TTS1.IsInitialized = False Then
        TTS1.Initialize("TTS1")
        TTS1.SpeechRate = 0.75
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    TTS1.Release
End Sub

B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Private AStream As AsyncStreams

    Public out6 As Pin
    Public out9 As Pin
    Public out3 As Pin
    Public in4 As Pin
    Public in5 As Pin
    Public freq As UInt
    Public overf As Byte
End Sub

#if C
void setTimers(B4R::Object* o)
{
          //set timer0 in=250Hz out=1Hz
    OCR0A = 249;
    TCCR0A=0b1000011; 
    TCCR0B=0b1110;  //  PWM mode, input T0 pin D4
    
      // set timer2 in=16MHz out=250Hz
    OCR2A =249;
    OCR2B = 125; 
    TCCR2A=0b110011;  //output B in phase, fast PWM mode
    TCCR2B=0b1110; // set prescaler to 256 and start the timer
    
        //  set timer1
    OCR1A = 32767;   //32768 counts
    TCCR1A = 0b1000011; 
    TCCR1B = 0b11110; //input pin D5
}
#End if

#if C
void count(B4R::Object* o)
{   
    TIFR1 |= _BV(1);    //reset interrupt
    OCR1A = 65535;  //32767
    TCNT1=0;

}
#End if   

#if C
void count2(B4R::Object* o){
    
  if(TIFR1 & _BV(1)) {(b4r_main::_overf)=(b4r_main::_overf)+1; TIFR1 |= _BV(1);}
 }
#End if   

#if C
void count3(B4R::Object* o){
  
   (b4r_main::_freq) = TCNT1 ;
}
#End if   

Private Sub AppStart
    Serial1.Initialize(9600)
    AStream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
    
    RunNative ("setTimers",Null)
    
    out6.Initialize(6, out6.MODE_OUTPUT)
    out9.Initialize(9, out9.MODE_OUTPUT)
    out3.Initialize(3, out3.MODE_OUTPUT)
    in4.Initialize(4, in4.MODE_INPUT)
    in5.Initialize(5, in5.MODE_INPUT)

    counter
End Sub

Private Sub counter
    Private ps(4), freqH, freqL As Byte
    Private i=True As Boolean

    Do While i=True
    Do While out6.DigitalRead=True
    Loop
    Do While out6.DigitalRead=False        'wait for start of 1 sec
    Loop
    overf=0
    RunNative ("count",Null)
    
    Do While out6.DigitalRead=True
        RunNative ("count2",Null)
    Loop

    RunNative ("count3",Null)

    freqH = freq / 256
    freqL = freq
    ps(0)=50
    ps(1)=overf
    ps(2)=freqH
    ps(3)=freqL
    AStream.Write(ps)

    Loop
End Sub

Sub Astream_NewData (Buffer() As Byte)
    
End Sub

Sub AStream_Error
    'Log("error")
End Sub
 

Attachments

  • talking_counter405.png
    talking_counter405.png
    19.4 KB · Views: 1,850
  • talking_counter.gif
    talking_counter.gif
    2.4 KB · Views: 186
  • talking_counter.png
    talking_counter.png
    15.1 KB · Views: 190
  • talking_counter.zip
    6.5 KB · Views: 166
  • talking_counter_4br.zip
    1.5 KB · Views: 170
Top