# Module Import
import LocoIOT
import time
# Class Instances Creation
loco_iot = LocoIOT.LocoIOT()
msg = LocoIOT.IOT_Codes()
# Initiate Connection to the Microcontroller
loco_iot.connect()
# Enable Photocell Hardware Configuration
loco_iot.enable(msg.SUBTYPE_PHOTO)
loco_iot.enable(msg.SUBTYPE_PAS_BUZZ)
# Start Listening for Control/Request Messages
loco_iot.start()
# Map an Input Value to a Different Range of Values
#Question 3
cover = input("Cover the photocell sensing surface, then press Enter:")
photo_dict = loco_iot.getData(msg.SUBTYPE_PHOTO)
photo_min = photo_dict['Photocell']
#Question 4
expose = input("Expose the photocell sensing surface to a light source, then press Enter:")
photo_dict = loco_iot.getData(msg.SUBTYPE_PHOTO)
photo_max = photo_dict['Photocell']
def map_to_range(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# Infinite Loop: Press STOP to end the loop.
while True:
# Update Photocell Light Data
photo_dict = loco_iot.getData(msg.SUBTYPE_PHOTO)
photo_now = photo_dict['Photocell']
buzz = map_to_range(photo_now, photo_min, photo_max, 532, 932)
loco_iot.setData(msg.SUBTYPE_PAS_BUZZ, [532], [buzz], [932])
# Pause program execution for the buzzer duration
time.sleep(0.5)
# Close Connection to the Microcontroller
loco_iot.close()