Let's improve our code to make it more re-usable! Variables for GPIO pin, LED count and colour Our Bouncing Lights code could have been better if we used variables for the number of LEDs, meaning changing this once would fix it everywhere each time we use a new type of RGB LED. However, we would also need to change any reference to 12 LEDs throughout the code too, because our strand has 15. Take our 'Bouncing Lights' program from day #8 for example - all we need to do is set this line to GPIO2 and 15 LEDs to address our strand: strand = NeoPixel(Pin(2), 15) This means you can re-use any of the examples programs (and anything you've come up with yourself) just by setting the pin reference and number of LEDs. The cool thing about our addressable ring and strand is that they work in the same way and are both RGB. # Define the strand pin number (2) and number of LEDs (15)Īctivity #2: Re-using (and improving) LED ring programs We then show red at low brightness (50,0,0) for 10 seconds, enough time to check everything's working.Ĭopy the code over to Thonny and give it a try: # Imports You already know your stuff when it comes to controlling RGB LEDs, so let's run a quick test to make sure everything's wired correctly and working as it should.Īs you'd expect, we've imported the neopixel library, set our dot strand pin number up ( GPIO2) and set the number of LEDs ( 15). Tip: You could wire this dot strand straight into the breadboard holes next to the Pico as 5V (VBUS), GND and a GPIO pin are all close together enough, however this adds some risk of wiring things up incorrectly as it's not as clear and visible.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |