Foosball is fun! Foosball with lights, sound effects, and active score tracking is even better!
This repository contains Arduino and Python source code to poll goal sensors, drive goal box LED lights for effect, maintain score state, and update LED score indicators.
Why Arduino and Python? The Arduino code drives the hardware - sensors and LEDs. The Python code runs on a Raspberry Pi and primarily plays sound effects but future improvements will bring score persistence, stats, and a foosball web server. Playing audio is too expensive to perform on the Arduino and its 25k usable memory doesn't cut it for most audio playback.
- Tornado foosball table (obviously any other table works too!)
- Arduino Uno R3
- Raspberry Pi (any model with a 3.5mm stereo jack works)
- Cheapo computer power supply which has both 5V and 12V rails
- Computer speakers
- Adafruit Neopixel Digital LED strips for score keeping
- Generic Analog 12V RGB LED strips for goalbox lighting and effects
- Pair of Breakbeam infared sensors for ball detection in each goalbox
- Resistors, capactitors, power MOSFETs for driving the analog LEDs, and other electronic components
The Arduino source code is located in a single sketch file - [FoosballArduino.ino] (FoosballArduino/FoosballArduino.ino)
The code runs on an Arduino Uno R3 and can be adapted for any Atmega Arduino board by changing the pinout variables.
The Arduino code polls two infared sensors, one for each goalbox. Upon detecting a trip when the ball goes in the goalbox, the goalbox analog LED strips flash and the digital LED strips increment score. A basic FSM implements this logic.
The Arduino also maintains the score and sends it through serial over USB to the Raspberry Pi for every detected goal.
The Raspberry Pi runs the Debian-based OS Raspbian with Python 2.7 installed. The e-foosball python code is located: GameManager.py
The following python packages must be installed prior to running GameManager.py
sudo apt-get update
sudo apt-get -y install python-serial
sudo apt-get -y install alsa-tools alsa-utils
sudo apt-get -y install python-alsaaudio
You may need to change a library file, see Issues section.
Once installed simply run:
python GameManager.py
The Pi opens a serial connection using PySerial and listens for game events (i.e. new game, goals) generated by the Arduino. Upon receiving a game event, the event is passed to event handlers to perform actions on the event. One event handler, SoundEventHandler
plays different sounds depending on the score.
GameManager uses pydub
library for audio playback. Unfortunately pydub's preferred backend pyaudio
has known issues running on a Raspberry Pi. I've also experienced these clipping issues and couldn't figure out a way around it. Getting pydub
to work properly involves a mega-hack. Edit library file: /usr/local/lib/python2.7/dist-packages/pydub/playback.py
and change this line:
subprocess.call([PLAYER, "-nodisp", "-autoexit", f.name])
to:
subprocess.call(["aplay", f.name])
This forces pydub to use linux utility aplay
to play audio files which works but a hack is still a hack.