Dance Mat
Introduction
You can use the inputs on CodeBug with the tethered mode to control your computer. Using PyDance you can then build your own arcade dance game with simple foil and foam mats.
Make sure you have followed the ‘Tethering CodeBug With Python’ guide before trying this activity.
Note: You need your foam to be reasonably spongy and about 1cm thick. You can cut up plastic film folders to use for the plastic sheets.
CodeBug | ||||
Raspberry Pi | ||||
Micro USB cable | ||||
Double-sided tape | ||||
Single-sided tape | ||||
Plastic sheets | ||||
Foam sheets | ||||
Kitchen foil | ||||
Wires |
Making your mats
Print out the templates and cut them out. Use your plastic sheet template to cut 8 pieces from your plastic sheets.
Cut 8 pieces of wire of 15cm in length. Remove 1cm of the plastic covering from both ends of the wires.
Wrap foil around one end of each piece of wire. Stick the foiled end of the wire to the edge of a square (7m x 7cm) of foil. Flip the foil square over and using double-sided tape, stick it in the centre of the plastic cut-out (with the wire coming out by the extended part of the plastic sheet). Stick the wire to the plastic sheet with a couple of pieces of tape. Do this for every plastic sheet.
Using the foam template, cut your foam into four 10cm x 10cm squares, with a 2cm hole in the center of the square (as on template sheet).
Sandwich each piece of foam between two of your plastic sheets (with the foil touching the foam), making sure the foam completely covers the foil (except for the hole in the foam). Stick the edges of the 2 plastic sheets together with a strong glue or single-sided tape.
Test your mats with a battery and an LED, by using the mat as a switch, pressing down on the mat should light up the LED.
You can print and cut out the arrows template to make your mats look great. Have the arrows facing away from the wires of the mat.
Installing PyDance
First update your Raspberry Pi’s software in a Terminal:
sudo apt-get update
sudo apt-get upgrade
Install PyGame onto your Raspberry Pi:
sudo apt-get install python3-pygame
Download the PyDance program files:
wget https://icculus.org/pyddr/pydance-1.1.0.zip
Unzip the files:
unzip pydance-1.1.0.zip
Move into the PyDance directory:
cd pydance-1.1.0.zip
Install the library and a dependency:
python setup.py install
sudo apt-get install gettext
You will need to use super user permissions for the next part, so change the super password:
sudo passwd root
Type raspberry each time.
Now make the install using the superuser password you just created:
su -c 'make install'
Installing Songs
Move into the PyDance user directory:
cd ~/.pydance
Make a songs folder and move into it:
mkdir songs
cd songs
Download a test song (music and steps) into this directory:
wget http://icculus.org/pyddr/forkbomb.ogg
wget http://icculus.org/pyddr/forkbomb.dance
Look here for more songs to download.
Testing PyDance
It is best to test that PyDance is working correctly at this point, so start the graphical environement:
startx
Open a Terminal and start PyDance:
cd ~/pydance-1.1.0
./pydance
Click the Play Game button (you will need to use the arrow and enter keys on your keyboard).
By pressing enter, select "4 panel" then “single” then “normal” then “Forkbomb”.
Test PyDance is working by pressing the arrow keys on your keyboard, the corresponding arrow on the screen will turn white (or green if you hit it when a blue step is on it).
Writing the CodeBug controller program
To interface with PyDance we need to write a tethered CodeBug Python program.
We need to install a Python module that will allow us to generate the arrow keyboard strokes that you need to play PyDance, but first we need to install Pip to be able to install the modules::
sudo apt-get install python3-pip
sudo pip-3.2 install python3-xlib
sudo pip-3.2 install PyUserInput
Create a python program called codebug_dance.py or download it here:
nano codebug_dance.py
And fill it with the code below.
from pykeyboard import PyKeyboard
import time
keyboard = PyKeyboard()
import codebug_tether
cb = codebug_tether.CodeBug()
keys = [ 'Up', 'Right', 'Left', 'Down']
pressed = [ False, False, False, False]
while True:
for i in range(4):
if not cb.get_input(i):
if not pressed[i]:
keyboard.tap_key(keys[i])
pressed[i] = True
time.sleep(.01)
else:
pressed[i] = False
This code checks if a CodeBug input has been connected to Ground and generates one key press each time the input is pressed.
Connecting up your mats
Connect a wire from each mat to the GND leg on CodeBug. Then connect the other wire of each mat to one of the numbered legs on CodeBug.
Running CodeBug PyDance
In one window start up the CodeBug program:
python3 codebug_dance.py
In another window startup PyDance:
cd pydance-1.1.0
./pydance
You can make PyDance fullscreen by pressing "F11" on your keyboard.
Test your CodeBug dance game is working by stepping on the pads, the corresponding arrow on the screen will turn white (or green if you hit it when a blue step is on it).
What next?
Install more songs and challenge your friends. You could even make another dance mat, set up another CodeBug and go head-to-head dancing at the same time.
You could find other uses for your mats, such as foot-controlled keyboard shortcuts! Change the "keys" list in the CodeBug Python code to achieve this.
Checkout the CodeBug Photographer with Raspberry Pi activity for instructions on how to make a pressure sensitive mat that you could use to trigger your camera.