Welcome to our third blog post, in a series of five, in celebration of National Coding Week. If you haven’t already, take a look here to find out more about what we’re doing, and why we’re doing it.
The next part of our game is going to be a letter spinner. The aim of this project is to randomly choose a letter of the alphabet, spread around, by way of a large spinning arrow. We will borrow from the foundations of our Probability Spinner Project and tweak it.
For this you will need:
- a Crumble Controller
- a battery box with batteries (not rechargeable)
- crocodile leads
- a push switch
- a motor
- a pulley/wheel
For starters, we are going to connect together the components. Connect the +ve and -ve on the battery box to the respective connections on the Crumble (left hand side). Make sure to keep the batteries switched off for the moment.
Now we are going to connect a motor, with the pulley attached. You can use either motor connection on the Crumble, but we’re using motor 2. Connect the red lead of the motor, to the + on the motor pad, and the black to the – .
Getting our motor to move is simple, as is getting it to stop, but we want to make it spin for a random amount of time. To do this, we can use the ‘random _ to _’ block; a common occurrence throughout this blog series. When the Crumble receives power, we will give ourselves 2 seconds to move our hand away, then we will turn the motor forwards. We will wait a number of milliseconds, between 3000 and 9000, and then stop the motor.
To make the spinner easier to use, we can use a push switch to trigger the spinner. This connects to a + connection, and an I/O pad (A, B, C or D).
As with our previous posts we are going to ‘pause’ the program and wait until the input (D) is HI’. Once the switch is pressed, the program continues and our spinner code will run. All of this will then be inside a loop, to allow for infinite letter choosing! Or until the batteries run out, or we get bored.
Although this spinner is perfectly adequate, to make it more realistic, we want it to slow down gradually. We are going to add a variable called ‘Speed’, which will start at 100 (%). When the spinner is due to stop, we will run a loop which subtracts varying amounts from ‘Speed’, bringing the motor to a gradual-ish stop.
Hint: we use the condition ‘if speed < 1’ as the speed value may not equal 0. If we didn’t, the value could become negative and the motor would begin speeding up in the other direction!
And there we have a our letter spinner ready to rock!