### GPIO Pin State Change

B4J Raspberry Pi Experiment - GPIO Pin1 State Change
App Type: B4J Non-UI (Console)
Parts Needed
• 1x Raspberry Pi
Wiring
Not used.
Description
Connecting the Parts
Each GPIO pin can be an input pin or an output pin:
• Input pins allow to listen for state changes.
• Output pins allow to set their state.
This experiment initializes two pins:
• Pin1 is an input pin
• Pin2 is an output pin.
• Pin1 state changes every 5 seconds with a Timer
Dependencies: the jPi4J Library (see category Raspberry Pi Libraries > jPi4J).
Developing the B4J non-UI program
The B4J pseudo code:
• Init the gpio controller
• Init the pins output, input and define the related statechange evnt for the inout pi
• Init the timer and start
• Start the message loop
• Handle pininput statechange event
Published External
No
ToDo
• Experiment is completed.
Version
20170304
B4J non-UI Example Code
`Sub Process_Globals
Private controller As GpioController
Private PinOutput As GpioPinDigitalOutput
Private PinInput As GpioPinDigitalInput
Private Timer1 As Timer
End Sub`
`Sub AppStart (Args() As String)
controller.Initialize
PinOutput.Initialize(1, True)
PinInput.Initialize("PinInput", 2)
PinInput.SetPinPullResistance("PULL_DOWN")
Log("Monitoring Pin2 state")
Timer1.Initialize("Timer1", 5000)
Timer1.Enabled = True
StartMessageLoop
End Sub`
`Sub Timer1_Tick
PinOutput.State = Not(PinOutput.State)
Log("Switching PinOutput state to " & Pin1.State)
End Sub`
`Sub PinInput_StateChange(State As Boolean)
Log("PinInput StateChange event to " & State)
End Sub`
Running the Experiment
On the RasPi start the B4J-Bridge by login on as user Pi, open a terminal, go to folder /home/pi/b4j and run sudo java -jar b4-bridge.jar, note the MyIp address.
Open the B4J IDE and load the B4J project file, connect to the B4J-Bridge (Menu Tools > B4J Bridge > Connect > Enter IP address.
Run the application from the B4J IDE by pressing F5.
Install on the Raspberry Pi
Example:
• Create a folder on the RPi, like /home/pi/b4j/rpipinchange
• Copy, using WinSCP, rpipinchange.jar from the Development Device to the folder created
• Run the application from the commandline: java -jar rpipinchange.jar

Changed: 2018-04-14 18:47, Category: Raspberry Pi Sample Projects, File: T6C238M1738.htm, ID: 1738