-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrawingMachineCode.ino
227 lines (192 loc) · 5.59 KB
/
drawingMachineCode.ino
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Arduino Pro Micro Drawing Machine Code (Continuous Line Drawing Machine - Super Make Something Episode 12) - https://youtu.be/-qeD2__yK4c
* by: Alex - Super Make Something
* date: February 24th, 2017
* license: Creative Commons - Attribution - Non-Commercial. More information available at: http://creativecommons.org/licenses/by-nc/3.0/
*/
// Includes derivative of "ReadWrite" by David A. Mellis and Tom Igoe available at: https://www.arduino.cc/en/Tutorial/ReadWrite
// Includes derivative of EasyDriver board sample code by Joel Bartlett available at: https://www.sparkfun.com/tutorials/400
/*
* This code contains the follow functions:
* - void setup(): initializes Serial port, SD card
* - void loop(): main loop
* - void rotateMotors(): rotates motors based on SD card values
*/
/*
* Pinout:
* SD card attached to SPI bus as follows:
* - MOSI - pin 16
* - MISO - pin 14
* - CLK - pin 15
* - CS - pin 10
*
* X-Axis stepper motor driver board:
* - STEP - pin 2
* - DIR - pin 3
* - MS1 - pin 4
* - MS2 - pin 5
* - Enable - pin 6
*
* Y-Axis stepper motor driver board:
* - STEP - pin 7
* - DIR - pin 8
* - MS1 - pin 9
* - MS2 - pin 18 (A0 on Arduino Pro Micro silkscreen)
* - ENABLE - pin 19 (A1 on Arduino Pro Micro silkscreen)
*/
#include <SPI.h>
#include <SD.h>
File xyValues;
String filename="drawing.txt";
int noXSteps=0;
int noYSteps=0;
int csPin=10;
int xStep=2;
int xDir=3;
int xMS1=4;
int xMS2=5;
int xEnable=6;
int yStep=7;
int yDir=8;
int yMS1=9;
int yMS2=18;
int yEnable=19;
int scalingFactor=5;
void setup()
{
//Define stepper pins as digital output pins
pinMode(xStep,OUTPUT);
pinMode(xDir,OUTPUT);
pinMode(xMS1,OUTPUT);
pinMode(xMS2,OUTPUT);
pinMode(xEnable,OUTPUT);
pinMode(yStep,OUTPUT);
pinMode(yDir,OUTPUT);
pinMode(yMS1,OUTPUT);
pinMode(yMS2,OUTPUT);
pinMode(yEnable,OUTPUT);
//Set microstepping mode for stepper driver boards. Using 1.8 deg motor angle (200 steps/rev) NEMA 17 motors (12V)
//X-Axis motor: no micro stepping (MS1 Low, MS2 Low) = 1.8 deg/step (200 steps/rev)
digitalWrite(xMS1,LOW);
digitalWrite(xMS2,LOW);
//Y-Axis motor: no micro stepping (MS1 Low, MS2 Low) = 1.8 deg/step (200 steps/rev)
digitalWrite(yMS1,LOW);
digitalWrite(yMS2,LOW);
//Enable motor controllers
digitalWrite(xEnable,LOW);
digitalWrite(yEnable,LOW);
//Open serial communications
Serial.begin(9600);
//Debug to Serial
Serial.print("Initializing SD card... ");
if (!SD.begin(csPin))
{
Serial.println("initialization failed! Aborting!");
Serial.println("----------");
return; //Aborts program if SD card initialization fails
}
Serial.println("initialization success!");
Serial.println("----------");
}
void loop()
{
//Open the file for reading:
xyValues = SD.open(filename);
if (xyValues)
{
Serial.print("Opened ");
Serial.print(filename);
Serial.println(":");
Serial.println("----------");
}
else
{
//If the file didn't open, print an error and abort
Serial.print("Error opening ");
Serial.print(filename);
Serial.println("!");
Serial.println("Aborting!");
Serial.println("----------");
return; //Aborts program if file could not be opened
}
//Read from file until there's nothing else in it:
while (xyValues.available())
{
//Read next X, Y waypoints
//Covert to integers
noXSteps = xyValues.parseInt();
noYSteps = xyValues.parseInt();
//Scale no of steps based on desired image size
noXSteps=noXSteps*scalingFactor;
noYSteps=noYSteps*scalingFactor;
Serial.print("x: ");
Serial.print(noXSteps);
Serial.print(" y: ");
Serial.println(noYSteps);
//Rotate motors
rotateMotors(noXSteps, noYSteps);
delay(200);
}
// Close the file:
xyValues.close();
// Drawing complete. Pause for one hour (3600 seconds), i.e. freeze until power off because drawing is done.
Serial.println("Drawing complete!");
Serial.println("Pausing for 1 hour.");
for (int k=0; k<3600; k++)
{
delay(1000);
}
}
void rotateMotors(int noXSteps, int noYSteps)
{
//Initialize while loop counter
int totalSteps=0;
int stepDelay=10;
//Set X-Axis motor rotation direction based on read value
if (noXSteps<0) //Set X-Axis rotation direction CCW
{
digitalWrite(xDir,LOW);
}
else //Set X-Axis rotation direction to CW
{
digitalWrite(xDir,HIGH);
}
//Set Y-Axis motor rotation direction based on read value
if (noYSteps<0) //Set Y-Axis rotation to CCW
{
digitalWrite(yDir,LOW);
}
else
{
digitalWrite(yDir,HIGH);
}
//Calculate total number of steps for while loop indexing
totalSteps=abs(noXSteps)+abs(noYSteps);
//Get absolute value of steps
noXSteps=abs(noXSteps);
noYSteps=abs(noYSteps);
//Move motors appropriate number of steps
while (totalSteps>0)
{
if (noXSteps>0) //Move X-Axis
{
//Move X-Axis one step
digitalWrite(xStep, LOW); //LOW to HIGH changes creates the "Rising Edge" so that the EasyDriver knows when to step.
delay(1);
digitalWrite(xStep, HIGH);
delay(1);
noXSteps=noXSteps-1; //Decrement remaining number of X-Axis steps
totalSteps=totalSteps-1; //Decrement remaining number of total steps
}
if (noYSteps>0) //Move Y-Axis
{
//Move Y-Axis one step
digitalWrite(yStep, LOW); //LOW to HIGH changes creates the "Rising Edge" so that the EasyDriver knows when to step.
delay(1);
digitalWrite(yStep, HIGH);
delay(1);
noYSteps=noYSteps-1; //Decrement remaining number of Y-Axis steps
totalSteps=totalSteps-1; //Decrement remaining number of total steps
}
delay(stepDelay);
}
}