Basic Setup:

Arduino Mega w ESP 8266 

CNC Shield

Arduino Nano on Print head

4 x LDO6AJSA LED Driver boards link

4 x 3W 380nm UV LED link

1 x Heatsink link

I. System Architecture Diagram

The system operates across three distinct tiers, separating the user interface, the motion control, and the real-time hardware execution.

======================================================================
                  TIER 1: THE NETWORK HUB (ESP32)
======================================================================
  [ Web Browser ]  --> HTTP POST (JSON) -->  [ ESP32 Web Server ]
  - User draws UI                            - Broadcasts standalone AP
  - Sends gridData                           - Hosts index.html & app.js
                                             - Converts JSON to Hex
                                                      |
                                                      | Serial2 (115200 Baud)
                                                      | TX/RX Protocol
                                                      v
======================================================================
               TIER 2: THE MAIN BRAIN (Mega 2560 + CNC Shield)
======================================================================
  [ Arduino Mega 2560 ] <----> [ Hardware Peripherals ]
  - Parses Hex image data      - X, Y, Z Stepper Motors
  - Calculates Mesh Bed        - SSD1306 OLED Screen
    Leveling (MBL)             - SD Card (Config & Mesh Storage)
  - Orchestrates X/Y motion    - User Input Buttons
                                                      |
                                                      | Serial1 (115200 Baud)
                                                      | TX/RX Protocol
                                                      v
======================================================================
               TIER 3: THE PRINTHEAD MCU (Nano Custom PCB)
======================================================================
  [ Arduino Nano ] <---------> [ Printhead Hardware ]
  - Real-time peripheral device - D10, D11, D12, D13: 4x UV LEDs
  - Never blocks UART          - D3, D7: CR Touch (Servo & Signal)
  - Microsecond timers         - A0-A5: TMP36 Temp Sensors
                               - D8: NeoPixel Status Indicator
======================================================================

I. System Architecture

The system operates across three distinct tiers, separating the user interface, the motion control, and the real-time hardware execution.

TIER 1: THE NETWORK HUB (ESP32)

TIER 2: THE MAIN BRAIN (Arduino Mega 2560)

TIER 3: THE PRINTHEAD MCU (Arduino Nano Custom PCB)

II. Protocol: Website to ESP32 (The Payload)

The frontend uses a simple, modern REST approach to send the user's design to the ESP32.

1. The HTTP POST Request When the user clicks "Print," the JavaScript fetch API fires a JSON payload to the "/print" ESP32's endpoint.

2. ESP32 to Mega Translation The ESP32 intercepts this JSON, strips out the network overhead, and compresses the pixel array into a single, continuous hexadecimal string to save RAM on the Mega. It sends this over hardware UART.

III. Protocol: Mega to Printhead (The Hardware UART Dictionary)

The Arduino Nano operates as a strict, non-blocking peripheral. It sits on Serial1 at 115200 baud, listening for newline-terminated ASCII commands. It immediately executes them and responds with a status string and a NeoPixel color flash.

Command Reference Table

CommandActionExpected ReturnNeoPixel Visual
S\nStatus Poll: Asks the Nano which LEDs are currently firing.S:[mask] (e.g., S:15 for all, S:0 for none)Dim White → Green
F:m,d0,d1,d2,d3\nFire LEDs: Sends the bitmask (m) and microsecond durations (dX) for all 4 LEDs.(None, must poll with S to check when finished)Purple → Green
D\nDeploy Probe: Commands the CR Touch servo to drop the pin.(None)Cyan → Green
U\nStow Probe: Commands the CR Touch servo to pull the pin up.(None)Cyan → Green
X\nReset Probe: Commands CR Touch to clear red alarm states.(None)Cyan → Green
P\nProbe State: Checks if the CR Touch signal pin is triggered.P:1 (Triggered) or P:0 (Open)Cyan → Green
T\nThermal Poll: Requests background temperature readings from the 6 sensors.T:v0,v1,v2,v3,v4,v5 (Values in Celsius)Dim White → Green
R\nEmergency Reset: Immediately kills all active UV LEDs and resets timers.(None)Orange → Green