-
Notifications
You must be signed in to change notification settings - Fork 0
/
magnetorquer_winder_arduino.ino
97 lines (75 loc) · 2.49 KB
/
magnetorquer_winder_arduino.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
/* HSL Magnetorquer Winder Testbench
* Outputs pulses to stepper motor driver circuit.
* Motor speed controlled by potentiometer and progress
* printed on LCD display.
*/
#include <LiquidCrystal.h>
#include <math.h>
// parameters to define
const int stepsPerRevolution = 200; // motor parameter
const int stepRatio = 3; // number of rotational steps to horizontal steps
const long totalSteps = 390400; // total number of rotational steps
const long stepsPerLayer = 8000; // horizontal steps before reversing direction
// setup
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // setup lcd display
const int rotStep = 10;
const int horizStep = 9;
const int dir = 6;
const int motorOn = 13;
long steps = 0;
long layerSteps = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// reads potentiometer input and converts to motor speed
int speed() {
int potInput = analogRead(A0);
return (int) (3000 - (2800/1023)*potInput);
}
int delayValue;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
digitalWrite(7, HIGH);
lcd.print("Progress:");
// set up 'PWM' output to stepper driver
pinMode(rotStep,OUTPUT); // rotational (main) step
pinMode(horizStep,OUTPUT); // horizontal (geared) step
pinMode(dir,OUTPUT); // shared direction
pinMode(motorOn,INPUT); // switch to activate winding
digitalWrite(dir,HIGH); // may need to change
}
void loop() {
// at end of solenoid, reverse direction of horizontal stepper motor
if (layerSteps >= stepsPerLayer) {
layerSteps = 0;
digitalWrite(dir, !digitalRead(dir));
}
// fetch speed from potentiometer input
delayValue = speed();
if ((steps < totalSteps) && (digitalRead(motorOn) == HIGH)) {
// <stepRatio> rotational steps
for (int i = 0; i < stepRatio; i++) {
digitalWrite(rotStep,HIGH);
delayMicroseconds(delayValue);
digitalWrite(rotStep,LOW);
delayMicroseconds(delayValue);
}
// one horizontal / translational step
digitalWrite(horizStep,HIGH);
delayMicroseconds(delayValue);
digitalWrite(rotStep,LOW);
delayMicroseconds(delayValue);
layerSteps++;
steps += stepRatio;
}
lcd.setCursor(0, 1);
float progress = 100.0*steps / totalSteps;
lcd.print(progress);
lcd.write("%");
// display motor direction (f or r)
lcd.setCursor(15,0);
if (digitalRead(dir)) {
lcd.write("F");
} else {
lcd.write("R");
}
}