Arduino UNO R4 Simple Game
I tried to make a simple game with the Arduino R4 board, I only added the GY-521 accelerometer sensor
I simply searched the web for a tutorial on how to connect a GY-521 to Arduino
Then I asked chatGPT to create a code for me to create the game you see in the video, I made various attempts and corrections, among these I asked that the GY-521 sensor be initialized as a horizontal position at the first start of the game (turning on the Arduino) and I also asked to store the record (if created) in the Arduino internal memory (Eprom). In this way the record will be stored even when the Arduino is turned off.
And here is the code I created with the help of chatGPT
//sensore GY-521 accellerometro giroscopio SDA A4 SCL A5 VCC 3,3V
#include <Arduino_LED_Matrix.h>
#include <EEPROM.h>
#include <Wire.h>
ArduinoLEDMatrix matrix;
const uint32_t animation[][4] = {
{
0x7008810,
0x40200201,
0xac18c000,
66
},
{
0x1040f800,
0x200201,
0xac18c000,
66
}
};
// Frame della matrice LED
uint8_t frame[8][12] = {0};
#define ROWS 8
#define COLUMNS 12
// Variabili di posizione del LED
int pointX = COLUMNS / 2;
int pointY = ROWS / 2;
int R = 0;
// Indirizzo del GY-521
const int MPU_ADDR = 0x68;
// Variabili per il gioco
int fixedX, fixedY;
int score = 0;
unsigned long startTime;
unsigned long elapsedTime;
bool gameActive = true;
// Variabili per il record
const int RECORD_ADDR = 0; // Indirizzo EEPROM per il record
unsigned long bestTime = 999999; //RECORD CHE VIENE SALVATO
// Variabili per i dati del sensore
int16_t accX, accY;
int16_t baseAccX = 0, baseAccY = 0;
void setup() {
Serial.begin(115200);
delay(1500); //EEPROM.put(RECORD_ADDR, 99999); ///per resettare il record in memoria
matrix.begin();
// Inizializza il sensore GY-521
Wire.begin();
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x6B); // Registro di alimentazione
Wire.write(0); // Sveglia il sensore
Wire.endTransmission(true);
// Calibrazione
calibrateSensor();
// Carica il record dalla EEPROM
EEPROM.get(RECORD_ADDR, bestTime);
// Genera il primo obiettivo
generateTarget();
startTime = millis();
}
void loop() { /////////////LOOP
if (gameActive) {
updatePosition();
checkCollision();
renderGame();
} else {
displayResults();
}
} /////////////LOOP
void calibrateSensor() {
Serial.println("Calibrating... Please hold the board still.");
delay(2000);
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, 4, true);
baseAccX = (Wire.read() << 8 | Wire.read());
baseAccY = (Wire.read() << 8 | Wire.read());
}
void updatePosition() {
// Leggi il sensore
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, 4, true);
accX = (Wire.read() << 8 | Wire.read());
accY = (Wire.read() << 8 | Wire.read());
int deltaY = map((int32_t)(accX - baseAccX), -17000, 17000, -1, 1);
int deltaX = map((int32_t)(accY - baseAccY), -17000, 0, -1, 1);
if (!(pointX == 0 && deltaX < 0) && !(pointX == COLUMNS - 1 && deltaX > 0)) {
pointX += deltaX;
}
if (!(pointY == 0 && deltaY < 0) && !(pointY == ROWS - 1 && deltaY > 0)) {
pointY += deltaY;
}
}
void generateTarget() {
fixedX = random(0, COLUMNS);
fixedY = random(0, ROWS);
}
void checkCollision() {
if (pointX == fixedX && pointY == fixedY) {
// Effetto luce piena
memset(frame, 1, sizeof(frame));
matrix.renderBitmap(frame, ROWS, COLUMNS);
delay(300);
score++;
if (score >= 7) { /////NUMERO DEI TARGET
gameActive = false;
elapsedTime = millis() - startTime;
if (elapsedTime < bestTime) { /////EPROM RECORD
R = 1;
bestTime = elapsedTime;
EEPROM.put(RECORD_ADDR, bestTime);
}
} else { R = 0;
generateTarget();
}
}
}
void renderGame() {
memset(frame, 0, sizeof(frame));
// Gestione del lampeggio dell'obiettivo
static unsigned long lastBlinkTime = 0;
static bool targetVisible = true;
if (millis() - lastBlinkTime >= 100) {
lastBlinkTime = millis();
targetVisible = !targetVisible;
}
if (targetVisible) {
frame[7 - fixedY][fixedX] = 1; // Obiettivo lampeggiante
}
frame[7 - pointY][pointX] = 1; // Giocatore
matrix.renderBitmap(frame, ROWS, COLUMNS);
delay(100);
}
void displayResults() {
Serial.println (R);
Serial.println (elapsedTime);
Serial.println (bestTime);
if (R == 1) {
matrix.loadFrame(animation[0]);
}
else {
matrix.loadFrame(animation[1]);
while (true); // Blocca il programma finché non viene premuto il reset
}
}
That's all. Have fun!