from IPython.core.display import HTML
def set_width(width):
display(HTML(f"""<style>
.container {{ width:{width}% !important;
min-width:800px !important; margin: 0 auto}}
.jp-Cell {{ width:{width}% !important;
min-width:800px !important; margin: 0 auto}} </style>"""))
# Set container width to X% of the fullscreen
set_width(50)
6C: Whack-a-mole#
Learning goal: Use the Alpaca’s digital signals to make a fun game: whack the mole. After exploring how the Alpaca’s switches work, you will build this game in steps with incrasing difficulty, to end up with a inpredictable game with anti-cheating measures.
Structure of an experiment:
Anticipe + Stimulate (5+15+0): per person. This is homework and should be finished before you start your 4 hours practicum session
Implement + Investigate (5+65): with your partner(group of 2)
Compare + Conclude: with a group of 4(per table)
BACKGROUND#
⏳ Estimated time: 5 min
Whack the mole is a game, in which moles are popping up and you have to hit them with a hammer, before they go underground again.
Because your Alpaca doesn’t have moles, nor hammers, you are going to replace them with leds (moles) and buttons (hammer). No destruction here.
You already know how to use leds, but need a background on how exacly buttons work in Alpaca. When looking at the electonic scheme of the Alpaca, you should notice that (the output of) SW1&2 are on 5V when the switch is not pushed. Once the switch is pushed a connection through ground is established, and SW1&2 become 0V. As you can see on the scheme below, the 3rd button is more complicated. You will build a bit more extended circuit to be able to use switch 3 in your experiment.

ANTICIPATE: elements you will need to program in#
⏳ Estimated time: 10 min
Your game will need to utilize leds to represent moles and buttons to represent a hammer. You should also record the reaction time.
You have three LEDs and three push buttons available as well as a buzzer.
In order to be a more efficient programmer, you need to break the problem into smaller steps.
Which steps will you take, in order to step-by-step test your software and hardware?
How can you make your game less predictable?
How to discourage cheating on the game (like pressing the button, without looking at the LED or holding all the buttons down)?
### TO DO ="your step by step approach"
SIMULATE#
⏳ Estimated time: 0 min
no simulate => the preparation does not cost that much time, but the implementation cost much more. It would be wise to already start coding parts below.
Feel free to watch the precap video from 2022, in which you’ll get an overview of all steps plus see the whack the mole demonstrated:
IMPLEMENT & INVESTIGATE 2: Build the game#
⏳ Estimated time: 10+15+15+15+10 min
Combine your plan from Anticipate and steps below to create a working game of whack-a-mole
Step 5: The End of the Game and Warning for Fraud#
⏳ Estimated time: 10 min
Signal the end of the game by making all 3 leds blink 3 times in unison.
Make sure that you cannot cheat the game by continuously pressing the button even before the LED lights up. You can resolve this be checking if the button is not pressed before you start the random wait time. If it is pressed (too early), let the buzzer sound for one second, print a warning on screen and set the measured reaction time to 5 seconds as punishment!
Test and enjoy your whack the mole game. Feel free to test whether you are faster with your left or right hand.
Remember that the buzzer is pretty loud and annoying, so don’t overdo it!
##buzzer code for you
from machine import Pin # type: ignore
import time
buzzer= Pin(14, Pin.OUT)
buzzer.value(1)
time.sleep(1)
buzzer.value(0)
import machine # type: ignore
import time
import random
from machine import Pin # type: ignore
buzzer= Pin(14, Pin.OUT)
ledA = Pin(15, Pin.OUT)
ledB = Pin(16, Pin.OUT)
ledC = Pin(17, Pin.OUT)
#ledC.value(0)
buttonA = Pin(19, Pin.IN, None)
buttonB = Pin(20, Pin.IN, None)
buttonC = Pin(21, Pin.IN, None)
reaction_time=[]
### TO DO="your code"
print('Your average reaction time is ',sum(reaction_time)/len(reaction_time))
COMPARE & CONCLUDE#
⏳ Estimated time: 5 min
Wait till all (4) group members finish their observation
Compare your results with your other group members.
If your results agree, and are in line with all predictions, then talk to a TA and get checked off
Otherwise, so if your results do not agree, or your results are not in line with your predictions, then first discuss amongst your group before getting a TA.
to be checked off by a TA:
let the TA enjoy your game (or you playing the game), reflect how all the features are implemented
exit card:
Write a brief abstract on what you learned (conclusion, useful graph),
Which troubleshooting skills do you want to remember for next sessions,
Which code do you copy for use in next sessions,
How do you think this notebook could be improved?
#6C Whack a mole
### TO DO="1. reflect: how did you manage to get implement all required features?"
### TO DO="2a. abstract"
### TO DO="2b. troubleshooting"
### TO DO="2c. code"
### TO DO="3. what changes would you suggest?"
%rebootdevice
%disconnect
