Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Wi-Fi Controlled Laser Shooting Game with OLED Display

View and Edit Project In Cirkit DesignerImage of ESP32-Based Wi-Fi Controlled Laser Shooting Game with OLED Display

Circuit Documentation

Summary

This circuit is designed to control a motorized system using an ESP32 microcontroller, a TB6612FNG motor driver, and various sensors and actuators. The system includes two photosensitive sensor modules for light intensity detection, a 0.96" OLED display for visual feedback, a DC Mini Metal Gear Motor for movement, and a laser controlled by a MOSFET. The circuit is powered by a 12V battery, regulated down to 3.3V using a Mini-360 DC-DC Step Down Buck Converter. The ESP32 microcontroller is programmed to handle motor control, sensor readings, and display updates.

Component List

Photosensitive Sensor Module Digital Light Intensity Detection

DC Mini Metal Gear Motor

TB6612FNG Motor Driver

0.96" OLED

ESP32 Devkit V1

Mosfet

12V Battery

Rocker Switch (SPST)

Mini-360 DC-DC Step Down Buck Converter

LED bulb AC / Bombillo AC

Wiring Details

Photosensitive Sensor Module Digital Light Intensity Detection

DC Mini Metal Gear Motor

TB6612FNG Motor Driver

0.96" OLED

ESP32 Devkit V1

Mosfet

12V Battery

Rocker Switch (SPST)

Mini-360 DC-DC Step Down Buck Converter

LED bulb AC / Bombillo AC

Code Documentation

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

// Define OLED display width and height
#define OLED_WIDTH 128
#define OLED_HEIGHT 64

// Define the I2C address for the OLED display
#define OLED_ADDR 0x3C

// Create display object
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);  // -1 for no reset pin

// Pin definitions
#define PHOTORESISTOR_1_PIN 14  // Pin for first LM393 module
#define PHOTORESISTOR_2_PIN 16  // Pin for second LM393 module
#define LASER_PIN 18            // Define GPIO pin for laser control

// Define motor driver pins
#define PWMA_PIN 23
#define AIN2_PIN 19
#define AIN1_PIN 5
#define PWMB_PIN 15
#define BIN2_PIN 2
#define BIN1_PIN 4 

// Define PWM Parameters
const int motorFreq = 1000;
const int motorResolution = 8;

// Define channels for each motor
const int motorAChannel = 3;
const int motorBChannel = 4;

// Fixed speed for movement and turning
const int moveSpeed = 200;  // Adjust this value for desired movement speed
const int turnSpeed = 180;  // Adjust this value for desired turning speed

// Variables for game state
int val1;
int val2;
int score = 0;
unsigned long lastScoreTime = 0;             // Tracks the last time the score was incremented
const unsigned long untouchableTime = 2000;  // 2 seconds of untouchable time

// Variables for laser control
bool laserOn = false; // Laser state (idle by default)
unsigned long laserOffTime = 0; // Tracks the time to return to idle state
const unsigned long laserFullPowerTime = 2000; // Laser stays at full power for 2 seconds

// Motor movement function
void moveMotors(int mtrAspeed, int mtrBspeed, bool mtrdir) {
  // Set direction pins
  if (!mtrdir) {
    // Move in reverse
    digitalWrite(AIN1_PIN, HIGH);
    digitalWrite(AIN2_PIN, LOW);
    digitalWrite(BIN1_PIN, HIGH);
    digitalWrite(BIN2_PIN, LOW);
  } else {
    // Move forward
    digitalWrite(AIN1_PIN, LOW);
    digitalWrite(AIN2_PIN, HIGH);
    digitalWrite(BIN1_PIN, LOW);
    digitalWrite(BIN2_PIN, HIGH);
  }

  // Drive motors with PWM
  ledcWrite(motorAChannel, mtrAspeed);
  ledcWrite(motorBChannel, mtrBspeed);
}

// PS3 controller callback function to handle D-pad inputs and shooting
void notify() {
  // Detect D-pad inputs for movement
  if (Ps3.data.button.up) {
    // Move forward
    moveMotors(moveSpeed, moveSpeed, true);
    Serial.println("Moving forward");

  } else if (Ps3.data.button.down) {
    // Move backward
    moveMotors(moveSpeed, moveSpeed, false);
    Serial.println("Moving backward");

  } else if (Ps3.data.button.left) {
    // Turn left (motor B runs faster, motor A runs slower/reverse)
    moveMotors(0, moveSpeed, true);
    Serial.println("Turning left");

  } else if (Ps3.data.button.right) {
    // Turn right (motor A runs faster, motor B runs slower