Built-in project · Arduino Uno R3

Catch the beans: a tilt game on an 8x8 matrix, on an Arduino Uno

Tip an MPU-6050 to slide a cup along a MAX7219 matrix (DIN 13, CLK 7, CS 8) and catch the falling beans. An RGB LED on 6, 9 and 5 shows your lives, a buzzer on 3 scores. The whole build, an Arduino Uno and 7 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $38. On Mokxi they're free.

Catch the beans: a tilt game on an 8x8 matrix · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Catch the beans: tip the board to slide a cup along the bottom of an 8x8
// matrix and catch the beans that fall from the top.
//
//   MAX7219 DIN -> pin 13    CLK -> pin 7    CS -> pin 8
//   MPU-6050 (GY-521) SDA -> A4    SCL -> A5
//   RGB LED red -> pin 6, green -> pin 9, blue -> pin 5, each through 220 ohm
//   passive buzzer -> pin 3
//
// The cup is three dots wide on the bottom row with walls one row up. A bean
// counts as caught when it drops into the middle of the cup. The RGB LED
// shows your lives: green, then yellow after one miss, red after two, and the
// third miss ends the game. Catch ten to win. Tip the board hard forward or
// back to start again.
//
// Drag the module's pitch to move the cup, and its roll to start a new game.

#include <Wire.h>
#include <LedControl.h>

const int MPU = 0x68;
const int LEAN = 5000;
const int RED_PIN = 6;
const int GREEN_PIN = 9;
const int BLUE_PIN = 5;
const int BUZZER = 3;
const int GOAL = 10;

LedControl lc = LedControl(13, 7, 8, 1);  // DIN, CLK, CS, modules

int cup = 3;       // the cup's middle column, 1 to 6
int beanX = 3;
int beanY = 0;
int caught = 0;
int missed = 0;
bool playing = true;
unsigned long lastFall = 0;
unsigned long lastMove = 0;

void readTilt(int &ax, int &ay) {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 4);
  ax = Wire.read() << 8 | Wire.read();
  ay = Wire.read() << 8 | Wire.read();
}

void color(int r, int g, int b) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
}

void showLives() {
  if (missed == 0) color(0, 160, 0);
  else if (missed == 1) color(160, 120, 0);
  else color(160, 0, 0);
}

void newBean() {
  beanX = random(1, 7);
  beanY = 0;
}

void draw() {
  lc.clearDisplay(0);
  lc.setLed(0, 7, cup - 1, true);
  lc.setLed(0, 7, cup, true);
  lc.setLed(0, 7, cup + 1, true);
  lc.setLed(0, 6, cup - 1, true);
  lc.setLed(0, 6, cup + 1, true);
  lc.setLed(0, beanY, beanX, true);
}

void picture(const byte rows[8]) {
  for (int row = 0; row < 8; row++) lc.setRow(0, row, rows[row]);
}

const byte SMILE[8] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
const byte CROSS[8] = {0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81};

void newGame() {
  caught = 0;
  missed = 0;
  cup = 3;
  playing = true;
  showLives();
  newBean();
  Serial.println("new game");
}

void endGame(bool won) {
  playing = false;
  picture(won ? SMILE : CROSS);
  color(0, 0, 0);
  tone(BUZZER, won ? 1047 : 131, 600);
  Serial.println(won ? "you win" : "game over");
}

void land() {
  if (beanX == cup) {
    caught++;
    tone(BUZZER, 880, 120);
    Serial.print("caught ");
    Serial.println(caught);
    if (caught >= GOAL) {
      endGame(true);
      return;
    }
  } else {
    missed++;
    tone(BUZZER, 110, 300);
    Serial.print("missed ");
    Serial.println(missed);
    showLives();
    if (missed >= 3) {
      endGame(false);
      return;
    }
  }
  newBean();
}

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // wake the chip
  Wire.write(0);
  Wire.endTransmission();
  lc.shutdown(0, false);
  lc.setIntensity(0, 6);
  randomSeed(analogRead(A0));
  Serial.println("Mokxi Uno: catch the beans. Tip the board to move the cup.");
  newGame();
}

void loop() {
  int ax, ay;
  readTilt(ax, ay);
  if (!playing) {
    if (abs(ay) > 3 * LEAN) newGame();
    delay(50);
    return;
  }
  if (millis() - lastMove >= 150) {
    int was = cup;
    if (ax > LEAN && cup > 1) cup--;
    if (ax < -LEAN && cup < 6) cup++;
    if (cup != was) {
      lastMove = millis();
      Serial.print("cup at ");
      Serial.println(cup);
    }
  }
  if (millis() - lastFall >= 300) {
    lastFall = millis();
    beanY++;
    if (beanY >= 6) land();
  }
  if (playing) draw();
  delay(20);
}

Parts list

9 parts, plus the jumper wires. They retail for about $38; on Mokxi they're free.

How it is wired

14 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 13; LED matrix, 8x8 pin DIN
  • Arduino Uno R3 pin 9; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 8; LED matrix, 8x8 pin CS
  • Arduino Uno R3 pin 7; LED matrix, 8x8 pin CLK
  • Arduino Uno R3 pin 6; Resistor, 220 Ω (3) pin 1
  • Arduino Uno R3 pin 5; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 3; Piezo speaker pin 1
  • Arduino Uno R3 pin 5V; LED matrix, 8x8 pin VCC; Accelerometer and gyroscope, MPU6050 pin VCC
  • Ground: Arduino Uno R3 pin GND; RGB LED pin C; LED matrix, 8x8 pin GND; Accelerometer and gyroscope, MPU6050 pin GND; Piezo speaker pin 2
  • Arduino Uno R3 pin A4; Accelerometer and gyroscope, MPU6050 pin SDA
  • Arduino Uno R3 pin A5; Accelerometer and gyroscope, MPU6050 pin SCL
  • RGB LED pin R; Resistor, 220 Ω (3) pin 2
  • RGB LED pin G; Resistor, 220 Ω (2) pin 2
  • RGB LED pin B; Resistor, 220 Ω (1) pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.