DIY compact projecters

From PD Research Wiki
Revision as of 19:01, 31 October 2026 by Nan (talk | contribs)
Jump to navigation Jump to search

DIY compact projectors

Designing a new version of compact and modular projectors that can easily fit into a suitcase.

hardware

Mircocontroller:

Currently using:

Arduino Leonardo

Alternative boards (varies on the number of I/O pins):


code and softwares

Arduino code

31 October 2026

#include <MIDIUSB.h>

// =====================================
// PIN DEFINITIONS
// =====================================

// Linear actuator 1
const byte IN1 = 0;
const byte IN2 = 1;
const byte LIMIT1 = 7;
const byte ENABLE1 = 12;
const byte POT_REVERSE1 = A2;

// Linear actuator 2
const byte IN3 = 2;
const byte IN4 = 3;
const byte LIMIT2 = 8;
const byte ENABLE2 = 13;
const byte POT_REVERSE2 = A3;

// Three spinning motors (shared pulse)
const byte SPIN_MOTORS = 5;
const byte POT_SPIN_INTERVAL = A0;
const byte POT_SPIN_DURATION = A1;

// Three XHP50 LED drivers
const byte LED1 = 9;
const byte LED2 = 10;
const byte LED3 = 11;

const byte POT_LED1 = A4;
const byte POT_LED2 = A5;
const byte POT_LED3 = A6;

// =====================================
// SETTINGS
// =====================================

const unsigned long REVERSE_MIN = 200;
const unsigned long REVERSE_MAX = 3000;
const unsigned long PAUSE_TIME = 50;

// Motor states:
// 0 = forward
// 1 = pause before reverse
// 2 = reverse
// 3 = pause before forward

byte state1 = 0;
byte state2 = 0;

unsigned long motorTime1 = 0;
unsigned long motorTime2 = 0;

unsigned long reverseTime1 = 0;
unsigned long reverseTime2 = 0;

// Used to pause and resume the timers
bool wasEnabled1 = false;
bool wasEnabled2 = false;

unsigned long stoppedAt1 = 0;
unsigned long stoppedAt2 = 0;

// Spinning motor timer
unsigned long spinStart = 0;

// =====================================
// MOTOR DIRECTION FUNCTIONS
// =====================================

void forward1() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
}

void backward1() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
}

void stop1() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
}

