Tilt maze on an Arduino Uno
Tilt an ADXL335 and a dot rolls around an 8x8 LED matrix, through a little maze to the blinking goal, then the maze changes. The whole build, an Arduino Uno and 2 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.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Tilt maze: an ADXL335 tips a dot around an 8x8 LED matrix on a MAX7219.
//
// A0, A1 the ADXL335's X and Y (GY-61 board; VCC and GND from the rails)
// pin 12 the matrix's DIN
// pin 11 the matrix's CLK
// pin 10 the matrix's CS
//
// The GY-61 runs the chip on 3.3 V, and the ADXL335's outputs are
// ratiometric: 0 g is half of 3.3 V, 1.65 V, which a 5 V Uno reads as about
// 338, and 1 g is a tenth of 3.3 V either side, about 68 counts. Tilted more
// than a quarter of a g (17 counts) the dot rolls one cell that way every
// 150 ms, unless a wall is in the way. Reach the blinking goal and the next
// maze appears.
#include <LedControl.h>
const int AXIS_X = A0;
const int AXIS_Y = A1;
const int ZERO_G = 338;
const int QUARTER_G = 17;
LedControl lc(12, 11, 10, 1);
// Each maze is eight rows, bit 7 the left column; a 1 is a wall.
const byte MAZES[2][8] = {
{0b00000000, 0b01111010, 0b01000010, 0b01011110, 0b01010000, 0b01010110, 0b00010000, 0b11111110},
{0b00000000, 0b11101110, 0b00101000, 0b10101011, 0b10100000, 0b10111110, 0b10000000, 0b10111110},
};
const int GOALS[2][2] = {{7, 7}, {7, 6}};
int level = 0;
int px = 0, py = 0;
bool blink = false;
int moves = 0;
bool wall(int x, int y) {
if (x < 0 || x > 7 || y < 0 || y > 7) return true;
return (MAZES[level][y] >> (7 - x)) & 1;
}
void draw() {
for (int y = 0; y < 8; y++) {
byte row = MAZES[level][y];
if (y == py) row |= (byte)(0x80 >> px);
if (y == GOALS[level][1] && blink) row |= (byte)(0x80 >> GOALS[level][0]);
lc.setRow(0, y, row);
}
}
void startLevel() {
px = 0;
py = 0;
Serial.print("tilt maze: level ");
Serial.println(level + 1);
draw();
}
int direction(int counts) {
if (counts > ZERO_G + QUARTER_G) return 1;
if (counts < ZERO_G - QUARTER_G) return -1;
return 0;
}
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: tilt maze");
lc.shutdown(0, false);
lc.setIntensity(0, 6);
lc.clearDisplay(0);
startLevel();
}
void loop() {
int dx = direction(analogRead(AXIS_X));
int dy = direction(analogRead(AXIS_Y));
bool moved = false;
if (dx != 0 && !wall(px + dx, py)) {
px += dx;
moved = true;
}
if (dy != 0 && !wall(px, py + dy)) {
py += dy;
moved = true;
}
if (moved) {
moves++;
Serial.print("dot at ");
Serial.print(px);
Serial.print(",");
Serial.println(py);
}
if (px == GOALS[level][0] && py == GOALS[level][1]) {
Serial.print("goal! in ");
Serial.print(moves);
Serial.println(" moves");
moves = 0;
level = (level + 1) % 2;
startLevel();
}
blink = !blink;
draw();
delay(150);
}
Parts list
4 parts, plus the jumper wires. They retail for about $38; on Mokxi they're free.
How it is wired
7 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 12; LED matrix, 8x8 pin DIN
- Arduino Uno R3 pin 11; LED matrix, 8x8 pin CLK
- Arduino Uno R3 pin 10; LED matrix, 8x8 pin CS
- Arduino Uno R3 pin 5V; Accelerometer, ADXL335 analog pin VCC; LED matrix, 8x8 pin VCC
- Ground: Arduino Uno R3 pin GND; Accelerometer, ADXL335 analog pin GND; LED matrix, 8x8 pin GND
- Arduino Uno R3 pin A0; Accelerometer, ADXL335 analog pin X
- Arduino Uno R3 pin A1; Accelerometer, ADXL335 analog pin Y
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.