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
 
You should use AddListener. It is simpler to implement.

You can use the same sub event to handle all pins:
Code: Copy
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
It's possible to AddListener to analog value? State as Int?
I try
Code: Copy
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?
Code: Copy
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
Back
Top