Arduino Stepper iPad Height Machine!

Bill of Materials

Required Libraries

One external library is required for the T-O-F sketch. Download and copy the ‘VL53L0X-arduino-master’ folder to program files/arduino/libraries.

Click here to download the VL53L0X library

T-O-F Sensor Arduino Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
|| -height-detecting iPad slider sensor control
|| -version 1.0
|| -author Ed Rees
|| -11/28/2017
|| -copyright Saunders Machine Works, LLC 2017
*/


#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor; // time-of-flight (LIDAR) height sensor.
VL53L0X sensor2; // time-of-flight (LIDAR) person sensor.

void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

delay(500);
Wire.begin();

pinMode(4, INPUT);
sensor.init(true); // initialize person sensor.
sensor.setAddress((uint8_t)22); // set i2c address of person sensor.

pinMode(5, INPUT);
sensor2.init(true); // initialize height sensor.
sensor2.setAddress((uint8_t)25); // set i2c address of height sensor.

sensor.init(); // reinitialize person sensor with new i2c address.
sensor.setTimeout(500);
sensor.startContinuous(100); // begin sensing at 100mS intervals.

sensor2.init(); // reinitialize height sensor with new i2c address.
sensor2.setTimeout(500);
sensor2.startContinuous(100); // begin sensing at 100mS intervals.
}

void loop()
{
if (sensor2.readRangeContinuousMillimeters() <= 1100) // person sensing.
{
digitalWrite(2, HIGH);
}
else
{
digitalWrite(2, LOW);
}

if (sensor.readRangeContinuousMillimeters() <= 1100 ) // height sensing.
{
digitalWrite(3, HIGH);
}
else
{
digitalWrite(3, LOW);
}
}

iPad Stepper Arduino Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
|| -height-detecting iPad slider stepper control
|| -version 1.0
|| -author Ed Rees
|| -11/28/2017
|| -copyright Saunders Machine Works, LLC 2017
|| -credit to Rob Hebing (robhebing.nl) for stepper functions.
*/


#define nrOfSteppers 1 //the number of steppermotors connected to arduino
const int stepperDirectionPin[nrOfSteppers] = {8}; //the pins of the steppers that control the direction
const int stepperPulsePin[nrOfSteppers] = {6}; //the pins of the steppers that control the pulse

int pers = 2; // person sensor pin.
int height = 3; // height sensor pin.
int Home = 11; // home switch pin.
int pwr = 12; // stepper power relay (power switch tail) control.
bool trigger = false; // records initial motion trigger
bool top = false; // holds iPad carrier at top of motion
int initStepDelay = 65; // stepper speed lower limit. value represents ms delay between steps. (higher = slower).
float stepDelay = initStepDelay; // initializes step delay equal to initial value.
float minStepDelay = 10; // stepper speed upper limit. value represents ms delay between steps. (lower = faster).
float accelRate = .2; // controls acceleration slope. (higher = faster acceleration speed).

void setup()
{

for (int s = 0; s < nrOfSteppers; s++) // set stepper output pins.
{
pinMode(stepperDirectionPin[s], OUTPUT);
digitalWrite(stepperDirectionPin[s], LOW);
pinMode(stepperPulsePin[s], OUTPUT);
digitalWrite(stepperPulsePin[s], LOW);
}

pinMode(pers, INPUT);
pinMode(height, INPUT);
pinMode(Home, INPUT);
pinMode(pwr, OUTPUT);
}

void loop()
{

if (digitalRead(pers) == HIGH) // if a person is detected, supply power to stepper driver - else remove power and reset trigger and top.
{
digitalWrite(pwr, HIGH);
}
else
{
digitalWrite(pwr, LOW);
top = false;
trigger = false;
}

 

while (digitalRead(pers) == HIGH && digitalRead(height) == HIGH && top == false) // while height sensor detects person and iPad is not at top of motion, move iPad upward.
{
trigger = true;
stepper(0, 15, 1); // move 15 steps upward.

if (stepDelay >= minStepDelay) // simple linear acceleration method.
{
stepDelay = stepDelay - accelRate; // every cycle through this while loop, the step delay is reduced by accelRate until minStepDelay is reached.
}
}

 

if (trigger == true && digitalRead(height) == LOW) // if scaning cycle has been triggered, and height sensor no longer detects person.
{
trigger = false;
top = true;
}

 

while (top == true && digitalRead(pers) == HIGH) // hold at top while person is detected.
{
delay(100);
}

 

while (digitalRead(pers) == LOW && digitalRead(Home) == LOW) // once person is no longer detected, move downward until home switch is triggered.
{
if (top == true)
{
top = false;
stepDelay = initStepDelay; // reset stepper speed to lower limit for return motion and for next operation cycle.
}
stepper(0, 5, 0); // move downward 5 steps per pass through while loop.
}

if (digitalRead(Home) == HIGH) // once home switch is triggered, reset relvant variables and remove stepper power.
{
delay(1000);
digitalWrite(pwr, LOW);
}
}

 

// stepper functions below. credit to Rob Hebing (robhebing.nl).

void(* resetFunc) (void) = 0; //declare reset function @ address 0

void stepper(int motor, int steps, boolean stepDirection)
{
int currentDirection = digitalRead(stepperDirectionPin[motor]); // first check the direction, if it's not the current direction change it.
if (stepDirection != currentDirection)
{
digitalWrite(stepperDirectionPin[motor], stepDirection);
delay(500); // this delay makes the motor stop for a while when changing direction
}

for (int s = 0; s < steps; s++) // a for loop to create the pulse
{
digitalWrite(stepperPulsePin[motor], LOW); //the LOW, then HIGH creates the pulse the driver is waiting for, no delay needed.
digitalWrite(stepperPulsePin[motor], HIGH);
delayMicroseconds(stepDelay); //this delay creates the pulse, the lower the number the faster the pulse.
}
}

Arduino Duo Nano Dock (3D Printed)