B4R Question Pin Input

whcir

Member
Licensed User
Longtime User
I have to monitor 6 pins with switches on each pin. My question is what is the best way to do this.
Do I use a timer and pole with digitalread or do I use addlistener for each pin? What are the pros and cons using both methods

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use AddListener. It is simpler to implement.

You can use the same sub event to handle all pins:
B4X:
pin1.AddListener("pin_StateChanged")
pin2.AddListener("pin_StateChanged")
 pin3.AddListener("pin_StateChanged")


Sub Pin_StateChanged (State As Boolean)
   Dim p As Pin = Sender
   Log("Pin: ", p.PinNumber, ", state: ", State)
End Sub
 
Upvote 0

BaGRoS

Active Member
Licensed User
Longtime User
It's possible to AddListener to analog value? State as Int?
I try
B4X:
SpeedMotor.Initialize(SpeedMotor.A0, SpeedMotor.MODE_INPUT)
SpeedMotor.AddListener("SpeedPotencjometr_StateChanged")

Sub SpeedPotencjometr_StateChanged(State As Int)
    Log(State)
End Sub

but status is only 25600 or 25601.
How to properly initialize the pins?
B4X:
SpeedMotor.Initialize(SpeedMotor.A0, SpeedMotor.MODE_INPUT)
SpeedMotor.Initialize(0, SpeedMotor.MODE_INPUT)
SpeedMotor.Initialize(SpeedMotor.A0, SpeedMotor.AnalogRead)
SpeedMotor.Initialize(0, SpeedMotor.AnalogRead)
Read from the potentiometer.
 
Upvote 0
Top