Hello all,
I tried to modify Erel's code here , so that I can replace the generated clock on pin 2 of Arduino, with an external clock, here's the code and the logs at the end.
Please help correct the code:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Note: I am trying to use a mechanical switch for clocking the 1637
			
			I tried to modify Erel's code here , so that I can replace the generated clock on pin 2 of Arduino, with an external clock, here's the code and the logs at the end.
Please help correct the code:
			
				TM1637 and Arduno with external clock:
			
		
		
		Sub Process_Globals
    Public Serial1 As Serial
    Private tm As TM1637Display
    Private switchPin As Pin
    Private counter As Int
    Private lastSwitchState As Boolean
    Private debounceTime As Long
    Private lastDebounceTime As Long
End Sub
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    ' Initialize TM1637 display
    tm.Initialize(2, 3)
  
    ' Initialize switch pin (connected to pin 4)
    switchPin.Initialize(4, Pin.INPUT_PULLUP)  ' Set pin mode to input with internal pull-up resistor
  
    ' Initialize the counter
    counter = 0
    lastSwitchState = True  ' Assume switch is not pressed initially
    debounceTime = 200  ' Debounce time in milliseconds
  
    ' Initialize a timer for periodic tasks, if needed
    'timer1.Initialize("Timer1_Tick", 1000)
    'timer1.Enabled = True
End Sub
Private Sub Timer1_Tick
    ' Optional: This can be used for other periodic tasks
End Sub
Private Sub HandleSwitch
    Dim currentSwitchState As Boolean
    currentSwitchState = switchPin.Value = Pin.LOW  ' Assuming LOW means pressed
    ' Check if the switch state has changed (falling edge detection)
    If lastSwitchState = True And currentSwitchState = False Then
        ' Increment the counter and update the display
        counter = counter + 1
        tm.ShowNumberDec2(counter, True, 4, 0)
      
        ' Record the time of this action
        lastDebounceTime = Millis
    End If
    ' Update the last switch state
    lastSwitchState = currentSwitchState
    ' Optional: Debounce logic
    If Millis - lastDebounceTime > debounceTime Then
        ' Additional debounce logic (if needed)
    End If
End Sub
Private Sub Loop
    ' Continuously check the switch state
    HandleSwitch
End Sub
'logs
'Main - 37: Undeclared variable 'pin' is used before it was assigned any value.
'Main - 19: Undeclared variable 'pin' is used before it was assigned any value.
'Main - 61: Syntax error
'Main - 58: 'end sub' is missing.Note: I am trying to use a mechanical switch for clocking the 1637
			
				Last edited: 
			
		
	
								
								
									
	
		
			
		
	
								
							
							 
				 
 
		 
 
		 
 
		 
 
		 
 
		