B4R Question ESP8266 Frequency

Discussion in 'B4R Questions' started by stari, Oct 20, 2019.

  1. stari

    stari Active Member Licensed User

    With the ESP8266 I want to make a frequency generator to generate frequencies from 50 to 500 Hz.
    However, the way I generate frequencies, these are very volatile. For example, at 50 Hz it varies from 49.2 to 50.8 Hz.
    Is there any other option than the one I use?

    Code:
    Sub Process_Globals
        
    Public Serial1 As Serial
        
    Private wifi As ESP8266WiFi  
      
        
    Private astream As AsyncStreams  
        
    Private  f_out As Pin
        
    Private led_out As Pin
      
        
    Dim bstring(100As String
      
        
    Dim pulse_h As Int = 2
        
    Dim pulse_l As Int = 900
        
    Dim led_h As Int                        : led_h = (pulse_l + pulse_h) / 2
      
        
    Dim stevec1 As Int                        :stevec1 = 0
      
        
    Dim pulse_timer As Timer
      
    End Sub
    Private Sub AppStart
        Serial1.Initialize(
    115200)      
        astream.Initialize(Serial1.Stream, 
    "astream_newdata"Null)  
      
        
    Log("Connected: ", wifi.Connect2("usr""pswd")) 'change as needed
      
        
    Log("AppStart Now")  
        f_out.Initialize(
    16, f_out.MODE_OUTPUT)        'GPIO16 - pulse
        led_out.Initialize(5,led_out.MODE_OUTPUT)    'GPIO05 - LED
      
        pulse_timer.Initialize(
    "pulse_timer_Tick",0.01)
        pulse_timer.Enabled = 
    True
      
        TimeNIST.Start  
      
        AddLooper(
    "Looper1")  
      
    End Sub
    Private Sub Looper1      
        
    'stevec1 = stevec1 + 1
            If stevec1 = 1 Then
                f_out.DigitalWrite(
    True)        'pulse ON
                led_out.DigitalWrite(True)        'LED on
            End If
            
    If stevec1 = pulse_h Then
                f_out.DigitalWrite(
    False)        'pulse OFF
            End If
            
    If stevec1 = led_h Then
                led_out.DigitalWrite(
    False)        'LED OFF
            End If
          
            
    If stevec1 = pulse_l Then
                stevec1 = 
    0
            
    End If
      
    End Sub
    Sub pulse_timer_Tick
        stevec1 = stevec1 + 
    1
      
    '    Select Case stevec1
    '        Case 1
    '            f_out.DigitalWrite(True)        'pulse ON
    '            led_out.DigitalWrite(True)        'LED on
    '        Case pulse_h
    '            f_out.DigitalWrite(False)        'pulse OFF
    '        Case led_h
    '            led_out.DigitalWrite(False)        'LED OFF
    '        Case pulse_l
    '            stevec1 = 0
    '    End Select
      
      
    '    If stevec1 = 1 Then
    '        f_out.DigitalWrite(True)        'pulse ON
    '        led_out.DigitalWrite(True)        'LED on
    '    End If
    '    If stevec1 = pulse_h Then
    '        f_out.DigitalWrite(False)        'pulse OFF
    '    End If
    '    If stevec1 = led_h Then
    '        led_out.DigitalWrite(False)        'LED OFF
    '    End If
    '      
    '    If stevec1 = pulse_l Then
    '        stevec1 = 0
    '    End If  
    End Sub
    Sub astream_NewData (Buffer() As Byte)            'Data from Bluetooth HM-11
      
        
    Dim bc As ByteConverter          
        
    Dim ptr As Int = 0
              
        
    For Each s() As Byte In bc.Split(Buffer,",")
            
    If ptr > Buffer.Length - 1 Then Exit
            bstring(ptr) = bc.StringFromBytes(s)                      
            ptr = ptr + 
    1
        
    Next
          
        
    'Log("ptr = : " , ptr)
        'Log("Buffer Lenght = : " , Buffer.Length)
      
        
    'For x = 0 To ptr
        '    Log(bstring(x))  
        'Next
      
        dekodiraj
          
    End Sub
    Private Sub dekodiraj
        
    If bstring(0) = "S" Then
            pulse_h = bstring(
    1)
            pulse_l = bstring(
    2)
            led_h = (pulse_h + pulse_l)/
    2
            
    'Log("Pulse_h: " , pulse_h, "   Pulse_l: " , pulse_l)      
        End If
        
    If bstring(0) = "Time" Then
            get_time      
        
    End If
    End Sub
    Public Sub get_time
        TimeNIST.Start
        
    Log("Date: ", TimeNIST.GetDate)
        
    Log("Time (UTC): "NumberFormat(TimeNIST.GetHours, 20), ":"NumberFormat(TimeNIST.GetMinutes, 20), _
            
    ":"NumberFormat(TimeNIST.GetSeconds, 20))
    End Sub
     
    Last edited: Oct 20, 2019
  2. thetahsk

    thetahsk Active Member Licensed User

    use Arduino's tone function with inline C
    https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/

    https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Tone.cpp

     
    Last edited: Oct 20, 2019
  3. stari

    stari Active Member Licensed User

  4. stari

    stari Active Member Licensed User

  5. stari

    stari Active Member Licensed User

    Solved !
    Very stable frequencies.




    Code:
    Private Sub F_On(tag As Byte)
        f_out.DigitalWrite(
    True)
        CallSubPlus(
    "F_Off"1,0)
    End Sub
    Private Sub F_Off(tag As Byte)
        f_out.DigitalWrite(
    False)
        CallSubPlus(
    "F_On"24.90)
        
    'CallSubPlus("F_On", 25, 0)
    End Sub
    Private Sub LedOn(tag1 As Byte)
        led_out.DigitalWrite(
    True)
        CallSubPlus(
    "LedOff"13,0)
    End Sub
    Private Sub LedOff(tag1 As Byte)
        led_out.DigitalWrite(
    False)
        CallSubPlus(
    "LedOn"120)
    End Sub
     
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