// Simplified ESP32 sketch for Wokwi simulation — NO PWM
// - Uses 4 push-buttons (active LOW with INPUT_PULLUP)
// - Controls 4 direction LEDs and 2 digital outputs for motor enable (ON/OFF)
// Pin mapping and wiring instructions are in the chat (below the canvas).
// Pin definitions (change if you want)
const int BTN_UP = 32; // pushbutton to GND (active LOW)
const int BTN_DOWN = 33; // pushbutton to GND (active LOW)
const int BTN_LEFT = 25; // pushbutton to GND (active LOW)
const int BTN_RIGHT = 26; // pushbutton to GND (active LOW)
// Direction indicator outputs (LEDs to GND through resistor)
const int OUT_UP = 18; // LED for forward
const int OUT_DOWN = 19; // LED for backward
const int OUT_LEFT = 21; // LED for left turn
const int OUT_RIGHT = 22; // LED for right turn
// Motor enable digital outputs (no PWM)
const int EN_A = 5; // digital enable A (HIGH = motor ON)
const int EN_B = 4; // digital enable B (HIGH = motor ON)
void setup() {
Serial.begin(115200);
delay(10);
// Buttons
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
pinMode(BTN_LEFT, INPUT_PULLUP);
pinMode(BTN_RIGHT, INPUT_PULLUP);
// Direction LEDs / outputs
pinMode(OUT_UP, OUTPUT);
pinMode(OUT_DOWN, OUTPUT);
pinMode(OUT_LEFT, OUTPUT);
pinMode(OUT_RIGHT, OUTPUT);
// Motor enable pins (digital)
pinMode(EN_A, OUTPUT);
pinMode(EN_B, OUTPUT);
// Ensure stopped at start
stopAll();
}
void loop() {
// Buttons are active LOW (pressed == LOW)
bool upPressed = digitalRead(BTN_UP) == LOW;
bool downPressed = digitalRead(BTN_DOWN) == LOW;
bool leftPressed = digitalRead(BTN_LEFT) == LOW;
bool rightPressed = digitalRead(BTN_RIGHT) == LOW;
// Movement priority: up > down > left > right
if (upPressed) {
moveForward();
} else if (downPressed) {
moveBackward();
} else if (leftPressed) {
turnLeft();
} else if (rightPressed) {
turnRight();
} else {
stopAll();
}
delay(50);
}
// Movement helper functions (no PWM — only ON/OFF)
void moveForward() {
Serial.println("State: FORWARD");
digitalWrite(OUT_UP, HIGH);
digitalWrite(OUT_DOWN, LOW);
digitalWrite(OUT_LEFT, LOW);
digitalWrite(OUT_RIGHT, LOW);
// both motors ON
digitalWrite(EN_A, HIGH);
digitalWrite(EN_B, HIGH);
}
void moveBackward() {
Serial.println("State: BACKWARD");
digitalWrite(OUT_UP, LOW);
digitalWrite(OUT_DOWN, HIGH);
digitalWrite(OUT_LEFT, LOW);
digitalWrite(OUT_RIGHT, LOW);
// both motors ON (direction assumed handled by H-bridge wiring if present)
digitalWrite(EN_A, HIGH);
digitalWrite(EN_B, HIGH);
}
void turnLeft() {
Serial.println("State: TURN LEFT");
digitalWrite(OUT_UP, LOW);
digitalWrite(OUT_DOWN, LOW);
digitalWrite(OUT_LEFT, HIGH);
digitalWrite(OUT_RIGHT, LOW);
// left turn: only one motor ON (or opposite signs depending on wiring)
digitalWrite(EN_A, HIGH);
digitalWrite(EN_B, LOW);
}
void turnRight() {
Serial.println("State: TURN RIGHT");
digitalWrite(OUT_UP, LOW);
digitalWrite(OUT_DOWN, LOW);
digitalWrite(OUT_LEFT, LOW);
digitalWrite(OUT_RIGHT, HIGH);
// right turn: only the other motor ON
digitalWrite(EN_A, LOW);
digitalWrite(EN_B, HIGH);
}
void stopAll() {
Serial.println("State: STOP");
digitalWrite(OUT_UP, LOW);
digitalWrite(OUT_DOWN, LOW);
digitalWrite(OUT_LEFT, LOW);
digitalWrite(OUT_RIGHT, LOW);
digitalWrite(EN_A, LOW);
digitalWrite(EN_B, LOW);
}