medical delivery robot — Arduino Mega simulation

An interactive Arduino Mega circuit simulation you can run free in your browser on Velxio, by vsrrobotics.

Components

Sketch code

// ROBOT (Arduino Mega 2560): full delivery cycle, sends ARRIVED:<room> on Serial1
// Motor A = left wheel, Motor B = right wheel
// Red LED is on pin 48 (pin 46 clashes with the Servo library timer)
// Serial1 TX = pin 18 -> Nano RX. On the real build this wire becomes the 433 MHz transmitter.

#include <Servo.h>

const int ENA = 5;
const int IN1 = 22;
const int IN2 = 23;
const int ENB = 6;
const int IN3 = 24;
const int IN4 = 25;

const int irPins[3] = {A0, A1, A2};   // Left, Centre, Right

const int TRIG_FRONT = 30;
const int ECHO_FRONT = 31;
const int TRIG_REAR  = 32;
const int ECHO_REAR  = 33;

const int SERVO_PIN = 9;
const int LED_RED   = 48;

const int FAST     = 120;   // forward speeds
const int SLOW     = 60;
const int REV_FAST = 100;   // reverse speeds
const int REV_SLOW = 50;

// Flip to true on the real robot if reverse steering corrects the wrong way
const bool REV_STEER_INVERT = false;

const int LID_CLOSED_ANGLE = 0;
const int LID_OPEN_ANGLE   = 90;

// Obstacle detection
const int OBSTACLE_CM = 20;
const unsigned long SONAR_INTERVAL_MS = 60;

// Timers: shortened for testing
const unsigned long ARRIVAL_WAIT_MS  = 10000;   // real: 120000 (2 min)
const unsigned long LID_OPEN_WAIT_MS = 8000;    // real: 60000 (1 min)
const unsigned long RECEIVED_WAIT_MS = 10000;   // real: 30000 (30 s)
const unsigned long STUCK_MS         = 6000;    // real: 10000. Set 0 to disable.

// Radio link
const unsigned long RADIO_SEND_MS = 1000;       // ARRIVED is sent once a second

// Buttons: 0 LAB, 1 EMERGENCY, 2 PATIENT ROOM, 3 OPEN, 4 CLOSE, 5 RECEIVED
const int NUM_BTNS = 6;
const int btnPins[NUM_BTNS] = {40, 41, 42, 43, 44, 45};
const char* btnNames[NUM_BTNS] = {"LAB", "EMERGENCY", "PATIENT ROOM", "OPEN", "CLOSE", "RECEIVED"};

const unsigned long DEBOUNCE_MS = 20;
const unsigned long ARM_MS      = 2000;
const unsigned long BAR_MS      = 30;

bool stableState[NUM_BTNS];
bool lastRaw[NUM_BTNS];
unsigned long rawChangedAt[NUM_BTNS];
bool armed = false;
unsigned long allHighSince = 0;

enum State { IDLE, GOING_OUT, WAITING_USER, LID_OPEN_AT_DEST, WAITING_RECEIVED, RETURNING, FAILED_HOME, STUCK };
State state = IDLE;
unsigned long stateStart = 0;

Servo lidServo;
bool lidOpen = false;

enum LedMode { LED_OFF, LED_BLINK, LED_SOLID };
LedMode ledMode = LED_OFF;
unsigned long lastBlink = 0;
bool blinkOn = false;

int targetJunction = 0;
int junctionCount = 0;
int barsToGo = 0;
bool deliveryFailed = false;
bool wasOnBar = false;
unsigned long barSince = 0;
int lastPattern = -1;

// Obstacle and stuck tracking
bool obstaclePaused = false;
int nearCount = 0;
int clearCount = 0;
unsigned long lastSonar = 0;
bool halted = false;
unsigned long haltedSince = 0;

unsigned long lastRadioSend = 0;

// ---------------------------------------------------------------------------
// RADIO (robot side). On the real build only this function changes.
// ---------------------------------------------------------------------------
void radioSendArrived(int room) {
  Seri

More projects by vsrrobotics