Micro:bit controls Neopixels

micro:bit, Python3.X

How to set up the hardware

Please follow the wiring shown on the photo below. There are two connections to the micro:bit, one is on the GND connector (the yellow cable on the picture), the other is on PIN0 (the white cable on the picture).

The Neopixels get their power from a battery pack, as they may draw too much current to be run via the USB interface and the micro:bit. The battery holder is made for 4 batteries, as we can only use 3, the missing battery is replaced with a wire (the green cable). The 3 batteries supply 4.5V, this is sufficient and safe.

IMG_1373

Here are some code snippets to upload, try out and modify to your liking.

You need to start a web browser and go to micro:bit to copy and paste the code below. Then press “Download” to create an executable file with the extension .hex. You find this file in your download folder. Next connect the micro:bit to an USB port and make sure that you see the micro:bit as a drive in the File Explorer. Then drag and drop the file onto the micro:bit drive icon. The yellow LED on the micro:bit will start to flash. If the program is correct, the program will start to run once the yellow LED lights up steadily. Otherwise you will get error messages with line numbers on the LED matrix of the micro:bit. Once you have fixed the errors, download again.

This first example changes the colour of the Neopixels one after another and leaves each pixel on until it is refreshed in a different colour.

"""
Repeatedly displays random colours onto the Neopixels.
Works for Neopixel strips of 30.
"""

from microbit import *
from neopixel import NeoPixel
from random import randint

# Setup the Neopixel strip on pin0 with a length of 30 pixels
np = NeoPixel(pin0, 30)
while True:
#Iterate over each LED in the strip
for pixel_id in range(0, len(np)):
red = randint(0, 60)
green = randint(0, 60)
blue = randint(0, 60)
# Assign the current LED a random red, green and blue value between 0 and 60
np[pixel_id] = (red, green, blue)
# Display the current pixel data on the Neopixel strip
np.show()
sleep(100)

Here is another example with a different lighting pattern:


"""
Adafruit example, modified to run with 30 LED strip.
see at
https://learn.adafruit.com/pages/11472/elements/2981640/download
for the basic version with one running neopixel
180815 Modified the original version, but not yet debugged.
181001 Debugged and running!
"""

from microbit import *
from neopixel import NeoPixel

num_pixels = 30
foreground = [255, 0, 0] # Int color - red, green and blue
foreground2 = [0, 255, 0]
background = [16, 16, 16]

ring = NeoPixel(pin0, num_pixels)

while True:
# red/green dot circles around a white background (for 30 neopixel strip)
for i in range(0, num_pixels-1):
ring[i] = foreground # set pixel i to foreground
ring[i+1] = foreground2
ring.show() # actually display it
sleep(50) # milliseconds
ring[i] = background # set pixel to background before moving on
ring[i+1] = background
ring.show()
# implement the closing of the circle for last and first element.
ring[num_pixels-1] = foreground
ring[0] = foreground2
sleep(50)
ring[num_pixels-1] = background
ring[0] = background

ATTENTION: This code snippet uses the concept of a block of code. The lines in a block of code belong together, here they are inside loops starting with while and for …  Everything inside the loop is repeated. In order for the interpreter to see where the blocks begin and end, each line inside is indented by four/eight spaces.

When copying the code over to your editor, please make sure that these lines remain indented.

The micro:bit

Blocks, micro:bit, Scratch2.0

The micro:bit is a tiny computer that you can program with blocks and also with other programming languages. It has two buttons, 25 LEDS, and some more sensors built in, you can add more sensors (to pick up and measure events and data) and activators (lamps, other items to do something). If you are fancy to see more, have a look here on this website, there are more projects! Today you can create a small Rock, Paper, Scissor game, follow the link below.

190918 GOLForMicroBit.jpg

Rock Paper Scissors Activity

How to use the micro:bit?

First, connect your micro:bit with the USB cable to the computer. Then you launch the editor  and write your code. At the end you need to compile (translate) the code into something the processor on the micro:bit can understand and download it. When the download is finished (a red LED on the micro:bit will stop blinking) the program will start automatically. You’re done, congratulations!

Micro:bit – Accelerometer, the Game Blocks and a Catch Game Part I

Blocks, Game, micro:bit, Uncategorized

For this game we need to use the accelerometer to sense the orientation of the micro:bit. This sensor is comprehensively described in the documentation. You find it under “Reference”.

mb_Accelero

Let’s have a look at one sensor, we choose the accelerometer. It measures acceleration, caused by gravity and sudden movements. Here is a sketch explaing about the three coordinates it uses to put the acceleration into numbers. You can measure the acceleration in on direction (x, y, z) at a time. The values change between -1023 and 1023.

mb_coordSys

Here is the link accelerometer so that you can have a look.

The documentation is interactive, so you can also upload a sketch to the IDE and run it there. Furthermore, you find examples that you can also easily transfer to the editor, no need to rebuild the projects.

Get to know sensors and how to program them.

Get the examples running on the simulator and your micro:bit. As a next step find the documentation for the magnetic sensor and implement a compass.

Game module

In the micro:bit editor under Advanced you find the category “Game”. It contains blocks to make small games.

The small display of the micro:bit contains 5 x 5 LEDs. In the game module you can address each LED with its coordinates (x, y). You may know coordinate systems from school, they have their origin (0,0) always at the lower left corner. In computer science we normally put the origin at the upper left corner.

mb_coordSysLED

Create your first game

Following you find the beginnings of a small game. At the beginning of the game a LED will light up at a random location. Then the player needs to move the other LED to that location using the buttons A and B. When he reaches the final position, the display shows “Win!”. The button A is already coded, try to understand what is happening when you press the button. Then finish the game by coding the functionality for button B.

181002_Catch1


let dir_x = 0
let sprite2: game.LedSprite = null
let sprite1: game.LedSprite = null

sprite1 = game.createSprite(2, 2)
sprite2 = game.createSprite(Math.random(5), Math.random(5))
dir_x = 1

basic.forever(() => {
if (sprite1.isTouching(sprite2)) {
basic.showString("Win!")
}
})

input.onButtonPressed(Button.A, () => {
if (sprite1.isTouchingEdge()) {
dir_x = dir_x * -1
}
sprite1.change(LedSpriteProperty.X, dir_x)
})

input.onButtonPressed(Button.B, () => {
})

Above is the code in Javascript, open the micro:bit Javascript editor, select “{}Javascript” and then copy and paste the code into the editor. Next, select “Blocks” and the code will be converted into the Block language. Here you can now

  • try out the program,
  • figure out what is happening when you press  Button A and
  • then add the missing functionality for button B.

Micro:bit – First experiments With a Game I

Blocks, micro:bit

In the future you will find here links to external and internal materials that help you to get faster up to the interesting activities.

For today I only post here one link, that is for the additional micro:bit project you may be able to do today.

You can create a simple Truth or Dare game with the micro:bit. When pressing the button A, the micro:bit will calculate a random number between 0 and 4. If the micro:bit chooses a 0, you will see “Truth” displayed on the screen, otherwise “Dare”.

180919 TruthOrDareScreenS

Truth or Dare with micro:bit

This small project has already all ingredients of a real programm, it uses a variable, a loop, a conditional if statement and works at least in parts sequentially. We will discuss those elements further next week.

Enjoy