DIY compact projecters

From PD Research Wiki
Revision as of 21:18, 8 November 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);
}

Turn table arduino code

// 200-step NEMA 17 with EasyDriver
// EasyDriver defaults to 1/8 microstepping
// 1600 STEP pulses per motor revolution
//
// Motor pulley: 60T
// Turntable effective pulley: approximately 314T
// Gear ratio: 314 / 60 = 5.233
//
// Current pulse method:
// STEP HIGH for stepDelay
// STEP LOW  for stepDelay
//
// Theoretical:
// RPM_motor = 18750 / stepDelay
// RPM_table = 3582.8 / stepDelay
//
// Approximate 16 mm lens theoretical neutral:
// table ~9.1 RPM
// motor ~47.6 RPM
// stepDelay ~394 us
//
// Real RPM still needs mechanical calibration.

const int stepPin   = 12;
const int dirPin    = 11;
const int potPin    = A0;

const int switch01  = 2;
const int switch02  = 3;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  pinMode(switch01, INPUT_PULLUP);
  pinMode(switch02, INPUT_PULLUP);

  digitalWrite(stepPin, LOW);

  Serial.begin(115200);
}

void loop() {

  // Read the 3-position direction switch
  bool switch1On = (digitalRead(switch01) == LOW);
  bool switch2On = (digitalRead(switch02) == LOW);

  // Read potentiometer
  int potValue = analogRead(potPin);

  // About 1.2 to 17.9 theoretical table RPM
 // int stepDelay = map(potValue, 0, 1023, 3000, 200);
  int stepDelay = map(potValue, 0, 1023, 3000, 120);

  // Theoretical RPM only
  float motorRPM = 18750.0 / stepDelay;
  float tableRPM = 3582.8 / stepDelay;

  bool motorRunning = false;

  // Switch position 1
  if (switch1On && !switch2On) {
    digitalWrite(dirPin, LOW);
    motorRunning = true;
  }

  // Switch position 2
  else if (switch2On && !switch1On) {
    digitalWrite(dirPin, HIGH);
    motorRunning = true;
  }

  // Middle position:
  // both inputs HIGH -> stop
  else {
    motorRunning = false;
  }

  // Generate a step only when one direction is selected
  if (motorRunning) {

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);

    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay);
  }

  // Serial output
  static unsigned long lastPrint = 0;

  if (millis() - lastPrint >= 500) {
    lastPrint = millis();

    Serial.print("Pot: ");
    Serial.print(potValue);

    Serial.print("   Delay: ");
    Serial.print(stepDelay);

    if (!motorRunning) {
      Serial.println(" us   STOP");
    }
    else {
      Serial.print(" us   Motor RPM: ");
      Serial.print(motorRPM, 2);

      Serial.print("   Table RPM: ");
      Serial.print(tableRPM, 2);

      Serial.print("   DIR: ");

      if (switch1On) {
        Serial.println("LOW");
      } else {
        Serial.println("HIGH");
      }
    }
  }
}