from pybricks.pupdevices import ColorSensor from pybricks.parameters import Port, Color from pybricks.tools import wait # Initialize the sensor. sensor = ColorSensor(Port.A) # Here we choose only red, yellow, green, and blue, and provide the hues. hues = {Color.RED: 350, Color.YELLOW: 30, Color.GREEN: 110, Color.BLUE: 210} # Save the updated settings. The values dictionary is empty because we don't # need to detect black/white in this demo application. sensor.color_map(hues, 30, {}) # color() works as usual, but it only returns red/yellow/green/blue, or None. # This avoids accidentally detecting black on the dark gray train tracks. while True: print(sensor.color()) wait(100)
from pybricks.pupdevices import ColorSensor from pybricks.parameters import Port from pybricks.tools import wait # Initialize the sensor. sensor = ColorSensor(Port.A) # Show the default color map. print(sensor.color_map()) while True: # Read the HSV values. h, s, v = sensor.hsv() # Read the corresponding color based on the existing settings. color = sensor.color() # Print the measured values. print("Hue:", h, "Sat:", s, "Val:", v, "Col:", color) # Wait so we can read the value. wait(100)
from pybricks.pupdevices import ColorSensor from pybricks.parameters import Port, Color from pybricks.tools import wait # Initialize the sensor. sensor = ColorSensor(Port.A) # Get the current color settings. hues, saturation, values = sensor.color_map() # Let's say we have used the hsv() method to determine # that the orange hue is 10. We can add it like this. hues[Color.ORANGE] = 10 # Also, let's say that we are not interested in blue. # So, we remove it from the hues dictionary. hues.pop(Color.BLUE) # Now save the new settings sensor.color_map(hues, saturation, values) # Now use the color() method as usual. Now it can also detect ORANGE # and it will not report BLUE, because we removed it from the hues. while True: print(sensor.color()) wait(100)