Files
pynamics/templates/color-sensor-start-logic.py

62 lines
2.2 KiB
Python
Raw Normal View History

from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Color, Port
from pybricks.tools import run_task
from pybricks.tools import wait
from pybricks.hubs import PrimeHub
hub = PrimeHub()
color_sensor = ColorSensor(Port.F) # Change the port to your color sensor's port
# Function to classify color based on HSV
def detect_color(h, s, v, reflected):
if reflected > 4:
if h < 4 or h > 350: # red
return "Red"
elif 3 < h < 40 and s > 70: # orange
return "Orange"
elif 47 < h < 56: # yellow
return "Yellow"
elif 70 < h < 160: # green - your brick should approach from the top for accuracy
return "Green"
elif 195 < h < 198: # light blue
return "Light_Blue"
elif 210 < h < 225: # blue - your brick should approach from the top for accuracy
return "Blue"
elif 260 < h < 350: # purple
return "Purple"
else:
return "Unknown"
return "Unknown"
async def main():
while True:
h, s, v = await color_sensor.hsv()
reflected = await color_sensor.reflection()
color = detect_color(h, s, v, reflected)
if color == "Green":
print('Running Task 1')
# Run a function with await Function() here
elif color == "Red":
print('Running Task 2')
# Run a function with await Function() here
elif color == "Yellow":
print('Running Task 3')
# Run a function with await Function() here
elif color == "Blue":
print('Running Task 4')
# Run a function with await Function() here
elif color == "Orange":
print('Running Task 5')
# Run a function with await Function() here
elif color == "Purple":
print('Running Task 6')
# Run a function with await Function() here
elif color == "Light_Blue":
print("Running Task 7")
# Run a function with await Function() here
else:
print(f"Unknown color detected (Hue: {h}, Sat: {s}, Val: {v})")
#pass
await wait(10)
# Run the main function
run_task(main())