Then print the value to the Python shell for debug. When pressed it will append the RGB color value to the player list. Create an if statement to read the state of each button, store the color pressed and show the value for debug. Use a while loop to check the length of the player list, and add a 0.3 second delay to this loop to reduce the chance of double button presses (debounce) The indented code will only run while there are less than four guesses by the player. Use the randomly chosen delay to show the color then set turn the LEDs off, using the same delay. Each time the loop iterates the RGB value is used with pixels.fill() to show that color. This for loop iterates over the four colors stored in the rnd_colors list. These will be the colors that the player has to guess. Use a for loop to add four randomly chosen colors to the rnd_colors list and print them to the Python shell for debug. This will generate a random number between 0.3 and 1, used to create a pause between the color sequence shown to the player. Print the output to the Python shell for debug. Wrap the round() function around this to format the output to one decimal place. Create a variable, “delay,” to store a randomly chosen float value between 0.3 and 1.0. So when the button is pressed the pins are pulled low. The buttons are connected to GPIO pins pulled high, and to GND. As this pin is pulled high using a pull up resistor, we need to trigger the game to start when the pin is low (False). To start the game we need to press the green button. Create an if conditional test to trigger the game to start. Create two blank lists to store the randomly chosen color sequence, and to store the players guesses. Create a while True loop and call the pixels object to turn off the LEDs. The main body of the project is a while True, which will run the game code. Each time the loop iterates it uses the rainbow_cycle function to create a light show when the game starts. Create a for loop which will repeat 10 times. Create a list called “colors,” and use it to store the color tuples we created earlier. This function is part of Adafruit’s standard “strand test” demo. Create a function, rainbow_cycle which will cycle through all of the NeoPixels, displaying a rainbow effect. Create five tuples (data storage objects) to store the RGB color values for the buttons and to turn the LEDs off. The way we have connected the button to the Pico will pull this pin to GND (low) when the red button is pressed. Using a pull up resistor we pull GPIO 27 high to 3.3V. Set the GPIO pin for the red button so that it pulls high. Set the GPIO pin for the red button as an input. Create an object, red, which represents the red button connected to GPIO 27. pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)Ĩ. We also set the brightness to 0.3 (30%) to reduce eye strain. Create an object, “pixels” and use it to connect to the NeoPixels using the two variables that identify the GPIO pin and the number of pixels. Setup GPIO 28 as the pin used to control our NeoPixels, then create a variable called num_pixels to store the number of NeoPixel LEDs. from random import choice, uniformįrom digitalio import DigitalInOut, Direction, PullĦ. In this case we want to use inputs (buttons) and set the internal resistor of the GPIO pins so that it pulls up (3.3V). DigitalIO works with the Board library to finetune how we use the GPIO. Uniform is a random number generator for float numbers. Random has many functions but choice enables us to pick “randomly” from lists, tuples, dictionaries etc. From random import choice and uniform, and from digitailio import DigitalInOut, Direction and Pull to handle input / output, pin direction and support for pull up resistors (see resistor color codes for help identifying different sizes). NeoPixel is the library used to control the eight RGB LEDs. Colorwheel is an animation tool that we will use to create a rainbow cycle effect. Import two more libraries for advanced control of NeoPixels. Time contains functions to pause the code, whereas Board provides basic GPIO access. Time and Board are used common CircuitPython libraries. Import libraries to control the speed of the game, and for access to the GPIO. Open the code.py file found in the root of the CIRCUITPY drive in your preferred editor.ģ. (Image credit: Tom's Hardware) Writing the Code for Simon Game on Raspberry Pi Picoġ.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |