/****************************************************************
Project Title: Motor Speed and Direction Control with LEDs
Target Hardware: ESP32
Summary: This program controls motor speed and direction using LEDs
as indicators. Slide switches control direction, while PWM
adjusts LED brightness to simulate speed.
****************************************************************/
// Define pin numbers
#define SPEED1_PIN 13 // GPIO13 for Motor 1 speed control (Green LED)
#define SPEED2_PIN 12 // GPIO12 for Motor 2 speed control (Red LED)
#define DIR1_PIN 21 // GPIO21 for Motor 1 direction (Blue LED)
#define DIR2_PIN 22 // GPIO22 for Motor 2 direction (Yellow LED)
#define SW_DIR1 23 // GPIO23 for Motor 1 direction switch
#define SW_DIR2 19 // GPIO19 for Motor 2 direction switch
// Define PWM parameters
#define PWM_FREQ 1000 // PWM frequency in Hz
#define PWM_RESOLUTION 10 // PWM resolution (10 bits: 0-1023)
#define PWM_CHANNEL1 0 // PWM channel for Motor 1 speed
#define PWM_CHANNEL2 1 // PWM channel for Motor 2 speed
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
Serial.println("Motor Speed and Direction Control Simulation Started!");
// Attach PWM to pins for speed control
ledcAttachChannel(SPEED1_PIN, PWM_FREQ,PWM_RESOLUTION, PWM_CHANNEL1);
ledcAttachChannel(SPEED2_PIN, PWM_FREQ,PWM_RESOLUTION, PWM_CHANNEL2);
// ledcAttach(SPEED2_PIN, PWM_CHANNEL2, PWM_FREQ, PWM_RESOLUTION);
// Configure switch and direction pins as inputs or outputs
pinMode(SW_DIR1, INPUT);
pinMode(SW_DIR2, INPUT);
pinMode(DIR1_PIN, OUTPUT);
pinMode(DIR2_PIN, OUTPUT);
}
void loop() {
// Read switch states
bool direction1 = digitalRead(SW_DIR1);
bool direction2 = digitalRead(SW_DIR2);
// Set direction LEDs
digitalWrite(DIR1_PIN, direction1);
digitalWrite(DIR2_PIN, direction2);
// Simulate speed by adjusting PWM brightness
int speed1 = direction1 ? 512 : 256; // Example PWM values for direction
int speed2 = direction2 ? 512 : 256;
ledcWrite(PWM_CHANNEL1, speed1);
ledcWrite(PWM_CHANNEL2, speed2);
// Print status to serial monitor
Serial.print("Motor 1 - Direction: ");
Serial.print(direction1 ? "Forward" : "Reverse");
Serial.print(", Speed: ");
Serial.print(speed1);
Serial.print(" | Motor 2 - Direction: ");
Serial.print(direction2 ? "Forward" : "Reverse");
Serial.print(", Speed: ");
Serial.println(speed2);
delay(500); // Delay to allow for readable output in the serial monitor
}