PCBX
Published

Create Your Own Arduino Obstacle Avoidance Game Circuit

Create an Arduino game that teaches coding by navigating a robot through obstacles—fun and educational.

BeginnerFull instructions provided88
Create Your Own Arduino Obstacle Avoidance Game Circuit

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
LCD1602 display or HD44780
×1
Breadboard (generic)
Breadboard (generic)
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1
Gravity:Digital Push Button (Yellow)
DFRobot Gravity:Digital Push Button (Yellow)
×1
resistor-PCBX
resistor-PCBX
×1

Software apps and online services

PCBX Online Simulation

Hand tools and fabrication machines

10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long

Story

Read more

Schematics

20241023094825_2YsLv4rVOv.png

Code

Arduino Obstacle Avoidance Game Circuit

Arduino
#include <LiquidCrystal.h>

// Initialize LCD pins
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Initialize button pin
const int buttonPin = 7;
int buttonState = HIGH; // Current button state
int lastButtonState = HIGH; // Last button state

// Initialize player's position
bool isJumping = false; // Whether the player is jumping

// Obstacle structure
struct Obstacle {
  int x;
  int row;
} obstacles[2]; // Two obstacles, one for each row

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print("Avoid Obstacles!");
  delay(2000);
  lcd.clear();

  // Randomly generate obstacles for both rows with at least two columns between them
  obstacles[0].row = 0; // First row
  obstacles[1].row = 1; // Second row
  generateObstacles();
}

void loop() {
  int reading = digitalRead(buttonPin);

  // Detect if the button is pressed
  if (reading == LOW && lastButtonState == HIGH) {
    isJumping = true;
  }

  // If the button is released and the player is jumping
  if (reading == HIGH && isJumping) {
    isJumping = false;
  }

  // Move each obstacle
  for (int i = 0; i < 2; i++) {
    if (obstacles[i].x > 0) {
      obstacles[i].x--;
    } else {
      // Reset obstacle position to a random position after the sixth column
      obstacles[i].x = (i == 0) ? random(6, 16) : random(8, 16); // Ensure first row obstacle starts after 6th column
      // Ensure at least two columns between obstacles on different rows
      while (abs(obstacles[i].x - obstacles[(i + 1) % 2].x) < 3) {
        obstacles[i].x = (i == 0) ? random(6, 16) : random(8, 16);
      }
    }
  }

  // Clear the screen and draw the game state
  lcd.clear();
  if (isJumping) {
    lcd.setCursor(0, 0); // Player jumps on the first row
    lcd.print("P");
  } else {
    lcd.setCursor(0, 1); // Player falls on the second row
    lcd.print("P");
  }
  for (int i = 0; i < 2; i++) {
    lcd.setCursor(obstacles[i].x, obstacles[i].row);
    lcd.print("O");
  }

  // Detect collision
  if (checkCollision()) {
    gameover();
  }

  lastButtonState = reading; // Update button state
  delay(300); // Reduce delay to increase obstacle speed
}

bool checkCollision() {
  for (int i = 0; i < 2; i++) {
    // Detect if the player and obstacle are on the same row and column
    if ((isJumping && obstacles[i].row == 0 && obstacles[i].x == 0) ||
        (!isJumping && obstacles[i].row == 1 && obstacles[i].x == 0)) {
      return true;
    }
  }
  return false;
}

void gameover() {
  lcd.clear();
  lcd.print("Game Over!");
  delay(2000);
  lcd.clear();
  isJumping = false;
  // Reset obstacles
  generateObstacles();
}

void generateObstacles() {
  do {
    obstacles[0].x = random(6, 16); // First row obstacle starts after 6th column
    obstacles[1].x = random(8, 16); // Second row obstacle starts after 7th column
  } while (abs(obstacles[0].x - obstacles[1].x) < 3); // Ensure at least two columns between obstacles
}

Credits

PCBX
33 projects • 9 followers
Customer Success: Your one-stop solution for PCB and PCBA services, plus component sourcing. Enjoy FREE online simulation and EDA.
Contact

Comments

Please log in or sign up to comment.