B4R Question How to adjust PWM frequency?

Chris Guanzon

Active Member
Licensed User
Longtime User
Im having a problem with Pulse Width Frequency using arduino uno. Here's my code

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private pinButton As Pin ', 'for sample pinPot, pinLED
    Private SateON_OFF = False As Boolean    'state ON or OFF
    Private MaxPulsePeriod = 2000 As ULong 'Max PulsePeriod value default 2000
    Private PulseWidth = 20 As ULong            'remains constant default 20
    Private PulsePeriod As ULong                    'calculated in TimerPulse_Tick
    Private BounceTime As ULong
    Private BounceDelay = 10 As ULong  'default 10
    Private cnt As Int = 0
   
End Sub

Private Sub AppStart
   
    Serial1.Initialize(115200)
    'Log("AppStart") 'for testing
   
    'Using the internal pull up resistor to prevent the pin from floating.
    pinButton.Initialize(2, pinButton.MODE_INPUT_PULLUP)
    pinButton.AddListener("pinButton_StateChanged")
    SetPulseWidthFrequency
End Sub

Private Sub pinButton_StateChanged (State As Boolean)
    'Log("states: ", State) 'for testing
   
    If State = True Then
        cnt = cnt + 1
        Log(cnt)
    End If
   
    'state will be False when the button is clicked because of the PULLUP mode.
    If State = False Then
        If Millis - BounceTime < BounceDelay Then
            Return
        Else
            SateON_OFF = Not(SateON_OFF)
            'Log("SateON_OFF: ", SateON_OFF) 'for testing
            BounceTime = Millis
            If SateON_OFF = True Then
                NextPulse(0)
            Else
                pinButton.DigitalWrite(False)
            End If
        End If
    End If


End Sub

Private Sub NextPulse(Tag As Byte)
    If SateON_OFF = True Then
        PulsePeriod = MapRange(pinButton.AnalogRead, 0, 1023, PulseWidth, MaxPulsePeriod)
        If PulsePeriod >= MaxPulsePeriod - 1 Then
            pinButton.DigitalWrite(False)
        Else if PulsePeriod <= PulsePeriod + 1 Then
            pinButton.DigitalWrite(True)
        Else
            pinButton.DigitalWrite(True)
            CallSubPlus("EndPulse", PulsePeriod, 0)
        End If
        CallSubPlus("NextPulse", PulsePeriod, 0)
        'Log("NextPulse")                            'Log for testing
    End If
End Sub

Private Sub EndPulse(Tag As Byte)

    pinButton.DigitalWrite(False)            'switch the LED OFF
'    Log("EndPulse")                                    'Log for testing
End Sub

Sub SetPulseWidthFrequency
    Dim pwm_freq As Int = 100 ' 100hz
    RunNative ("SetPWMFreq",Null)
End Sub
 
#If C
    void SetPWMFreq(B4R::Object* o)
    {  
    analogWrite(b4r_main::_pwm_freq);
    }
#End If

This is the error im getting.

B4X:
Building project    (0.41s)
Compiling & deploying Ino project (Arduino/Genuino Uno - COM9)    Error
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
D:\MYD7C1~1\MYSAMP~1\BILLVA~1\BV20\Objects\bin\sketch\b4r_main.cpp: In function 'void SetPWMFreq(B4R::Object*)':
b4r_main.cpp:20:5: error: 'analogWriteFreq' was not declared in this scope
     analogWriteFreq(b4r_main::_pwm_freq);
     ^~~~~~~~~~~~~~~
D:\MYD7C1~1\MYSAMP~1\BILLVA~1\BV20\Objects\bin\sketch\b4r_main.cpp:20:5: note: suggested alternative: 'analogWrite'
     analogWriteFreq(b4r_main::_pwm_freq);
     ^~~~~~~~~~~~~~~
     analogWrite
exit status 1
 
Last edited:

Mark Read

Well-Known Member
Licensed User
Longtime User
I think you may have a small mistake. Try this code from here.

B4X:
dim pwm_freq as int = 100 ' 100hz
RunNative ("SetPWMFreq",Null)
end sub


#If C
void SetPWMFreq(B4R::Object* o)
{
    analogWriteFreq(b4r_main::_pwm_freq);
}
#End If
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
i already changed it. but still got an error.

Have a look to the function definition:

Syntax
analogWrite(pin, value)

Parameters
pin: the Arduino pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.

Returns
Nothing
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
ive change from this

B4X:
Sub SetPulseWidthFrequency
    Dim pwm_freq As Int = 100 ' 100hz
    RunNative ("SetPWMFreq",Null)
End Sub
#If C
    void SetPWMFreq(B4R::Object* o)
    {
    analogWrite(b4r_main::_pwm_freq);
    }
#End If

into this

B4X:
Sub SetPulseWidthFrequency
    Dim pwm_freq As Int = 100 ' 100hz
    RunNative ("SetPWMFreq",Null)
End Sub
#If C
    void SetPWMFreq(B4R::Object* o)
    {
    analogWriteFreq(b4r_main::_pwm_freq);
    }
#End If

and still get an error. This is the error:

B4X:
Verifying...
D:\MYD7C1~1\MYSAMP~1\BILLVA~1\BV20\Objects\bin\sketch\b4r_main.cpp: In function 'void SetPWMFreq(B4R::Object*)':
b4r_main.cpp:18:31: error: '_pwm_freq' is not a member of 'b4r_main'
     analogWriteFreq(b4r_main::_pwm_freq);
                               ^~~~~~~~~
b4r_main.cpp:18:5: error: 'analogWriteFreq' was not declared in this scope
     analogWriteFreq(b4r_main::_pwm_freq);
     ^~~~~~~~~~~~~~~
D:\MYD7C1~1\MYSAMP~1\BILLVA~1\BV20\Objects\bin\sketch\b4r_main.cpp:18:5: note: suggested alternative: 'analogWrite'
     analogWriteFreq(b4r_main::_pwm_freq);
     ^~~~~~~~~~~~~~~
     analogWrite
exit status 1
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
What libraries and versions are you using?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I am not an expert but I don't see the method "analogWriteFreq()" in the core library but it is in the rESP8266 library. I think that the method you are using will not work on the Uno as it uses a different chip (Atmega...) and not the 8266 chip.

The analogWrite method is in the core library but I think you have to use it in a different way.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
This is what I meant: Link

Sorry if this does not help you.
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
Have you read the documentation, there is no function with such a prototype as you wrote in your file. You must add an PWM supporting pin.
B4X:
void SetPWMFreq(B4R::Object* o)
    {
    analogWrite(br4_main::your_pin_that_supports_PWM,b4r_main::_pwm_freq);
    }
#End If
 
Upvote 0
Top