ZISHAN — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by zishanansari94.

Components

Sketch code

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

// =====================================================
// PINS
// =====================================================

#define FIRE_PIN     4
#define TRIGGER_PIN  27
#define RELOAD_PIN   26
#define BUZZER_PIN   25

// =====================================================
// OLED
// =====================================================

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// =====================================================
// AMMO
// =====================================================

const int MAX_AMMO = 30;
int ammo = MAX_AMMO;

// =====================================================
// FAST FIRE
// =====================================================
//
// Approximately 15 shots/sec
//
// 20 ms ON
// 47 ms OFF
//
// Total = 67 ms
//

const unsigned long FIRE_ON  = 20;
const unsigned long FIRE_OFF = 47;

// =====================================================
// BUZZER
// =====================================================

const int BUZZER_FREQUENCY = 1400;

// =====================================================
// FIRE STATE
// =====================================================

bool fireState = false;

unsigned long lastFireChange = 0;

// =====================================================
// BUZZER STATE
// =====================================================

bool buzzerRunning = false;

// =====================================================
// OLED REFRESH
// =====================================================

// OLED is NOT allowed to control firing speed.
// It refreshes separately.

const unsigned long OLED_UPDATE_TIME = 80;

unsigned long lastOLEDUpdate = 0;

int lastDisplayedAmmo = -1;

// =====================================================
// SHOW AMMO
// =====================================================

void updateOLED()
{
    if (ammo == lastDisplayedAmmo)
        return;

    display.clearDisplay();

    display.setTextColor(SSD1306_WHITE);

    display.setTextSize(1);
    display.setCursor(48, 5);
    display.println("AMMO");

    display.setTextSize(4);

    if (ammo >= 10)
        display.setCursor(40, 25);
    else
        display.setCursor(52, 25);

    display.println(ammo);

    display.display();

    lastDisplayedAmmo = ammo;
}

// =====================================================
// START FIRE PULSE
// =====================================================

void startFirePulse()
{
    if (ammo <= 0)
        return;

    // ================================================
    // ONE SHOT
    // ================================================

    ammo--;

    // ================================================
    // MUZZLE / IR / LED ON
    // ================================================

    digitalWrite(FIRE_PIN, HIGH);

    // ================================================
    // BUZZER ON
    //
    // Duration is a