This example constructs a program to roll an eight sided dice (sometimes called a D8). Why a D8? Well the blinkstick square has 8 LEDs.
The program steps are as follows: 1. initialise the blinkstick, checking one is connected, 2. generate a random number, called throw, between 1 and 8, 3. light up the required number of LEDs. Rembering that the LEDs on the blink stick are numbered 0 to 7
lets try that in python
Code
import random # we need a random number generatorfrom blinkstick import blinkstick# find the first available blinkstickbstick = blinkstick.find_first()if bstick isNone:print("The Blinkstick is not connected, either plug it in " \+" or check your USB cable supports data and power!")else:# generate a random integer in the range 1 to 8. throw = random.randrange(1, 8)# print it out so we know what we threwprint(throw)# now light that many LEDsfor led inrange(0,throw): bstick.set_color(index=led,name="red")
6
Code
bstick = blinkstick.find_first()bstick.turn_off()
1.1 diagonistics
We the program works but it leaves all the LEDs on and only runs once.
What we need is a loop which keeps going until we’ve finished and then closes down the blinkstick.
We can keep asking the user if they want to throw again, while they say yes (or y).
The steps now looks like this: 1. initialise the blinkstick 2. set again to ‘y’ 3. while again is ‘y’ 1. generate a random integer in the range 1 to 8. 2. light that many LEDs 3. ask if they want to do it again? 4. close down the blink sitck
Code
import random # we need a random number generatorfrom blinkstick import blinkstick# find the first available blinkstickbstick = blinkstick.find_first()if bstick isNone:print("The Blinkstick is not connected, either plug it in " \+" or check your USB cable supports data and power!")else: AGAIN ="y"# Initialise AGAINwhile AGAIN =="y":# generate a random integer in the range 1 to 8. throw = random.randrange(1, 8)# print it out so we know what we threwprint(throw)# now light that many LEDsfor led inrange(0,throw): bstick.set_color(index=led,name="darkblue") AGAIN =input('throw again (y/n)?')# turn off the blinkstick bstick.turn_off()
1
throw again (y/n)? y
5
throw again (y/n)? y
1
throw again (y/n)? n
1.2 problem 2
It’s still not doing quite what we want. LEDs which have been lit stay on so we can only see the highest number thrown.
Problems like this are called bugs https://en.wikipedia.org/wiki/Software_bug named in honour of the moth that created havock in an early computer.
Clearly we need to turn off the LEDs before each throw. We better also turn them off at the end.
Code
import random # we need a random number generatorfrom blinkstick import blinkstick# find the first available blinkstickbstick = blinkstick.find_first()if bstick isNone:print("The Blinkstick is not connected, either plug it in " \+" or check your USB cable supports data and power!")else: AGAIN ="y"# initialise AGAINwhile AGAIN =="y":# generate a random integer in the range 1 to 8. throw = random.randrange(1, 8)# print it out so we know what we threwprint(throw)# turn off all the LEDsfor led inrange(0,8): bstick.set_color(index=led,name="black")# now light that many LEDsfor led inrange(0,throw): bstick.set_color(index=led,name="yellow") AGAIN =input('throw again (y/n)?')# turn off all the LEDsfor led inrange(1,8): bstick.set_color(index=led,name="black")# turn off the blinkstick bstick.turn_off()
2
throw again (y/n)? y
3
throw again (y/n)? y
5
throw again (y/n)? y
7
throw again (y/n)? y
1
throw again (y/n)? n
1.3 The production version
Now we know it works we can remove the print statement and write a short function to turn everything off. The function is a useful thing as we were repeating the code to turn off all the LEDs twice (once in the loop and once at the end).
Code
"""This program sets simulates rolling an eight sided diceusing a blinkstick to display the results.It was written by David Ingram for the Programming Skills for Engineers 2course.(c)2020 University of EdinburghLicense: CC-BYIt uses three variables:throw is the number rolled on the dice,bstick is the object used to talk to the blinkstick, andagain is used to track if the user wishes to throw again."""# we need a random number generator, and the blinkstick libraryimport randomfrom blinkstick import blinkstickdef turn_off_all(stick):"this function turns off all the LEDs on the blink stick."for led_number inrange(0, 8): stick.set_color(index=led_number, name="black")# This is the MAIN programme# check a blinkstick is connected, and if it is find it.bstick = blinkstick.find_first()if bstick isNone:# No blink stick is conected so report an error.print("The Blinkstick is not connected, either plug it in " \+" or check your USB cable supports data and power!")else:# we have found a blink stick. again ="y"# Initialise AGAINwhile again =="y":# generate a random integer in the range 1 to 8. throw = random.randrange(1, 8)# turn off all the LEDs turn_off_all(bstick)# now light that many LEDsfor led inrange(0, throw): bstick.set_color(index=led, name="green")# find out if we want to throw again again =input('throw again (y/n)?')# finish up, turn off all the LEDs turn_off_all(bstick)# and then turn off the blinkstick bstick.turn_off()
throw again (y/n)? y
throw again (y/n)? y
throw again (y/n)? y
throw again (y/n)? y
throw again (y/n)? y
throw again (y/n)? y
throw again (y/n)? n