Ultrasonic Sensor

# Module Imports

import LocoIOT

import time


# Class Instances Creation

loco_iot = LocoIOT.LocoIOT()

msg = LocoIOT.IOT_Codes()

# Initiate Connection to the Microcontroller

loco_iot.connect()


# Enable Ultrasonic Sensor Hardware Configuration

loco_iot.enable(msg.SUBTYPE_ULTRA)

loco_iot.enable(msg.SUBTYPE_RGB)

loco_iot.enable(msg.SUBTYPE_PAS_BUZZ)

# Start Listening for Control/Request Messages

loco_iot.start()



# Timed While Loop To Run For 40 Seconds

start_time = time.time()

while (time.time() - start_time) < 40:


# Request Distance Data

dist_dict = loco_iot.getData(msg.SUBTYPE_ULTRA)

x = dist_dict['Distance (cm)']

Output = (x - 3) * (255 - 0) / (210 - 3) + 0

# Print Distance Dictionary

print(dist_dict)

# WARNING - REPLACE ZERO WITH EQUATION FROM THE PREVIOUS QUESTION

output = (x - 3) * (255 - 0) / (210 - 3) + 0

outputs = (x - 3) * (1000 - 500) / (210 - 3) + 500

# Set All RGB Values To The Mapped Output

data = [output, output, output]

loco_iot.setData(msg.SUBTYPE_RGB, data)

datas = [outputs]

loco_iot.setData(msg.SUBTYPE_PAS_BUZZ, datas)

# Short Arbitrary Delay

time.sleep(0.25)

# Close Connection to the Microcontroller

loco_iot.close()