void forward2() {
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void backward2() {
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void stop2() {
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

// =====================================
// USB MIDI
// =====================================

void sendMidiNote(byte note) {

  midiEventPacket_t noteOn = {
    0x09, 0x90, note, 127
  };

  midiEventPacket_t noteOff = {
    0x08, 0x80, note, 0
  };

  MidiUSB.sendMIDI(noteOn);
  MidiUSB.sendMIDI(noteOff);
  MidiUSB.flush();
}

// =====================================
// LINEAR ACTUATOR 1
// =====================================

void updateMotor1(unsigned long now) {

  bool enabled = digitalRead(ENABLE1) == LOW;

  // Switch OFF: stop and remember time
  if (!enabled) {

    if (wasEnabled1) {
      stoppedAt1 = now;
    }

    stop1();
    wasEnabled1 = false;
    return;
  }

  // Switch ON: resume timer
  if (!wasEnabled1) {

    if (stoppedAt1 != 0) {
      motorTime1 += now - stoppedAt1;
      stoppedAt1 = 0;
    }

    wasEnabled1 = true;
  }

  switch (state1) {

    case 0: // FORWARD

      forward1();

      if (digitalRead(LIMIT1) == LOW) {

        stop1();
        sendMidiNote(60);

        reverseTime1 = map(
          analogRead(POT_REVERSE1),
          0, 1023,
          REVERSE_MIN, REVERSE_MAX
        );

        motorTime1 = now;
        state1 = 1;
      }
      break;

    case 1: // PAUSE BEFORE REVERSE

      if (now - motorTime1 >= PAUSE_TIME) {
        backward1();
        motorTime1 = now;
        state1 = 2;
      }
      break;

    case 2: // REVERSE

      backward1();

      if (now - motorTime1 >= reverseTime1) {
        stop1();
        sendMidiNote(61);

        motorTime1 = now;
        state1 = 3;
      }
      break;

    case 3: // PAUSE BEFORE FORWARD

      if (now - motorTime1 >= PAUSE_TIME) {
        forward1();
        state1 = 0;
      }
      break;
  }
}

// =====================================
// LINEAR ACTUATOR 2
// =====================================

void updateMotor2(unsigned long now) {

  bool enabled = digitalRead(ENABLE2) == LOW;

  // Switch OFF: stop and remember time
  if (!enabled) {

    if (wasEnabled2) {
      stoppedAt2 = now;
    }

    stop2();
    wasEnabled2 = false;
    return;
  }

  // Switch ON: resume timer
  if (!wasEnabled2) {

    if (stoppedAt2 != 0) {
      motorTime2 += now - stoppedAt2;
      stoppedAt2 = 0;
    }

    wasEnabled2 = true;
  }

  switch (state2) {

    case 0: // FORWARD

      forward2();

      if (digitalRead(LIMIT2) == LOW) {

        stop2();
        sendMidiNote(62);

        reverseTime2 = map(
          analogRead(POT_REVERSE2),
          0, 1023,
          REVERSE_MIN, REVERSE_MAX
        );

        motorTime2 = now;
        state2 = 1;
      }
      break;

    case 1: // PAUSE BEFORE REVERSE

      if (now - motorTime2 >= PAUSE_TIME) {
        backward2();
        motorTime2 = now;
        state2 = 2;
      }
      break;

    case 2: // REVERSE

      backward2();

      if (now - motorTime2 >= reverseTime2) {
        stop2();
        sendMidiNote(63);

        motorTime2 = now;
        state2 = 3;
      }
      break;

    case 3: // PAUSE BEFORE FORWARD

      if (now - motorTime2 >= PAUSE_TIME) {
        forward2();
        state2 = 0;
      }
      break;
  }
}

// =====================================
// THREE SPINNING MOTORS
// =====================================

void updateSpinningMotors(unsigned long now) {

 int intervalPot = analogRead(POT_SPIN_INTERVAL);
  int durationPot = analogRead(POT_SPIN_DURATION);

  // Minimum position = OFF
  if (durationPot< 2) {
    digitalWrite(SPIN_MOTORS, LOW);
    spinStart = now;
    return;
  }

  unsigned long interval = map(
    intervalPot,
    0, 1023,
    10, 1200
  );

  unsigned long duration = map(
    durationPot,
    0, 1023,
    2, 600
  );

  duration = min(duration, interval);

  unsigned long phase =
    (now - spinStart) % interval;

  digitalWrite(
    SPIN_MOTORS,
    phase < duration ? HIGH : LOW
  );
  }


// =====================================
// THREE LED BLINK CONTROLS
// =====================================

void updateLED(
  byte ledPin,
  byte potPin,
  unsigned long now
) {

  int pot = analogRead(potPin);

  // Minimum position = OFF
  if (pot < 10) {
    digitalWrite(ledPin, LOW);
    return;
  }

  unsigned long interval = map(
    pot,
    10, 1023,
    100, 10000
  );

  unsigned long phase = now % interval;

  // 50% ON / 50% OFF
  digitalWrite(
    ledPin,
    phase < interval / 2 ? HIGH : LOW
  );
}

// =====================================
// SETUP
// =====================================

void setup() {

  // Linear actuator outputs
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  // Lever switches
  pinMode(LIMIT1, INPUT_PULLUP);
  pinMode(LIMIT2, INPUT_PULLUP);

  // Manual ON/OFF switches
  pinMode(ENABLE1, INPUT_PULLUP);
  pinMode(ENABLE2, INPUT_PULLUP);

  // Spinning motors
  pinMode(SPIN_MOTORS, OUTPUT);

  // LED driver PWM inputs
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  // Everything initially stopped
  stop1();
  stop2();

  digitalWrite(SPIN_MOTORS, LOW);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
}

// =====================================
// MAIN LOOP
// =====================================

void loop() {

  unsigned long now = millis();

  // Independent linear actuators
  updateMotor1(now);
  updateMotor2(now);

  // Shared spinning motor pulse
  updateSpinningMotors(now);

  // Independent LED blinking
  updateLED(LED1, POT_LED1, now);
  updateLED(LED2, POT_LED2, now);
  updateLED(LED3, POT_LED3, now);
}