Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Gesture-Controlled Snake Game with OLED Display

Image of Arduino UNO Gesture-Controlled Snake Game with OLED Display

Circuit Documentation

Summary

This circuit implements a simple snake game using an Arduino UNO, a PAJ7620 Gesture Recognition Sensor, and a 0.96" OLED display. The snake is controlled using gestures detected by the PAJ7620 sensor, and the game is displayed on the OLED screen.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. Gesture Recognition Sensor PAJ7620

    • Description: A sensor capable of recognizing various hand gestures.
    • Pins: VCC, Ground, SCL, SDA, INT, 1
  3. 0.96" OLED

    • Description: A small OLED display used to show the game.
    • Pins: GND, VDD, SCK, SDA

Wiring Details

Arduino UNO

  • GND is connected to:

    • GND of the 0.96" OLED
    • Ground of the Gesture Recognition Sensor PAJ7620
  • 3.3V is connected to:

    • VDD of the 0.96" OLED
  • A4 is connected to:

    • SCK of the 0.96" OLED
  • A5 is connected to:

    • SDA of the 0.96" OLED
  • A0 is connected to:

    • SCL of the Gesture Recognition Sensor PAJ7620
  • A1 is connected to:

    • SDA of the Gesture Recognition Sensor PAJ7620
  • 5V is connected to:

    • VCC of the Gesture Recognition Sensor PAJ7620

Gesture Recognition Sensor PAJ7620

  • Ground is connected to:

    • GND of the Arduino UNO
  • VCC is connected to:

    • 5V of the Arduino UNO
  • SCL is connected to:

    • A0 of the Arduino UNO
  • SDA is connected to:

    • A1 of the Arduino UNO

0.96" OLED

  • GND is connected to:

    • GND of the Arduino UNO
  • VDD is connected to:

    • 3.3V of the Arduino UNO
  • SCK is connected to:

    • A4 of the Arduino UNO
  • SDA is connected to:

    • A5 of the Arduino UNO

Code Documentation

Arduino UNO Code

/*
 * Snake Game using Arduino UNO, PAJ7620 Gesture Sensor, and 0.96" OLED
 *
 * This code implements a simple snake game where the snake is controlled
 * using gestures detected by the PAJ7620 sensor. The game is displayed
 * on a 0.96" OLED screen.
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <paj7620.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define SCL_PIN A0
#define SDA_PIN A1

int snakeX[100], snakeY[100];
int snakeLength = 5;
int foodX, foodY;
int direction = 0; // 0: up, 1: right, 2: down, 3: left

void setup() {
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  paj7620Init();
  paj7620WriteReg(0x72, 0x01); // Enable gesture recognition
  randomSeed(analogRead(0));
  resetGame();
}

void loop() {
  int gesture = paj7620ReadReg(0x43);
  if (gesture == GES_UP_FLAG) direction = 0;
  else if (gesture == GES_RIGHT_FLAG) direction = 1;
  else if (gesture == GES_DOWN_FLAG) direction = 2;
  else if (gesture == GES_LEFT_FLAG) direction = 3;
  moveSnake();
  checkCollision();
  drawGame();
  delay(200);
}

void resetGame() {
  snakeLength = 5;
  for (int i = 0; i < snakeLength; i++) {
    snakeX[i] = SCREEN_WIDTH / 2;
    snakeY[i] = SCREEN_HEIGHT / 2 + i;
  }
  spawnFood();
}

void moveSnake() {
  for (int i = snakeLength - 1; i > 0; i--) {
    snakeX[i] = snakeX[i - 1];
    snakeY[i] = snakeY[i - 1];
  }
  if (direction == 0) snakeY[0]--;
  else if (direction == 1) snakeX[0]++;
  else if (direction == 2) snakeY[0]++;
  else if (direction == 3) snakeX[0]--;
}

void checkCollision() {
  if (snakeX[0] == foodX && snakeY[0] == foodY) {
    snakeLength++;
    spawnFood();
  }
  if (snakeX[0] < 0 || snakeX[0] >= SCREEN_WIDTH ||
      snakeY[0] < 0 || snakeY[0] >= SCREEN_HEIGHT) {
    resetGame();
  }
  for (int i = 1; i < snakeLength; i++) {
    if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
      resetGame();
    }
  }
}

void spawnFood() {
  foodX = random(0, SCREEN_WIDTH);
  foodY = random(0, SCREEN_HEIGHT);
}

void drawGame() {
  display.clearDisplay();
  display.drawRect(foodX, foodY, 1, 1, SSD1306_WHITE);
  for (int i = 0; i < snakeLength; i++) {
    display.drawRect(snakeX[i], snakeY[i], 1, 1, SSD1306_WHITE);
  }
  display.display();
}

Gesture Recognition Sensor PAJ7620 Code

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

0.96" OLED Code

/*
 * Simple OLED Screen Initialization
 *
 * This code initializes a 0.96" OLED screen connected to an Arduino UNO.
 * The screen is powered on and a simple message is displayed.
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  // Initialize the display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.println("OLED Initialized!");
  display.display();
}

void loop() {
  // Nothing to do here
}