Arduino Code

image40

Code for XYZ camera slider

/*

stepper motor Rail Pan Tilt camera controller

by Markus Fuller 2020 www.markusfuller.com

using 1 Arduino UNO 3x nema17 stepper motors and 3x EasyDrivers from www.schmalzhaus.com

Library - AccelStepper by Mike McCauley:

*/

#include <AccelStepper.h>

#include <MultiStepper.h>

#define PanX A0  // Pan X pin

#define TiltY A1  // Tilt Y pin

#define Rail A2  // Slider potentiometer

#define inOutPot A3  // In and Out speed potentiometer

#define PanStop 10 // to stop pan cable tangling

#define Button 12  // Set Button

#define EndStop 11

#define RedLED 8  // RGB Led Red

#define GreenLED 9 // RGB Led Green

#define blueLED 13 // RGB Led blue

// Set stepper motor pins on Arduino as type, Step, and Direction,

AccelStepper stepper1(1, 7, 6);

AccelStepper stepper2(1, 5, 4);

AccelStepper stepper3(1, 3, 2);

MultiStepper StepperControl;

long gotoposition[3]; // stores the A and B position for every stepper motor

int PanPos = 0;

int TiltYPos = 0;

int RailPos = 0;

int currentSpeed = 100;

int inOutSpeed = 100;

int XInPoint = 0;

int YInPoint = 0;

int ZInPoint = 0;

int XOutPoint = 0;

int YOutPoint = 0;

int ZOutPoint = 0;

int InandOut = 0;

void setup() {

stepper1.setMaxSpeed(3000);

stepper1.setSpeed(300);

stepper2.setMaxSpeed(1500);

stepper2.setSpeed(75);

stepper3.setMaxSpeed(1500);

stepper3.setSpeed(75);

pinMode(PanStop, INPUT_PULLUP); //faultstop to help prevent pan cable tangling

pinMode(Button, INPUT_PULLUP); //setswitch which is also activate button

pinMode(EndStop, INPUT_PULLUP); //endstop microswitch

pinMode(RedLED, OUTPUT); //red led

pinMode(GreenLED, OUTPUT); //green led

pinMode(blueLED, OUTPUT); //blue led

StepperControl.addStepper(stepper1);

StepperControl.addStepper(stepper2);

StepperControl.addStepper(stepper3);

// Move the slider to the Home position when first turned on

while (digitalRead(EndStop) != 0) {

stepper1.setSpeed(1500);

stepper1.runSpeed();

stepper1.setCurrentPosition(0); // When EndStop is activated this sets position as step 0

}

delay(30);

// Moves back slightly from the rail endstop

while (stepper1.currentPosition() != -1500) {

stepper1.setSpeed(-1500);

stepper1.run();

// This section below for Pan centre control

}

// Moves the pan head anticlockwise until microswitch is activated

while (digitalRead(PanStop) != 0) {

stepper3.setSpeed(-400);

stepper3.runSpeed();

stepper3.setCurrentPosition(0); // When Pan is centred set position to 0 steps

}

delay(30);

// Moves pan clockwise untilfacing forward, this may need to be changed depending on how many teeth between stepper and pan wheel

while (stepper3.currentPosition() != 1070) { 

stepper3.setSpeed(400); 

stepper3.run();

// End of section above

digitalWrite(blueLED, HIGH); // blue led turns on

}

}

