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

Discussion in 'B4J Questions' started by mark35at, Aug 2, 2016.

  1. mark35at

    mark35at Well-Known Member Licensed 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:

    Code:
    '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(
    0True)
       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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    (There is no StateChange event for outputs)

    Are you able to see the logs?

    Add pin1.SetPinPullResistance("PULL_UP")
     
  3. mark35at

    mark35at Well-Known Member Licensed 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!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice