Im having a problem with Pulse Width Frequency using arduino uno. Here's my code
This is the error im getting.
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: