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.
Arduino UNO
Gesture Recognition Sensor PAJ7620
0.96" OLED
GND is connected to:
3.3V is connected to:
A4 is connected to:
A5 is connected to:
A0 is connected to:
A1 is connected to:
5V is connected to:
Ground is connected to:
VCC is connected to:
SCL is connected to:
SDA is connected to:
GND is connected to:
VDD is connected to:
SCK is connected to:
SDA is connected to:
/*
* 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();
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
/*
* 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
}