Outils pour utilisateurs

Outils du site


issue181:micro-ci

Ceci est une ancienne révision du document !


First, please allow me to apologize for not having a Micro This Micro That. I was having very bad issues with my vision and it was all I could do to get the “normal” Python article done. It’s not much better right now, especially when trying to wire up a circuit. However, we will press onward as best as we can. Think back two months to part 13 (which was in FCM #179). We were using LSM 303 with the Raspberry Pi Pico to create a digital compass. The final product was obtaining a normalized integer value that gave us the number of degrees off of North. So if we were standing with the breadboard and sensor pointing to the East, we would get 90 as our heading. This month, we will use a 24 pixel NeoPixel ring. Since there are 360 degrees in our circle and we have 24 LEDs, that would mean that each of our LEDs would represent 15 degrees. So if LED[0] represents North, East would then be LED[6], South would be LED[12] and West would be LED[18].

The basic algorithm (in pseudo code) is something like shown above. We’ll have to add the Neopixel ring to the breadboard. Remember, we will also have to have a 3 x AA rechargeable battery pack to power the Neopixel ring. Please notice that the image shows using a 3 AAA battery pack, but it is really a 3 AA battery pack. Also notice that there is a ground connection between the Neopixel ring, the battery pack and the RPi Pico. This month, we will create a test program that will verify the logic that we will use to integrate the heading from the LSM303 to the NeoPixel ring. Of course, we have to start with the imports. We will also create some variables that will be needed for the Neopixel ring. import array, time from machine import Pin import rp2 # Configure the number of WS2812 LEDs, pins and brightness. NUM_LEDS = 24 PIN_NUM = 16 brightness = 0.1

Now we need to create the driver. We will embed this (right) into our program. Remember that we set which NeoPixel, and its color, using the pixels_set() function, but until the pixels_show() function is called, the Neopixel LED doesn’t actually light. Now we set a few constants that provide the RGB values for some predefined colors. BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) WHITE = (255, 255, 255) Now (next page, top right) we can create a few support functions that we will need to make our life easier.

We will use the Red color to mark North (LED[0]) and Blue to mark East, South and West. When we get to it, the color that marks the heading will be in Green (shown bottom left). The turn_off_all() function simply sets all the pixels on the ring to BLACK (or off). We’ve put the markers (which should pretty much stay lit unless the heading is one of those directions) for North, South, East and West into a list so we can check to see if we are on a marker LED. North will set LED[0] to Red and the other three are set to Blue. Now the real worker function is set_heading(). This function (shown middle right) embodies the logic from the pseudo code we created earlier.

Finally, we create the logic to control everything (shown bottom right). To simulate moving around in a circle, we use a for loop, stepping from 0 to 361, and pass that value into our set_heading() function to light the correct LED. Then we reverse the for loop to simulate moving in an anti-clockwise circle. Save this program as CompassDisplay1.py. When you run it, you should see the four marker LEDs then after a short delay, you should see the green LED marking our heading move around the ring then move back around to 0 (the red LED). It’s somewhat kludgy, but it does the job. You can find the program code on my repository at https://github.com/gregwa1953/FCM-181_MicroThisMicroThat . Next time, we’ll add in the code that supports the LSM303 to finalize our project. Until next time, as always; stay safe, healthy, positive and creative!

issue181/micro-ci.1653890507.txt.gz · Dernière modification : 2022/05/30 08:01 de d52fr