Using an IR sensor to check if an object is within range.
Posted: Wed May 06, 2020 10:55 am
Hi all,
I have an IR Reflective Sensor 1-4mm (ID: 1146_0) and I just want to use it to check if an object is within range of the sensor (i.e. within 1-4mm away).
Here is the example code for my device:
I basically want to add something like this pseudo-code to my main
but I can't figure out what the correct syntax to replace "out of range".
Can anyone help?
Thanks,
Josh
I have an IR Reflective Sensor 1-4mm (ID: 1146_0) and I just want to use it to check if an object is within range of the sensor (i.e. within 1-4mm away).
Here is the example code for my device:
Code: Select all
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time
#Declare any event handlers here. These will be called every time the associated event occurs.
def onSensorChange(self, sensorValue, sensorUnit):
print("SensorValue: " + str(sensorValue))
print("SensorUnit: " + str(sensorUnit.symbol))
print("----------")
def onError(self, code, description):
print("Code: " + ErrorEventCode.getName(code))
print("Description: " + str(description))
print("----------")
def main():
#Create your Phidget channels
voltageRatioInput0 = VoltageRatioInput()
#Set addressing parameters to specify which channel to open (if any)
voltageRatioInput0.setHubPort(5)
#Assign any event handlers you need before calling open so that no events are missed.
voltageRatioInput0.setOnSensorChangeHandler(onSensorChange)
voltageRatioInput0.setOnErrorHandler(onError)
#Open your Phidgets and wait for attachment
voltageRatioInput0.openWaitForAttachment(5000)
#Do stuff with your Phidgets here or in your event handlers.
#Set the sensor type to match the analog sensor you are using after opening the Phidget
voltageRatioInput0.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1146)
try:
input("Press Enter to Stop\n")
except (Exception, KeyboardInterrupt):
pass
#Close your Phidgets once the program is done.
voltageRatioInput0.close()
main()
Code: Select all
if out of range:
print("Out of range.")
else:
print("Distance = {}".format(voltageRatioInput0.getSensorValue()))
Can anyone help?
Thanks,
Josh