This is the last one :)
Last summer I created some resources to use with Micro:bit/s for CPC and over the next few months and I will try and share the best bits with the kind permission of CPC.
You can go and buy a Micro:bit project kit from here: http://cpc.farnell.com/bbc-micro-bit-kits
Here are a series of basic micro python challenges to try out on the Micro:bit, enjoy!
Challenge 1 Hello world, hello mum!
This does the basic hello world and says a hello to a few more people along the way using a basic list data structure to store the names, try it out:
Key concepts used here are:
- Lists
- Iteration
- Index
- Count controlled loops
- Accessing values stored in a list
- joining together string values
- Casting
Code
import microbit
namesList = ["Nanny June","Daddy","Mum"] #list of 3 names
index = 0
microbit.display.scroll("Hello World") #message
while index <=2:
msg = "Hello "+str(namesList[index])
microbit.display.scroll(msg)
index = index +1
Challenge 2 using buttons
This challenge is a basic example of getting the Micro:bit to respond to buttons ‘A’ and ‘B’ being pressed. The second example uses the 'A' and 'B' buttons to print out messages to screen.
Testing out using the buttons try this code by creating a test program:
Code:
import microbit
while True:
if microbit.button_a.is_pressed():
microbit.display.scroll("This is a ...")
if microbit.button_b.is_pressed():
microbit.display.scroll("....test program")
Challenge 3 Random Name scroller
This third program makes use of two lists and uses two lists to display a random name made up of a randomly selected first name then a randomly selected second name.
Key concepts used here are:
- Multiple lists
- Infinite loops
- Accessing values stored in two lists
Code
import microbit
import random
RandomFirstName = ["Steve","Shannon","Jenny"]
RandomSecondName = ["Debank","Green","Penny","Smith"]
while True:
microbit.display.scroll(random.choice(RandomFirstName)
microbit.sleep(1000)#wait 1 sec
microbit.display.scroll(random.choice(RandomSecondName))
microbit.sleep(1000)#wait 1 sec
Challenge 4 Shake it random nickname generator
The fourth example is a random nickname generator based on adjectives for body type and random names It uses the majority of the coding concepts previously looked at except it introduces the shake.
Code:
from microbit import *
import random
NameList = ["Paul","Dave","Gem","Rachel","Chris"]
BodyTypeAdjectiveList = ["Sturdy","Bullnecked","Gangling","Heavyset","Lanky","Musclebound"]
while True:
if accelerometer.was_gesture('shake'):# if shaken then
RandomNickname = random.choice(BodyTypeAdjectiveList)+" "+random.choice(NameList)
display.scroll(RandomNickname)
Challenge 5 shake it, dice roller
This simple dice simulation introduces the accelerometer and how to use it to randomly simulate a 6 sided dice.
Code
from microbit import *
import random
DiceNumbers = [1,2,3,4,5,6]#List of 6 possible numbers
while True:
if accelerometer.was_gesture('shake'):# if shaken then
msg = "You rolled.. "+str(random.choice(DiceNumbers))
display.scroll(msg)#show the text
Challenge 6 Shake the bit, display a random picture
This basically allows you to shake the microbit and this will randomly display one of the library of images. N.B. I have only implemented a few to give a brief idea :)
Concepts covered:
- Lists
- Functions
- Loops
- Accelerometer
- Random library
- Conditional statements
Code
from microbit import *
import random
"""
List of possible pictures not sure if it is exhaustive, stored as string so they can be stored in a list
this process is called casting.
"""
PicNamesList = [str(Image.SAD),str(Image.HEART),str(Image.MEH),str(Image.RABBIT),str(Image.COW)]
"""
I have created a function that groups the code and makes the final program much cleaner
it basically:
*imports the list as a parameter
*creates a temp variable which stores the string representation of the image randomly selected
*then uses if and elif statements to check which image it should display on microbit
"""
def checkWhichImageIAm(PicNamesList):
chosenImage = random.choice(PicNamesList)
if chosenImage == str(Image.SAD):
display.show(Image.SAD)
elif chosenImage == str(Image.HEART):
display.show(Image.HEART)
elif chosenImage == str(Image.MEH):
display.show(Image.MEH)
elif chosenImage == str(Image.RABBIT):
display.show(Image.RABBIT)
elif chosenImage == str(Image.COW):
display.show(Image.COW)
while True:
if accelerometer.was_gesture('shake'):# if shaken then
checkWhichImageIAm(PicNamesList)# run check which image am I function
That's it!
If you look further on the blog you will find a lot of new content http://warksjammy.blogspot.com/2017/07/blog-1-getting-started-with-bitio.html
ReplyDelete