MicroPython Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

The first line of code imports the Circuit Playground Express library and the time library. Then, the color constants and brightness level are set. The i variable will keep track of which pixel is currently lit. The direction variable will either have the value of 1 or -1 and will control whether the pixel moves in a clockwise or counterclockwise direction.

In the infinite loop, the direction will be changed if either push button A or push button B is pressed. The direction is applied to the position and a modulus 10 operation is applied so that the position value rotates between the values of 0 and 10.

In each iteration, all the pixels are turned off and then the selected pixel is turned on. The speed of the light animation is controlled with a call to make the board sleep for 50 milliseconds between each loop iteration.