diff --git a/snippets/pup/sensor_color/color_map_modify.py b/snippets/pup/sensor_color/color_map_modify.py new file mode 100644 index 0000000..9af70e2 --- /dev/null +++ b/snippets/pup/sensor_color/color_map_modify.py @@ -0,0 +1,26 @@ +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) diff --git a/snippets/pup/sensor_color/color_map_new.py b/snippets/pup/sensor_color/color_map_new.py new file mode 100644 index 0000000..3f24925 --- /dev/null +++ b/snippets/pup/sensor_color/color_map_new.py @@ -0,0 +1,24 @@ +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) diff --git a/snippets/pup/sensor_color/color_print.py b/snippets/pup/sensor_color/color_print.py new file mode 100644 index 0000000..d456f51 --- /dev/null +++ b/snippets/pup/sensor_color/color_print.py @@ -0,0 +1,19 @@ +from pybricks.pupdevices import ColorSensor +from pybricks.parameters import Port +from pybricks.tools import wait + +# Initialize the sensor. +sensor = ColorSensor(Port.A) + +while True: + # Read the color. + color = sensor.color() + + # Print the measured color. + print(color) + + # Move the sensor around and see how + # well you can detect colors. + + # Wait so we can read the value. + wait(100) diff --git a/snippets/pup/sensor_color/hsv.py b/snippets/pup/sensor_color/hsv.py new file mode 100644 index 0000000..3a08cc6 --- /dev/null +++ b/snippets/pup/sensor_color/hsv.py @@ -0,0 +1,22 @@ +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) diff --git a/snippets/pup/sensor_color/lights_blink.py b/snippets/pup/sensor_color/lights_blink.py new file mode 100644 index 0000000..08c72b3 --- /dev/null +++ b/snippets/pup/sensor_color/lights_blink.py @@ -0,0 +1,27 @@ +from pybricks.pupdevices import ColorSensor +from pybricks.parameters import Port +from pybricks.tools import wait + +# Initialize the sensor. +sensor = ColorSensor(Port.A) + +# Repeat forever. +while True: + + # Turn on one light at a time, at half the brightness. + # Do this for all 3 lights and repeat that 5 times. + for i in range(5): + sensor.lights.on(50, 0, 0) + wait(100) + sensor.lights.on(0, 50, 0) + wait(100) + sensor.lights.on(0, 0, 50) + wait(100) + + # Turn all lights on at maximum brightness. + sensor.lights.on(100) + wait(500) + + # Turn all lights off. + sensor.lights.off() + wait(500) diff --git a/snippets/pup/sensor_color/wait_for_color.py b/snippets/pup/sensor_color/wait_for_color.py new file mode 100644 index 0000000..aa6b58e --- /dev/null +++ b/snippets/pup/sensor_color/wait_for_color.py @@ -0,0 +1,27 @@ +from pybricks.pupdevices import ColorSensor +from pybricks.parameters import Port, Color +from pybricks.tools import wait + +# Initialize the sensor. +sensor = ColorSensor(Port.A) + + +# This is a function that waits for a desired color. +def wait_for_color(desired_color): + # While the color is not the desired color, we keep waiting. + while sensor.color() != desired_color: + wait(20) + + +# Now we use the function we just created above. +while True: + + # Here you can make your train/vehicle go forward. + + print("Waiting for red ...") + wait_for_color(Color.RED) + + # Here you can make your train/vehicle go backward. + + print("Waiting for blue ...") + wait_for_color(Color.BLUE)