void loop() {

// this will cancel out the rail movement if you try and travel beyond safe limits

while (digitalRead(EndStop) == 0 || stepper1.currentPosition() < -40500) {}

if (digitalRead (Button) == 0) {

delay(1000); // hold switch for 1 second to reset all values then LED turns Blue

if (digitalRead (Button) == 0) {

InandOut = 4;

}

switch (InandOut) {

case 0:  // Set the A position

InandOut = 1;

XInPoint = stepper1.currentPosition(); // Set the IN position for steppers 1

YInPoint = stepper2.currentPosition(); // Set the IN position for steppers 2

ZInPoint = stepper3.currentPosition(); // Set the IN position for steppers 3

digitalWrite(blueLED, LOW); // MSF blue led goes out

digitalWrite(RedLED, HIGH); // Light up inLed

break;

case 1: // Set the B position

InandOut = 2;

XOutPoint = stepper1.currentPosition(); // Set the B positions for steppers

YOutPoint = stepper2.currentPosition();

ZOutPoint = stepper3.currentPosition();

digitalWrite(RedLED, LOW); // red Led goes off then Green Led Illuminates stating ready to run

digitalWrite(GreenLED, HIGH);

break;

case 2: // Move to A position / go to next part case 3

InandOut = 3;

inOutSpeed = analogRead(inOutPot);

// Place the A position into the Array

gotoposition[0] = XInPoint;

gotoposition[1] = YInPoint;

gotoposition[2] = ZInPoint;

stepper1.setMaxSpeed(inOutSpeed);

stepper2.setMaxSpeed(inOutSpeed);

stepper3.setMaxSpeed(inOutSpeed);

StepperControl.moveTo(gotoposition);

StepperControl.runSpeedToPosition();

delay(200);

break;

case 3: // Move to B position and returns go back to case 2 above

InandOut = 2;

inOutSpeed = analogRead(inOutPot);

// Place the B position into the Array

gotoposition[0] = XOutPoint;

gotoposition[1] = YOutPoint;

gotoposition[2] = ZOutPoint;

stepper1.setMaxSpeed(inOutSpeed);

stepper2.setMaxSpeed(inOutSpeed);

stepper3.setMaxSpeed(inOutSpeed);

StepperControl.moveTo(gotoposition);

StepperControl.runSpeedToPosition();

delay(200);

break;

case 4: // If Set button is held longer than one second go back to the beginning case 0 above

InandOut = 0;

digitalWrite(RedLED, LOW);

digitalWrite(GreenLED, LOW);

digitalWrite(blueLED, HIGH);

delay(1000);

break;

}

}

// PanX - Pan movement XPos

PanPos = analogRead(PanX);

// if Pan Pot is moved left, move stepper 2 pan anticlockwise

if (PanPos > 600) {

//stepper2.setSpeed(currentSpeed);

PanPos = map(PanPos, 600, 1024, 0, 200);

stepper2.setSpeed(PanPos); // Increase speed

}

// if PanPot is moved right, move stepper 2 pan clockwise

else if (PanPos < 400) {

//stepper2.setSpeed(-currentSpeed);

PanPos = map(PanPos, 400, 0, 0, 200);

stepper2.setSpeed(-PanPos); // Increase speed

}

// if PanPot is centred then there is no movement

else {

stepper2.setSpeed(0);

}

//Tilt Y - Tilt movement

TiltYPos = analogRead(TiltY);

if (TiltYPos > 600) {

//stepper3.setSpeed(currentSpeed);

TiltYPos = map(TiltYPos, 600, 1024, 0, 200);

stepper3.setSpeed(TiltYPos); // Increase speed

}

else if (TiltYPos < 400) {

//stepper3.setSpeed(-currentSpeed);

TiltYPos = map(TiltYPos, 400, 0, 0, 200); // Try changing this to lower value to slow tilt stepper

stepper3.setSpeed(-TiltYPos); // Increase speed

// If TiltPot is centred then there is no movement

}

else {

stepper3.setSpeed(0);

}

// RAIL potentiometer

RailPos = analogRead(Rail);

// If potentiometer is turned left then the Gantry moves towards the control unit

if (RailPos > 600) {

RailPos = map(RailPos, 600, 1024, 0, 3000);

stepper1.setSpeed(-RailPos); // Increase speed

}

// If potentiometer is turned right then the Gantry moves away from the control unit

else if (RailPos < 400 ) {

RailPos = map(RailPos, 400, 0, 0, 3000);

stepper1.setSpeed(RailPos); // Increase speed

// If potentiometer is centred then there is no movement

}  

else {

stepper1.setSpeed(0);

}

// run the camera slider

stepper1.runSpeed();

stepper2.runSpeed();

stepper3.runSpeed();

}

Some more info that may be helpful

All the potentiometers are B10K

All resistors are 120k ( I think but you can experiment )

The problem I had was fixing pins on the underside of the board so it would plug into the Arduino Uno. I used header pins soldered them into place then gently prized off the black plastic strip that held the pins in a row so they all lined up with the Arduino (maybe I should have used double sided board. 

The image of the board above shows Blue jumper wires. (again not needed if double sided board was used as you could place these jumpers onto the copper strip on the second side.

the 3 stepper motor boards were placed close to the motors so I only needed the Step and Direction outputs from the board going to the easystepper boards.

I started working on a much smaller board with arduino nano and controls in a seperate box and a 15pin d-sub cable going to the rail with 3 mini stepper controllers on the motors. like I say though that is a something I am working on but thinking about making it control via Bluetooth.

 Any questions please do contact me and let me see your creations. good luck. Markus.