Using a 4×4 keypad and a 16×2 LCD screen, can you construct a simple calculator?
Step 1: Connect up the hardware. Review the notes on keypads and LCD screens, make sure you understand how they work, connect them up and run some test programs. Both should be able to connect to pins 19 and 20 (the I2C pins) and operate independently.
Step 2: Can you make what you type appear on the screen, one character at a time? What about using * to clear the screen?
Step 3: Start with a very simple calculator that will accept a single digit, use A-D for +,-,x and ÷, then calculate on the second digit pressed. Show the input on the top line and the answer on the second. Tip, you will find the function int(string) useful for converting strings to integers.
Step 4: Make the calculator more advanced. Allow you to put in multiple digit numbers, then use the # key for an =. You will need to break down the string into variables for each part. There is a list of string functions at w3schools. What might be useful is the split() function. Remember you can make a note of which operator is used as it is pressed. This avoids having to search the string for operators, or you could use the find() function.
Can we control a servo with a rotary encoder? Can we make it turn in the same direction as we turn the rotary encoder? What could we attach to the servo arm? Perhaps use this to control a crane or fork lift? Can it be attached to some lego to make something interesting?
Step 1: Set up the rotary encoder. Review Micro:bit Rotary Encoder and connect up the hardware. Run the test code to make sure the encoder is working.
Step 2: Can you turn this code into a function? Sit in the loop and call a function checkEncoder(), which will report back the direction (if any) it is being turned. Perhaps return 0 for stationary, 1 for turning clockwise and 2 for turning anti-clockwise. Consider returning two values to also report the state of the button.
Step 4: Connect the servo. Follow Micro:bit SG09 Servo and test the servo. Note, both examples use pin0 so you will have to make adjustments.
Step 5: Can you turn the servo in the same direction as the rotary encoder? You may need to write a function to tell the servo to move by X number of degrees. Try playing with different values for X and different sensitivities to control how fast the encoder turns the servo. Don’t forget to make sure you do not try to turn the servo beyond it’s limits.
Given what we have learned about controlling the joystick and neopixel hardware in Micro:bit Joystick Module and Micro:bit Neopixels, can we construct a simple game? We need to consider the hardware, animation, random position generation and collision detection.
Step 1: Construct the hardware. Can you connect both the Neopixel grid and the joystick to the micro:bit? Is there any testing we can do to make sure they work?
Step 2: Start the game by positioning the ‘player’ in the top left corner. Pick a colour for the player and light up that LED. Be sure to use variables to describe the player position as we are going to move this soon.
Step 3: Can we move the player around with the joystick? In a loop detect joystick movement then adjust the coordinates of the players position accordingly. Be sure to put a delay in the loop, otherwise the player will move very fast! What happens when the player reaches the edge of the display? Can we stop the player leaving the screen?
Step 4: Can you make something happen to the player when you press the joystick down? Perhaps flash?
Step 5: Using a random number generator, can you put some ‘food’ at a random point on the screen? How can we check this food does not land on top of the player? Do we need to store the food coordinates for when we redraw the screen?
Step 6: Now lets look at collision detection. Can you detect when the player lands on the food? Can it ‘eat’ the food and then new food appears at another random location? Can you keep a score of food eaten on the micro:bit LEDs? End the game when the you reach 10 bits of food.
Step 7: Can you make the game more interesting or use what you have learned to make a different game? What if there were two players taking it in turn to move? What if the food made the player grow like a worm? Could painting the screen a different colour be part of the game?