Back to: Multiplier Event Luxembourg
import time from adafruit_circuitplayground import cp # Function map_range from library simpleio. def map_range( x: float, in_min: float, in_max: float, out_min: float, out_max: float ) -> float: """ Maps a number from one range to another. Note: This implementation handles values < in_min differently than arduino's map function does. :return: Returns value mapped to new range :rtype: float """ in_range = in_max - in_min in_delta = x - in_min if in_range != 0: mapped = in_delta / in_range elif in_delta != 0: mapped = in_delta else: mapped = 0.5 mapped *= out_max - out_min mapped += out_min if out_min <= out_max: return max(min(mapped, out_max), out_min) return min(max(mapped, out_max), out_min) while True: # If button A is pressed, all NeoPixels are turned off. if cp.button_a: cp.pixels.fill((0, 0, 0)) # If button B is pressed, the color will be detected. if cp.button_b: # Make sure that all NeoPixels are turned off. cp.pixels.fill((0, 0, 0)) time.sleep(0.1) # Get red value. cp.pixels[1] = (255, 0, 0) red = cp.light cp.pixels.fill((0, 0, 0)) time.sleep(0.1) # Get green value. cp.pixels[1] = (0, 255, 0) green = cp.light cp.pixels.fill((0, 0, 0)) time.sleep(0.1) # Get blue value. cp.pixels[1] = (0, 0, 255) blue = cp.light cp.pixels.fill((0, 0, 0)) time.sleep(0.1) # Determine the lowest and highest light readings. minimum = min(red, green, blue) maximum = max(red, green, blue) # Map light from between minimum and maximum reading to 0-255. red = int(map_range(red, minimum, maximum, 0, 255)) green = int(map_range(green, minimum, maximum, 0, 255)) blue = int(map_range(blue, minimum, maximum, 0, 255)) # Reduce the range of detected colors. if red < 30: red = 0 if green < 30: green = 0 if blue < 30: blue = 0 # Depending on the switch, write to # * the serial console (if True, i.e. left), or to # * the CircuitPython storage (if False, i.e. right). if cp.switch: # Distinguish between a good reading and a bad reading. if maximum - minimum <= 30: cp.pixels.fill((0, 0, 0)) else: cp.pixels.fill((red, green, blue)) print( "Output RGB is (", red, ",", green, ",", blue, ")") # Print the red, green and blue values to match the color lines on the Mu plotter. # The orange line represents red. print( (blue, green, red) ) else: f = open("rgb.csv", "a") f.write(repr(red) + "," + repr(green) + "," + repr(blue) + "\n") f.close()
import storage from adafruit_circuitplayground import cp storage.remount("/", readonly=cp.switch)