Sub Process_Globals
Public Serial1 As Serial
Private tm As TM1637Display
Private counter As Int
Private buttonPin As Pin
Private lastButtonState As Boolean
Private lastDebounceTime As Long
Private debounceDelay As Long
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
tm.Initialize(2, 3) ' Initialize the display
buttonPin.Initialize(4, 0) ' Initialize button pin (change 4 to your actual button pin)
counter = 0 ' Initialize counter to 0
lastButtonState = False
lastDebounceTime = 0
debounceDelay = 50 ' Adjust debounce delay as needed
' Set up a loop to continuously check the button state
Dim loopTimer As Timer
loopTimer.Initialize("LoopTimer_Tick", 50)
loopTimer.Enabled = True
End Sub
Sub LoopTimer_Tick
Dim currentButtonState As Boolean
currentButtonState = buttonPin.Read() = 0 ' Assuming button press pulls the pin low (change to 1 if high)
' Check if the button state has changed and debounce
If currentButtonState <> lastButtonState Then
lastDebounceTime = Millis
End If
If (Millis - lastDebounceTime) > debounceDelay Then
If currentButtonState <> lastButtonState Then
lastButtonState = currentButtonState
' Increment counter if button is pressed
If currentButtonState = True Then ' Button pressed
counter = counter + 1
tm.ShowNumberDec2(counter, True, 4, 0)
End If
End If
End If
End Sub
DigitalRead As Boolean
Reads the pin value and returns true or false.
Private buttonPin As Pin
...
Sub LoopTimer_Tick
Dim currentButtonState As Boolean
currentButtonState = (buttonPin.DigitalRead() = False) ' Assuming button press pulls the pin low (False?)
Should Read not be DigitalRead?
https://www.b4x.com/b4r/help/core.html#pin_digitalread
Also, it returns Boolean, thus "= 0" is not needed.
Although you might still need to invert the state, if button is "active low" eg:
B4X:Private buttonPin As Pin ... Sub LoopTimer_Tick Dim currentButtonState As Boolean currentButtonState = (buttonPin.DigitalRead() = False) ' Assuming button press pulls the pin low (False?)
Sub Process_Globals
Public Serial1 As Serial
Private tm As TM1637Display
Private counter As Int
Private buttonPin As Pin
Private lastButtonState As Boolean
Private lastDebounceTime As Long
Private debounceDelay As Long
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
tm.Initialize(2, 3) ' Initialize the display
buttonPin.Initialize(4, 0) ' Initialize button pin (change 4 to your actual button pin)
counter = 0 ' Initialize counter to 0
lastButtonState = False
lastDebounceTime = 0
debounceDelay = 50 ' Adjust debounce delay as needed
' Set up a loop to continuously check the button state
Dim loopTimer As Timer
loopTimer.Initialize("LoopTimer_Tick", 50)
loopTimer.Enabled = True
End Sub
'Sub LoopTimer_Tick
' Dim currentButtonState As Boolean
' currentButtonState = buttonPin.Read() = 0 ' Assuming button press pulls the pin low (change to 1 if high)
Sub LoopTimer_Tick
Dim currentButtonState As Boolean
currentButtonState = (buttonPin.DigitalRead() = False) ' Assuming button press pulls the pin low (False?)
' Check if the button state has changed and debounce
If currentButtonState <> lastButtonState Then
lastDebounceTime = Millis
End If
If (Millis - lastDebounceTime) > debounceDelay Then
If currentButtonState <> lastButtonState Then
lastButtonState = currentButtonState
' Increment counter if button is pressed
If currentButtonState = True Then ' Button pressed
counter = counter + 1
tm.ShowNumberDec2(counter, True, 4, 0)
End If
End If
End If
End Sub
Button is between pin4 and gnd. Any meaning for that?
Button is between pin4 and gnd. Any meaning for that?
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Public E4 As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("Inicio Aplicacion")
E4.Initialize(4,E4.MODE_INPUT_PULLUP)
E4.AddListener(CH_E4)
End Sub
Sub CH_E4(Estado As Boolean)
If Estado = Treu Then
Log("Entrada Activa")
Else
Log("Entrada Desactiva")
End If
End Sub
Button is between pin4 and gnd. Any meaning for that?
How is it pulled high when the button is not pressed?
Is it done internally within the microcontroller, or by the Arduino (or ESP32?) board, or by your own circuitry with a resistor to VCC?
What board or module are you using?
I reviewed the code and found nothing needs to be corrected .
buttonPin.Initialize(4, 0) ' Initialize button pin (change 4 to your actual button pin)
Well, if your project is not working then something needs to be corrected.
More ideas:
Arduino pin numbering starts from 0. For most bus and chip pin numbering, the first pin is pin 1, but for Arduino shields the first pin is Pin0 aka D0. Thus Pin4 is actually the 5th pin along the shield connector. I think everybody's miscounted pin numbers at least once ? and experts probably more than once ?
Maybe try using Pin2 (the third pin along) instead of Pin4, because Pin2 doesn't have any other additional functions that might be interfering with its use as a digital input.
An infra-red diode was reversed
You're not the first genius to do that ? so you're in good company. Welcome to the club! ?