From 219e812c2e01d8df294051381b126811da243554 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 18 Oct 2025 03:13:47 +0000 Subject: [PATCH 1/4] Initial plan From 8657e0ad1ee933da22ba4d19653bc7394d4767b7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 18 Oct 2025 03:18:45 +0000 Subject: [PATCH 2/4] Add ESP32 4-servo controller with PlatformIO setup Co-authored-by: DarinAnthony <159497601+DarinAnthony@users.noreply.github.com> --- .gitignore | 16 +++++++ README.md | 118 ++++++++++++++++++++++++++++++++++++++++++++- platformio.ini | 17 +++++++ src/main.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 platformio.ini create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0301f47 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# PlatformIO files +.pio/ +.vscode/ +.pioenvs/ +.piolibdeps/ + +# Build artifacts +*.pyc +__pycache__/ +.cache/ + +# IDE files +.DS_Store +*.swp +*.swo +*~ diff --git a/README.md b/README.md index 3317d9a..860197a 100644 --- a/README.md +++ b/README.md @@ -1 +1,117 @@ -# icebreakers \ No newline at end of file +# ESP32 4-Servo Controller + +This repository contains code for programming an ESP32 microcontroller to control 4 microservos continuously in a loop pattern. + +## Overview + +The program makes 4 servos sweep back and forth continuously between 0° and 180°, creating a smooth looping motion. Each servo operates independently and can be customized. + +## Hardware Requirements + +- ESP32 development board (ESP32-DevKitC or compatible) +- 4x Microservos (SG90 or similar) +- Power supply for servos (5V, adequate current for 4 servos - typically 2A+) +- Jumper wires +- Breadboard (optional) + +## Wiring Connections + +Connect the servos to the following GPIO pins on the ESP32: + +| Servo | GPIO Pin | +|-------|----------| +| Servo 1 | GPIO 18 | +| Servo 2 | GPIO 19 | +| Servo 3 | GPIO 21 | +| Servo 4 | GPIO 22 | + +**Important:** +- Connect all servo signal wires to the respective GPIO pins +- Connect all servo ground (brown/black) wires to ESP32 GND +- Connect all servo power (red) wires to external 5V power supply +- Connect ESP32 GND to power supply GND (common ground) + +## Software Requirements + +### Install PlatformIO + +This project uses PlatformIO for dependency management and building. You can use PlatformIO with: + +#### Option 1: PlatformIO IDE (VS Code Extension) +1. Install [Visual Studio Code](https://code.visualstudio.com/) +2. Install the PlatformIO IDE extension from VS Code marketplace +3. Open this project folder in VS Code + +#### Option 2: PlatformIO CLI +```bash +# Install PlatformIO CLI +pip install platformio + +# Navigate to project directory +cd icebreakers +``` + +## Dependencies + +The project automatically installs the following dependencies (configured in `platformio.ini`): +- **ESP32Servo** - Servo control library optimized for ESP32 + +These will be automatically downloaded when you first build the project. + +## Building and Uploading + +### Using PlatformIO IDE (VS Code) +1. Open the project folder in VS Code +2. Click the checkmark icon (✓) in the bottom toolbar to build +3. Click the arrow icon (→) in the bottom toolbar to upload to ESP32 + +### Using PlatformIO CLI +```bash +# Build the project +pio run + +# Upload to ESP32 (make sure ESP32 is connected via USB) +pio run --target upload + +# Monitor serial output (optional) +pio device monitor +``` + +## Customization + +You can modify the following parameters in `src/main.cpp`: + +- **Servo pins**: Change `SERVO_PIN_1` through `SERVO_PIN_4` constants +- **Angle range**: Modify `MIN_ANGLE` and `MAX_ANGLE` (default: 0-180°) +- **Speed**: Adjust `STEP_DELAY` (milliseconds between steps) +- **Smoothness**: Change `ANGLE_STEP` (degrees per step) + +## Serial Monitor + +The program outputs servo positions to the serial monitor at 115200 baud. You can view this for debugging: + +```bash +pio device monitor --baud 115200 +``` + +## Troubleshooting + +- **Servos jittering**: Ensure adequate power supply for all servos +- **Servos not moving**: Check wiring connections and GPIO pin assignments +- **Upload fails**: Make sure ESP32 is properly connected and correct COM port is selected +- **Build errors**: Run `pio lib install` to ensure all dependencies are installed + +## Project Structure + +``` +icebreakers/ +├── src/ +│ └── main.cpp # Main Arduino sketch +├── platformio.ini # PlatformIO configuration & dependencies +├── .gitignore # Git ignore file +└── README.md # This file +``` + +## License + +This project is open source and available for educational and personal use. \ No newline at end of file diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..399394c --- /dev/null +++ b/platformio.ini @@ -0,0 +1,17 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +lib_deps = + ESP32Servo +monitor_speed = 115200 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d040ab4 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,126 @@ +/* + * ESP32 4-Servo Continuous Loop Controller + * + * This program controls 4 microservos connected to an ESP32, + * moving them continuously in a loop pattern. + * + * Connections: + * - Servo 1: GPIO 18 + * - Servo 2: GPIO 19 + * - Servo 3: GPIO 21 + * - Servo 4: GPIO 22 + * + * The servos will sweep back and forth continuously. + */ + +#include +#include + +// Define servo pins +const int SERVO_PIN_1 = 18; +const int SERVO_PIN_2 = 19; +const int SERVO_PIN_3 = 21; +const int SERVO_PIN_4 = 22; + +// Create servo objects +Servo servo1; +Servo servo2; +Servo servo3; +Servo servo4; + +// Servo parameters +const int MIN_ANGLE = 0; +const int MAX_ANGLE = 180; +const int STEP_DELAY = 15; // Delay in ms between steps +const int ANGLE_STEP = 1; // Degrees to move per step + +// Current positions +int pos1 = MIN_ANGLE; +int pos2 = MIN_ANGLE; +int pos3 = MIN_ANGLE; +int pos4 = MIN_ANGLE; + +// Direction flags (1 = increasing, -1 = decreasing) +int dir1 = 1; +int dir2 = 1; +int dir3 = 1; +int dir4 = 1; + +void setup() { + // Initialize serial communication for debugging + Serial.begin(115200); + Serial.println("ESP32 4-Servo Controller Starting..."); + + // Attach servos to pins + servo1.attach(SERVO_PIN_1); + servo2.attach(SERVO_PIN_2); + servo3.attach(SERVO_PIN_3); + servo4.attach(SERVO_PIN_4); + + // Initialize servos to start position + servo1.write(pos1); + servo2.write(pos2); + servo3.write(pos3); + servo4.write(pos4); + + Serial.println("Servos initialized. Starting continuous loop..."); + + delay(1000); // Wait 1 second before starting +} + +void loop() { + // Update servo 1 + pos1 += dir1 * ANGLE_STEP; + if (pos1 >= MAX_ANGLE) { + pos1 = MAX_ANGLE; + dir1 = -1; + } else if (pos1 <= MIN_ANGLE) { + pos1 = MIN_ANGLE; + dir1 = 1; + } + servo1.write(pos1); + + // Update servo 2 (slightly offset for visual effect) + pos2 += dir2 * ANGLE_STEP; + if (pos2 >= MAX_ANGLE) { + pos2 = MAX_ANGLE; + dir2 = -1; + } else if (pos2 <= MIN_ANGLE) { + pos2 = MIN_ANGLE; + dir2 = 1; + } + servo2.write(pos2); + + // Update servo 3 + pos3 += dir3 * ANGLE_STEP; + if (pos3 >= MAX_ANGLE) { + pos3 = MAX_ANGLE; + dir3 = -1; + } else if (pos3 <= MIN_ANGLE) { + pos3 = MIN_ANGLE; + dir3 = 1; + } + servo3.write(pos3); + + // Update servo 4 + pos4 += dir4 * ANGLE_STEP; + if (pos4 >= MAX_ANGLE) { + pos4 = MAX_ANGLE; + dir4 = -1; + } else if (pos4 <= MIN_ANGLE) { + pos4 = MIN_ANGLE; + dir4 = 1; + } + servo4.write(pos4); + + // Small delay between updates + delay(STEP_DELAY); + + // Optional: Print positions every 100 iterations for debugging + static int counter = 0; + counter++; + if (counter >= 100) { + Serial.printf("Positions - S1:%d S2:%d S3:%d S4:%d\n", pos1, pos2, pos3, pos4); + counter = 0; + } +} From 0cfeca9fa53ce97872ea37675d0ee4d94ad3bf06 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 18 Oct 2025 03:19:48 +0000 Subject: [PATCH 3/4] Refactor servo control to reduce code duplication Co-authored-by: DarinAnthony <159497601+DarinAnthony@users.noreply.github.com> --- src/main.cpp | 61 ++++++++++++++++------------------------------------ 1 file changed, 18 insertions(+), 43 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d040ab4..c47d2ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,50 +68,25 @@ void setup() { delay(1000); // Wait 1 second before starting } -void loop() { - // Update servo 1 - pos1 += dir1 * ANGLE_STEP; - if (pos1 >= MAX_ANGLE) { - pos1 = MAX_ANGLE; - dir1 = -1; - } else if (pos1 <= MIN_ANGLE) { - pos1 = MIN_ANGLE; - dir1 = 1; - } - servo1.write(pos1); - - // Update servo 2 (slightly offset for visual effect) - pos2 += dir2 * ANGLE_STEP; - if (pos2 >= MAX_ANGLE) { - pos2 = MAX_ANGLE; - dir2 = -1; - } else if (pos2 <= MIN_ANGLE) { - pos2 = MIN_ANGLE; - dir2 = 1; - } - servo2.write(pos2); - - // Update servo 3 - pos3 += dir3 * ANGLE_STEP; - if (pos3 >= MAX_ANGLE) { - pos3 = MAX_ANGLE; - dir3 = -1; - } else if (pos3 <= MIN_ANGLE) { - pos3 = MIN_ANGLE; - dir3 = 1; +// Helper function to update a single servo +void updateServo(Servo &servo, int &pos, int &dir) { + pos += dir * ANGLE_STEP; + if (pos >= MAX_ANGLE) { + pos = MAX_ANGLE; + dir = -1; + } else if (pos <= MIN_ANGLE) { + pos = MIN_ANGLE; + dir = 1; } - servo3.write(pos3); - - // Update servo 4 - pos4 += dir4 * ANGLE_STEP; - if (pos4 >= MAX_ANGLE) { - pos4 = MAX_ANGLE; - dir4 = -1; - } else if (pos4 <= MIN_ANGLE) { - pos4 = MIN_ANGLE; - dir4 = 1; - } - servo4.write(pos4); + servo.write(pos); +} + +void loop() { + // Update all servos + updateServo(servo1, pos1, dir1); + updateServo(servo2, pos2, dir2); + updateServo(servo3, pos3, dir3); + updateServo(servo4, pos4, dir4); // Small delay between updates delay(STEP_DELAY); From 5b6fd62672e6782e59b0619f522a63f8c500d029 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 18 Oct 2025 03:20:59 +0000 Subject: [PATCH 4/4] Add documentation to updateServo helper function Co-authored-by: DarinAnthony <159497601+DarinAnthony@users.noreply.github.com> --- src/main.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c47d2ae..ef755d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,12 @@ void setup() { delay(1000); // Wait 1 second before starting } -// Helper function to update a single servo +/** + * Update a single servo's position in the continuous loop + * @param servo Reference to the Servo object to update + * @param pos Reference to current position (modified by this function) + * @param dir Reference to current direction (modified by this function) + */ void updateServo(Servo &servo, int &pos, int &dir) { pos += dir * ANGLE_STEP; if (pos >= MAX_ANGLE) {