Skip to content

Embedded programming

Strategy

In V0.1, the code was all mixed up in one huge Arduino file, which made it hard to read and change. In V0.2, I decided to split the code into smaller files for each function.

Header (.h) and source (.cpp)

File type Role What it contains Notes
.ino Orchestration / entry point setup(), loop(), high-level flow Typically only one per project, Arduino-specific, auto-includes Arduino.h, auto-generates prototypes, merged into .cpp at build time
.h Interface Declarations, shared constants, class definitions No logic, included with #include, usually starts with #pragma once
.cpp Implementation Function bodies, internal state, hardware logic Standard C++, must include its own .h and explicitly include needed headers (e.g. Arduino.h)
  1. Header (.h)

    • Function declarations
    • Shared constants
    • Class declarations
    // motion.h
    #pragma once
    void moveTo(float x, float y);
    
    • #pragma once prevents duplicate inclusion
  2. Source (.cpp)

    • Function implementations
    • Internal state
    • Hardware logic
    // motion.cpp
    #include "motion.h"
    
    void moveTo(float x, float y) {
        // motor control
    }
    
    • #include pulls the header in, controls where a file is used
  3. Configuration

    PLOTBOT_V02/
    ├── PLOTBOT_V02.ino   → Program entry point and module orchestration.
    │
    ├── pins.h            → Centralized GPIO pin mapping.
    ├── config.h          → Machine constants and tuning values.
    │
    ├── motion.h          → Motion control interface.
    ├── motion.cpp        → Stepper control and kinematics implementation.
    │
    ├── gcode.h           → -code command interface.
    ├── gcode.cpp         → G-code parsing and execution logic.
    │
    ├── power_monitor.h   → Power monitoring interface.
    ├── power_monitor.cpp → ADC voltage measurement implementation.
    │
    ├── display.h         → Display update interface.
    └── display.cpp       → OLED initialization and rendering.
    

Codes

Pin assignment

Pico GPIO Category Usage Notes
GP0 UART TX Communication with MCU or sensor
GP1 UART RX ↑
GP2 I2C SDA Communication with sensor or OLED (Pull-up 10kΩ)
GP3 I2C SCL ↑ (Pull-up 10kΩ)
GP4 SPI MISO Communication with sensor
GP5 SPI CS ↑ (Pull-up 10kΩ)
GP6 SPI SCK ↑
GP7 SPI MOSI ↑
GP8 GPIO GP8 General purpose (e.g., 5V fan)
GP9 GPIO GP9 ↑
GP10 GPIO GP10 ↑
GP11 GPIO GP11 ↑
GP12 GPIO GP12 ↑
GP13 GPIO SERVO PWM for servo motor
GP14 GPIO LIMIT_X Limit switch for X
GP15 GPIO LIMIT_Y Limit switch for Y
GP16 A4988 DIR_RF DIR/STP for A4988
GP17 A4988 STEP_RF ↑
GP18 A4988 STEP_RB ↑
GP19 A4988 DIR_RB ↑
GP20 A4988 STEP_LB ↑
GP21 A4988 STEP_LB ↑
GP22 A4988 DIR_LF ↑
GP26 ADC ADC Battery voltage divided by 10k & 33kΩ
GP27 A4988 STEP_LF DIR/STP for A4988
GP28 A4988 EN Shared for all drivers