B4J Question [Solved] Newbie: Detect a switch on RPi GPIO

Mark Read

Well-Known Member
Licensed User
Longtime User
I have just got my sunfounder RPi kit and am slowly working through the examples. I am new to this so please excuse the dumb question.

I have a LED connected to GPIO Pin0 and a switch to pin1. If the switch is pressed the LED should turn on. I am trying all the examples with B4J as I have a project lined up. I am probably making a very simple mistake.

My code is:

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
  
   Private controller As GpioController
   Private pin0 As GpioPinDigitalOutput
   Private pin1 As GpioPinDigitalInput  
End Sub

Sub AppStart (Args() As String)
   controller.Initialize
   pin0.Initialize(0, True)
   pin1.Initialize("SwitchValue",1)
   StartMessageLoop
End Sub

Sub SwitchValue_StateChange(State As Boolean)
    Log("Switch: " & State)
    pin0.State = Not(pin0.State)
End Sub

Sub pin0_StateChange(State As Boolean)
   Log("Pin0 StateChange event: " & pin0.State)
End Sub

Neither sub executes. Would someone please explain what I have done wrong. Many thanks.

Ps. RPi Model 3
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Great Erel, :D:D.

The sub pin0_StateChange was a left over from the first example - deleted. Added your line and set the initialize to false. Now it works. Many thanks. Learning is fun!
 
Upvote 0
Top