-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tesla_Coil_Winder.ino
170 lines (143 loc) · 3.26 KB
/
Tesla_Coil_Winder.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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); //Adress (found with i2c scanner example code), rows, lines
int rotator[] = {2, 3, 4, 5};
int slider[] = {6, 7, 8, 9};
int motor_list[] = {rotator, slider};
int current_step[5] = {false, false, false, false, false};
const int chip_select = 10;
String filename;
File file;
char data;
String turns;
int microstep_flag;
int step_flag;
int turn_flag;
int last_turn_flag;
int current_turn = 1;
int percent;
void get_coil_data()
{
data = file.read();
if (data != 'E') {
turns += data;
get_coil_data();
}
}
void setup()
{
lcd.begin(16, 2);
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
for (int i = 0; i < 4; i++)
{
pinMode(rotator[i], OUTPUT);
pinMode(slider[i], OUTPUT);
}
Serial.begin(9600);
lcd.print("Initializing SD card...");
lcd.setCursor(0, 1);
if (!SD.begin(10))
{
lcd.print("Initialization failed!");
while (1)
{
lcd.scrollDisplayLeft();
delay(200);
}
}
lcd.print("Done.");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
if (SD.exists("coil.dola")) {
filename = "coil.dola";
}
else if (SD.exists("coil.dol")) {
filename = "coil.dol";
}
else if (SD.exists("coil~1.dola")) {
filename = "coil~1.dola";
}
else if (SD.exists("coil~1.dol")) {
filename = "coil~1.dol";
}
else
{
lcd.print("coil.dola doesn't exist.");
while (1) {
lcd.scrollDisplayLeft();
delay(200);
}
}
if (!SD.open(filename, FILE_READ))
{
lcd.print("File Error");
}
else {
file = SD.open(filename, FILE_READ);
}
get_coil_data();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Turn: ");
lcd.print(current_turn);
lcd.print("/");
lcd.print(turns);
lcd.setCursor(0, 1);
lcd.print("Progress: %0");
}
void motor(int motor[], bool A, bool B, bool C, bool D)
{
digitalWrite(motor[0], A);
digitalWrite(motor[1], B);
digitalWrite(motor[2], C);
digitalWrite(motor[3], D);
}
void loop()
{
if (turn_flag != turns.toInt())
{
while (turn_flag != turns.toInt())
{
last_turn_flag = turn_flag;
for (int i = 0; i < 5; i++)
{
if (file.read() == 48) {
current_step[i] = 0;
}
else {
current_step[i] = 1;
}
}
if (current_step[0] == 0) {
microstep_flag++;
step_flag = microstep_flag / 8;
turn_flag = step_flag / 512;
}
motor(motor_list[current_step[0]], current_step[1], current_step[2], current_step[3], current_step[4]);
delay(1);
if (turn_flag > last_turn_flag) {
current_turn++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Turn: ");
lcd.print(current_turn);
lcd.print("/");
lcd.print(turns);
lcd.setCursor(0, 1);
lcd.print("Progress: ");
lcd.print("%");
percent = map(current_turn, 0, turns.toInt(), 0, 100);
lcd.print(percent);
}
if (turn_flag == turns.toInt()) {
break;
}
}
}
}