/*
This project is to replicate an E-11 STormtrooper Blaster
Major componens list:
1- ESP32 C6 (seeed)
2- 0.91" OLED display is a compact 128×32-pixel driven by an SSD1306 controller
3- TP4056 CHARGING MODULE WITH BATTERY PROTECTION CIRCUIT
4- 10K RESISTORS = 3
5- 4.7L RESISTOR = 1
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// =====================================
// E-11 BLASTER V2
// Phase 2: Selector + OLED + Ammo
// =====================================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_ADDRESS 0x3C
#define OLED_SDA 6
#define OLED_SCL 7
#define FIRE_BUTTON 2
#define RELOAD_BUTTON 3
#define MODE_POT 4
#define MAX_AMMO 25
#define DEBOUNCE_MS 35
Adafruit_SSD1306 display(
SCREEN_WIDTH,
SCREEN_HEIGHT,
&Wire,
-1
);
enum BlasterMode {
SAFE,
LASER,
STUN,
SPECIAL
};
const char* modeNames[] = {
"SAFE",
"LASER",
"STUN",
"SPECIAL"
};
BlasterMode currentMode = SAFE;
int ammo = MAX_AMMO;
// Button debounce state
struct Button {
int pin;
bool lastReading = HIGH;
bool stableState = HIGH;
unsigned long lastChange = 0;
};
Button fireButton = {FIRE_BUTTON};
Button reloadButton = {RELOAD_BUTTON};
// =====================================
// BUTTON HANDLING
// =====================================
bool buttonPressed(Button &button) {
bool reading = digitalRead(button.pin);
if (reading != button.lastReading) {
button.lastChange = millis();
button.lastReading = reading;
}
if (millis() - button.lastChange >= DEBOUNCE_MS) {
if (reading != button.stableState) {
button.stableState = reading;
if (button.stableState == LOW) {
return true;
}
}
}
return false;
}
// =====================================
// SELECTOR
// =====================================
BlasterMode readSelector() {
int value = analogRead(MODE_POT);
if (value < 1024) return SAFE;
if (value < 2048) return LASER;
if (value < 3072) return STUN;
return SPECIAL;
}
// =====================================
// OLED DISPLAY
// =====================================
void drawScreen() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("E-11 BLASTER");
display.drawLine(0, 10, 127, 10, SSD1306_WHITE);
display.setCursor(0, 14);
display.print("MODE: ");
display.println(modeNames[currentMode]);
display.setCursor(0, 24);
display.print("AMMO: ");
display.print(ammo);
if (currentMode == SAFE) {
display.setCursor(82, 24);
display.print("SAFE");
} else if (ammo == 0) {
display.setCursor(82, 24);
display.print("EMPTY");
}
display.display();
}
// =====================================
// SETUP
// =====================================
void setup() {
Serial.begin(115200);
pinMode(FIRE_BUTTON, INPUT_PULLUP);
pinMode(RELOAD_BUTTON, INPUT_PULLUP);
analogReadResolution(12);
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(
SSD1306_SWITCHCAPVCC,
OLED_ADDRESS
)) {
Serial.println("OLED initialisation failed");
while (true) {
delay(100);
}
}
currentMode = readSelector();
drawScreen();
Serial.println("E-11 BLASTER READY");
}
// =====================================
// MAIN LOOP
// =====================================
void loop() {
// Check selector
BlasterMode newMode = readSelector();
if (newMode != currentMode) {
currentMode = newMode;
Serial.print("MODE: ");
Serial.println(modeNames[currentMode]);
drawScreen();
}
// Check fire button
if (buttonPressed(fireButton)) {
if (currentMode == SAFE) {
Serial.println("FIRING DISABLED - SAFE MODE");
}
else if (currentMode == LASER ||
currentMode == STUN) {
if (ammo > 0) {
ammo--;
Serial.print(modeNames[currentMode]);
Serial.print(" FIRE - Ammo: ");
Serial.println(ammo);
} else {
Serial.println("AMMO EMPTY");
}
}
else if (currentMode == SPECIAL) {
Serial.println("SPECIAL MODE - RESERVED");
}
drawScreen();
}
// Check reload button
if (buttonPressed(reloadButton)) {
ammo = MAX_AMMO;
Serial.println("RELOAD COMPLETE");
drawScreen();
}
delay(5);
}Loading
esp32-c6-devkitc-1
esp32-c6-devkitc-1
Loading
ssd1306
ssd1306