- MicroPython Cookbook
- Marwan Alsabbagh
- 143字
- 2021-06-24 14:28:05
How to do it...
To do this, perform the following steps:
- Run the following lines of code in the REPL:
>>> from adafruit_circuitplayground.express import cpx >>> from random import randint >>> randint(0, 255) 186 >>> randint(0, 255) 84
- Each time you run the previous lines of code, you should get a random integer between the values of 0 and 255.
- Use the following code to define a function, and then call the function to confirm that it is working correctly:
>>> def get_random_color(): ... return (randint(0, 255), randint(0, 255), randint(0, 255)) ... ... ... >>> get_random_color() (208, 161, 71) >>> get_random_color() (96, 126, 158)
- Call the following code repeatedly; the first NeoPixel should change to a random color on each call:
>>> cpx.pixels[0] = get_random_color()
- Set all the pixels to the same random color on each call using the following code:
>>> cpx.pixels.fill(get_random_color())