
What's on the bench
- Arduino Uno R4
- Joystick
How it's wired
- Arduino Uno R4 · gnd2→Joystick · gnd
- Arduino Uno R4 · 5v→Joystick · vcc
- Arduino Uno R4 · a0→Joystick · vrx
- Arduino Uno R4 · a1→Joystick · vry
- Arduino Uno R4 · d2→Joystick · sw
The code
This Arduino C++ sketch lives in Code; sign in and press Compile & upload to build real firmware for the emulated board.
// SPDX-License-Identifier: MIT // Snake on the UNO R4 WiFi's 12x8 LED matrix, steered by a joystick. // Wiring: VRx -> A0, VRy -> A1, SW -> D2, +5V -> 5V, GND -> GND. // Push the stick to turn and eat the blinking dot to grow. The snake wraps // around the edges; the game ends when it bites itself. The matrix then // shows your score. Press the cap or push the stick to play again. #include <Arduino_LED_Matrix.h> ArduinoLEDMatrix matrix; const uint8_t ROWS = 8, COLS = 12, CELLS = ROWS * COLS; const int JOY_X = A0, JOY_Y = A1, JOY_SW = 2; const int DEAD_ZONE = 200; // ADC counts either side of the 512 centre const unsigned long START_STEP_MS = 220, MIN_STEP_MS = 100, BLINK_MS = 150; // 3x5 digits for the score screen, one row per byte, MSB on the left. const uint8_t DIGITS[10][5] = { {7, 5, 5, 5, 7}, {2, 6, 2, 2, 7}, {7, 1, 7, 4, 7}, {7, 1, 7, 1, 7}, {5, 5, 7, 1, 1}, {7, 4, 7, 1, 7}, {7, 4, 7, 5, 7}, {7, 1, 2, 2, 2}, {7, 5, 7, 5, 7}, {7, 5, 7, 1, 7}, }; uint8_t frame[ROWS][COLS]; bool occupied[CELLS]; uint8_t body[CELLS]; // ring buffer of cell indices; body[head] is the head uint8_t head, length, food, score; int8_t dirRow, dirCol, wantRow, wantCol; bool playing, foodVisible, stickCentred; unsigned long lastStep, lastBlink, overAt, stepMs; uint32_t seed = 0x2545F491; uint8_t cellAt(uint8_t row, uint8_t col) { return row * COLS + col; } uint32_t nextRandom() { seed ^= seed << 13; seed ^= seed >> 17; seed ^= seed << 5; return seed; } void placeFood() { // Pick the n-th free cell so every free cell is equally likely. seed ^= millis(); uint8_t n = nextRandom() % (CELLS - length); for (uint8_t cell = 0; cell < CELLS; cell++) { if (occupied[cell]) continue; if (n-- == 0) { food = cell; return; } } } void draw() { memset(frame, 0, sizeof(frame)); for (uint8_t i = 0; i < length; i++) { uint8_t cell = body[(head + CELLS - i) % CELLS]; frame[cell / COLS][cell % COLS] = 1; } if (foodVisible) frame[food / COLS][food % COLS] = 1; matrix.renderBitmap(frame, ROWS, COLS); } void drawDigit(uint8_t digit, uint8_t left) { for (uint8_t row = 0; row < 5; row++) { for (uint8_t col = 0; col < 3; col++) { frame[row + 1][left + col] = (DIGITS[digit][row] >> (2 - col)) & 1; } } } void drawScore() { memset(frame, 0, sizeof(frame)); if (score >= 10) { drawDigit(score / 10, 2); drawDigit(score % 10, 7); } else { drawDigit(score, 4); } matrix.renderBitmap(frame, ROWS, COLS); } void newGame() { memset(occupied, 0, sizeof(occupied)); length = 3; head = 2; for (uint8_t i = 0; i < length; i++) { body[i] = cellAt(3, 3 + i); occupied[body[i]] = true; } dirRow = wantRow = 0; dirCol = wantCol = 1; score = 0; food = cellAt(3, 9); // The first dot sits straight ahead. foodVisible = true; stepMs = START_STEP_MS; lastStep = lastBlink = millis(); playing = true; Serial.println("Snake: steer with the joystick"); draw(); } // Returns true while the stick is pushed past the dead zone. bool readStick() { int x = analogRead(JOY_X) - 512; int y = analogRead(JOY_Y) - 512; if (abs(x) < DEAD_ZONE && abs(y) < DEAD_ZONE) return false; if (abs(x) >= abs(y)) { wantRow = 0; wantCol = x > 0 ? 1 : -1; } else { wantCol = 0; wantRow = y > 0 ? -1 : 1; // Pushing up raises VRy; row 0 is the top. } return true; } void gameOver() { playing = false; Serial.print("Game over: score "); Serial.println(score); foodVisible = false; for (uint8_t flash = 0; flash < 3; flash++) { matrix.clear(); delay(150); draw(); delay(150); } drawScore(); overAt = millis(); stickCentred = false; } void step() { // A U-turn would bite the neck, so it is ignored. if (wantRow != -dirRow || wantCol != -dirCol) { dirRow = wantRow; dirCol = wantCol; } uint8_t from = body[head]; uint8_t row = (from / COLS + ROWS + dirRow) % ROWS; uint8_t col = (from % COLS + COLS + dirCol) % COLS; uint8_t cell = cellAt(row, col); bool eating = cell == food; // The tail moves out of the way this step unless the snake grows. if (!eating) occupied[body[(head + CELLS - (length - 1)) % CELLS]] = false; if (occupied[cell]) { gameOver(); return; } head = (head + 1) % CELLS; body[head] = cell; occupied[cell] = true; if (eating) { length++; score++; Serial.print("Score "); Serial.println(score); if (length == CELLS) { gameOver(); return; } placeFood(); // Each dot speeds the snake up a little, down to MIN_STEP_MS. stepMs = score >= 15 ? MIN_STEP_MS : START_STEP_MS - 8UL * score; } draw(); } void setup() { Serial.begin(115200); pinMode(JOY_SW, INPUT_PULLUP); matrix.begin(); newGame(); } void loop() { bool pushed = readStick(); unsigned long now = millis(); if (!playing) { // Wait for the stick to return to centre, so a held direction does // not restart the game straight away. if (!pushed) stickCentred = true; bool pressed = digitalRead(JOY_SW) == LOW; if (now - overAt > 600 && (pressed || (pushed && stickCentred))) newGame(); } else if (now - lastStep >= stepMs) { lastStep = now; step(); } else if (now - lastBlink >= BLINK_MS) { lastBlink = now; foodVisible = !foodVisible; draw(); } delay(10); }
Firmware licence
Notice: this firmware uses the LGPL-licensed Arduino core library
arduino-unor4wifi-matrix-snake.bin is statically linked with ArduinoCore-renesas 1.6.0, in Breadboard's port of its UNO R4 WiFi variant to Renesas FSP 5.3.0. Parts of that core are licensed under the GNU Lesser General Public License, version 2.1 or (at your option) any later version ("the Library"). Breadboard distributes this firmware under terms that permit you to modify the work for your own use and to reverse engineer it for debugging such modifications.
- Relink package: arduino-unor4wifi-matrix-snake-relink.zip (351 KB)
- Corresponding source: ArduinoCore-renesas-1.6.0-fsp53-source.zip (3.7 MB)
- Licence texts and relink instructions



