Skip to content

Lists, buttons and Raspberry Pi with the microbit

ncscomputing edited this page Feb 16, 2016 · 6 revisions

This is my effort to help :) regards WarksRaspiJam

The first program you ever get started with is the obligatory "hello world". Here is my play on the classic. Entitled....

Hello world, hello...Dave

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 = ["Dave","Fran","Beth"] #list of 3 names 

index = 0 # count of current location in the loop / list 

microbit.display.scroll("Hello World") # standard
while index <=2:
        msg = "Hello "+str(namesList[index]) # cherry on top :)
        microbit.display.scroll(msg)
        index = index +1

This is a test program

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")

Minecraft block Id and block name scroller.

This third program makes use of two lists and uses two lists to display some Minecraft block id's followed by the block name. It uses two lists and it prints out the values of each list after each loop/iteration through the list. Have a go a

Key concepts used here are:

  • Multiple lists
  • Iteration
  • Index
  • Infinite loops
  • Accessing values stored in two lists
  • Casting

Code

import microbit



BlockIdsList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]



BlockNamesList = [

"Air",

"Stone",

"Grass",

"Dirt",

"Cobblestone",

"Wood Planks",

"Sapling",

"Bedrock",

"Water",

"Water Stationary",

"Lava flowing",

"Lava stationary",

"Sand",

"Gravel",

"Gold Ore",

"Iron Ore",

"Coal Ore",

"Wood",

"Leaves",]



Count = 0



while True:

    microbit.display.scroll(str(BlockIdsList[Count]))

    microbit.sleep(1000)

    microbit.display.scroll(str(BlockNamesList[Count]))

    Count = Count +1

    

    if Count == 19:

        Count = 0

    
Clone this wiki locally