Components and supplies
Arduino UNO
Tools and machines
Crimp Tool, Heavy-Duty
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Project description
Code
leadScrewV1
arduino
1// included libraries 2// liquid crystal l2c library and lcd function 3#include <LiquidCrystal_I2C.h> 4LiquidCrystal_I2C lcd(0x27, 20, 4); 5#include <AccelStepper.h> 6AccelStepper stepper(AccelStepper::DRIVER,10,9); 7// Speed sensor variables 8const int SensorPin = 2; // the number of the IR sensor input pin 9int sensorState; // the current state from the input pin 10int lastSensorState = LOW; // the previous InputState from the input pin 11long lastDebounceTime = 0.00; // the last time the output pin was toggled 12long debounceDelay = 5.00; // the debounce time; increase if the output flickers 13long time; 14long endTime; 15long startTime; 16volatile float RPM = 0.00; 17float stepperRPM = 0.00; 18float lnTime = 0.00; 19int lcdRPM = 0; 20int lastRPM = 0; 21 22//button variables 23const int n = 2; 24const int buttonPin[n] = {3,4}; 25int buttonState[n] = {LOW,LOW}; 26int lastButtonState[n]= {LOW,LOW}; 27bool buttonFlags[n] = {LOW, LOW}; 28 29//three way switch variables 30const int leftPin = 6; 31const int rightPin = 5; 32 33//screen variables 34const int x = 2; 35int currentX = 0; 36int currentY = 0; 37String screens[x][5] = {{"TPI","Dir","LatheRPM ","ServoRPM","tpi/mm"}, {"Pitch","Dir","LatheRPM","ServoRPM","tpi/mm"}}; 38 39//user input variables 40const int nTPI = 18; 41const int nPitch = 11; 42int tpi[nTPI] = {256, 128, 64, 56, 48, 40, 32, 24, 20, 18, 16, 14, 13, 12, 11, 10, 9, 8}; 43int currentTPI = 0; 44float pitch[nPitch]={0.40, 0.45, 0.50, 0.70, 0.70, 0.80, 1.00, 1.25, 1.50, 2.00, 2.50}; 45int currentPitch = 0; 46//String dir[2] = {"CW ","CCW"}; 47//String currentDir = "CW"; 48 49//internal timer to send data 50unsigned long prevMillis = 0; 51const long interval = 1000; 52 53//lathe parameters 54float servoRPM = 0.00; 55//int direction = 1; 56 57// =========================== setup ======================================== 58void setup() 59{ 60 lcd.init(); 61 lcd.backlight(); 62 lcd.begin(20,4); 63 Serial.begin(9600); 64 for(int i=0; i < n; i++) 65 { 66 pinMode(buttonPin[i], INPUT); 67 //Serial.print("Button: "); 68 //Serial.println(buttonPin[i]); 69 } 70 pinMode(SensorPin, INPUT); 71 endTime = 0; 72 printScreen(); 73 // stepper stuff 74 stepper.setEnablePin(8); 75 stepper.disableOutputs(); 76 stepper.setMaxSpeed(2502); 77 stepper.setAcceleration(300); 78} 79// =========================== main loop ==================================== 80void loop() 81{ 82 setButtonFlags(); 83 resolveButtonFlags(); 84 sendData(); 85 speedSensor(); 86 if (digitalRead(leftPin) == HIGH) 87 { 88 //direction = 1; 89 lcd.setCursor(4,1); 90 lcd.print("CW "); 91 lcd.setCursor(9,3); 92 lcd.println(servoRPM); 93 lcd.rightToLeft(); 94 lcd.print(" "); 95 lcd.leftToRight(); 96 lcd.setCursor(9,2); 97 lcd.println(lcdRPM); 98 lcd.rightToLeft(); 99 lcd.print(" "); 100 lcd.leftToRight(); 101 delay(5); 102 stepper.enableOutputs(); 103 stepper.setSpeed(stepperRPM); 104 stepper.run(); 105 while(stepper.isRunning()) 106 { 107 speedSensor(); 108 stepper.setSpeed(stepperRPM); 109 stepper.run(); 110 if(digitalRead(leftPin)==LOW) 111 { 112 stepper.disableOutputs(); 113 lcd.setCursor(4,1); 114 lcd.print("OFF"); 115 break; 116 } 117 } 118 } 119 else if (digitalRead(rightPin) == HIGH) 120 { 121 //direction = -1.00; 122 lcd.setCursor(4,1); 123 lcd.print("CCW"); 124 lcd.setCursor(9,3); 125 lcd.println(servoRPM); 126 lcd.rightToLeft(); 127 lcd.print(" "); 128 lcd.leftToRight(); 129 lcd.setCursor(9,2); 130 lcd.println(lcdRPM); 131 lcd.rightToLeft(); 132 lcd.print(" "); 133 lcd.leftToRight(); 134 delay(5); 135 stepper.enableOutputs(); 136 stepper.setSpeed(-stepperRPM); 137 stepper.run(); 138 while(stepper.isRunning()) 139 { 140 speedSensor(); 141 stepper.setSpeed(-1*stepperRPM); 142 stepper.run(); 143 if(digitalRead(rightPin)==LOW) 144 { 145 stepper.disableOutputs(); 146 lcd.setCursor(4,1); 147 lcd.print("OFF"); 148 break; 149 } 150 } 151 } 152} 153// =========================== set input flags =========================== 154void setButtonFlags() 155{ 156 for(int i=0; i < n; i++) 157 { 158 buttonState[i] = digitalRead(buttonPin[i]); 159 delay[1]; 160 if(buttonState[i] != lastButtonState[i]) 161 { 162 if(buttonState[i] == HIGH) 163 { 164 //Serial.print("Button: "); 165 //Serial.println(buttonPin[i]); 166 buttonFlags[i] = HIGH; 167 } 168 } 169 lastButtonState[i] = buttonState[i]; 170 } 171} 172// =========================== resolve button flags ========================= 173void resolveButtonFlags() 174{ 175 for(int i = 0; i < n; i++) 176 { 177 if(buttonFlags[i] == HIGH) 178 { 179 buttonAction(i); 180 buttonFlags[i] = LOW; 181 printScreen(); 182 } 183 } 184} 185// =========================== button action =============================== 186void buttonAction(int button) 187{ 188 if (button == 0) 189 { 190 if (currentX == x-1) 191 { 192 currentX = 0; 193 } 194 else 195 { 196 currentX++; 197 } 198 } 199 if (button == 1) 200 { 201 if (currentX == 0) 202 { 203 if(currentTPI == nTPI-1) 204 { 205 currentTPI = 0; 206 } 207 else 208 { 209 currentTPI++; 210 } 211 } 212 else if (currentX == 1) 213 { 214 if(currentPitch == nPitch-1) 215 { 216 currentPitch = 0; 217 } 218 else 219 { 220 currentPitch++; 221 } 222 } 223 } 224} 225// =========================== print screen =========================== 226void printScreen() 227{ 228 lcd.clear(); 229 lcd.print(screens[currentX][0]); 230 lcd.setCursor(0,1); 231 lcd.print(screens[currentX][1]); 232 lcd.setCursor(0,2); 233 lcd.print(screens[currentX][2]); 234 lcd.setCursor(0,3); 235 lcd.print(screens[currentX][3]); 236 lcd.setCursor(14,0); 237 lcd.print(screens[currentX][4]); 238 if(currentX == 0) 239 { 240 lcd.setCursor(4,0); 241 lcd.println(tpi[currentTPI]); 242 lcd.rightToLeft(); 243 lcd.print(" "); 244 lcd.leftToRight(); 245 lcd.setCursor(4,1); 246 lcd.print("OFF"); 247 } 248 else if (currentX == 1) 249 { 250 lcd.setCursor(6,0); 251 lcd.println(pitch[currentPitch]); 252 lcd.rightToLeft(); 253 lcd.print(" "); 254 lcd.leftToRight(); 255 lcd.setCursor(4,1); 256 lcd.print("OFF"); 257 } 258} 259// =========================== Speed Sensor loop =========================== 260void speedSensor() 261{ 262 time = millis(); 263 int currentSensorState = digitalRead(SensorPin); 264 265 if (currentSensorState != lastSensorState) 266 { 267 lastDebounceTime = millis(); 268 } 269 270 if ((millis() - lastDebounceTime) > debounceDelay) 271 { 272 if (currentSensorState != sensorState) 273 { 274 sensorState = currentSensorState; 275 if (sensorState == LOW) 276 { 277 calculateRPM(); // Real RPM from sensor 278 if (currentX == 0) 279 { 280 englishCalc(); 281 } 282 else if (currentX == 1) 283 { 284 metricCalc(); 285 } 286 } 287 } 288 } 289 lastSensorState = currentSensorState; 290 lcdRPM = RPM; 291} 292 293// =========================== RPM calculation =========================== 294void calculateRPM() 295{ 296 startTime = lastDebounceTime; 297 lnTime = startTime - endTime; 298 RPM = 60000.00 / (startTime - endTime); 299 endTime = startTime; 300} 301// =========================== send speed data =========================== 302void sendData() 303{ 304 unsigned long currentMillis = millis(); 305 if(currentMillis - prevMillis >= interval) 306 { 307 prevMillis = currentMillis; 308 lcd.setCursor(9,2); 309 lcd.println(lcdRPM); 310 lcd.rightToLeft(); 311 lcd.print(" "); 312 lcd.leftToRight(); 313 lcd.setCursor(9,3); 314 lcd.println(servoRPM); 315 lcd.rightToLeft(); 316 lcd.print(" "); 317 lcd.leftToRight(); 318 lcdRPM = 0; 319 } 320} 321// =========================== english stepper speed ======================== 322void metricCalc() 323{ 324 stepperRPM = 1.529 * 0.315 * 400 * RPM * pitch[currentPitch] / 60.00; 325 servoRPM = 0.482 * RPM * pitch[currentPitch]; 326} 327// =========================== metric stepper speed ========================= 328void englishCalc() 329{ 330 stepperRPM = 1.529 * 8.00 * 400.00 * RPM / ( tpi[currentTPI] * 60.00 ); 331 servoRPM = 1.529 * 8.00 * RPM / tpi[currentTPI]; 332}
LeadScrewV2
arduino
With rapid mode
1// included libraries 2// liquid crystal l2c library and lcd function 3#include <LiquidCrystal_I2C.h> 4LiquidCrystal_I2C lcd(0x27, 20, 4); 5#include <AccelStepper.h> 6AccelStepper stepper(AccelStepper::DRIVER,10,9); 7// Speed sensor variables 8const int SensorPin = 2; // the number of the IR sensor input pin 9int sensorState; // the current state from the input pin 10int lastSensorState = LOW; // the previous InputState from the input pin 11long lastDebounceTime = 0.00; // the last time the output pin was toggled 12long debounceDelay = 1.00; // the debounce time; increase if the output flickers 13long time; 14long endTime; 15long startTime; 16volatile float RPM = 0.00; 17float stepperRPM = 0.00; 18float lnTime = 0.00; 19int lcdRPM = 0; 20int lastRPM = 0; 21 22//button variables 23const int n = 2; 24const int buttonPin[n] = {3,4}; 25int buttonState[n] = {LOW,LOW}; 26int lastButtonState[n]= {LOW,LOW}; 27bool buttonFlags[n] = {LOW, LOW}; 28 29//three way switch variables 30const int leftPin = 6; 31const int rightPin = 5; 32 33//screen variables 34const int x = 2; 35int currentX = 0; 36int currentY = 0; 37String screens[x][5] = {{"TPI","Dir","LatheRPM ","ServoRPM","tpi/mm"}, {"Pitch","Dir","LatheRPM","ServoRPM","tpi/mm"}}; 38 39//user input variables 40const int nTPI = 18; 41const int nPitch = 11; 42int tpi[nTPI] = {256, 128, 64, 56, 48, 40, 32, 24, 20, 18, 16, 14, 13, 12, 11, 10, 9, 8}; 43int currentTPI = 0; 44float pitch[nPitch]={0.40, 0.45, 0.50, 0.70, 0.70, 0.80, 1.00, 1.25, 1.50, 2.00, 2.50}; 45int currentPitch = 0; 46//String dir[2] = {"CW ","CCW"}; 47//String currentDir = "CW"; 48 49//internal timer to send data 50unsigned long prevMillis = 0; 51const long interval = 1000; 52 53//lathe parameters 54float servoRPM = 0.00; 55//int direction = 1; 56 57// =========================== setup =========================== 58void setup() 59{ 60 lcd.init(); 61 lcd.backlight(); 62 lcd.begin(20,4); 63 Serial.begin(9600); 64 for(int i=0; i < n; i++) 65 { 66 pinMode(buttonPin[i], INPUT); 67 //Serial.print("Button: "); 68 //Serial.println(buttonPin[i]); 69 } 70 pinMode(SensorPin, INPUT); 71 endTime = 0; 72 printScreen(); 73 // stepper stuff 74 stepper.setEnablePin(8); 75 stepper.disableOutputs(); 76 stepper.setMaxSpeed(2250); 77 stepper.setAcceleration(300); 78} 79// =========================== main loop =========================== 80void loop() 81{ 82 setButtonFlags(); 83 resolveButtonFlags(); 84 sendData(); 85 speedSensor(); 86 if (digitalRead(leftPin) == HIGH) 87 { 88 //direction = 1; 89 lcd.setCursor(4,1); 90 lcd.print("CW "); 91 lcd.setCursor(9,3); 92 lcd.println(servoRPM); 93 lcd.rightToLeft(); 94 lcd.print(" "); 95 lcd.leftToRight(); 96 lcd.setCursor(9,2); 97 lcd.println(lcdRPM); 98 lcd.rightToLeft(); 99 lcd.print(" "); 100 lcd.leftToRight(); 101 delay(5); 102 stepper.enableOutputs(); 103 stepper.setSpeed(stepperRPM); 104 stepper.run(); 105 while(stepper.isRunning()) 106 { 107 speedSensor(); 108 stepper.setSpeed(stepperRPM); 109 stepper.run(); 110 if(digitalRead(buttonPin[1]) == HIGH) 111 { 112 stepper.setSpeed(-1500); 113 stepper.run(); 114 } 115 if(digitalRead(leftPin)==LOW) 116 { 117 stepper.disableOutputs(); 118 lcd.setCursor(4,1); 119 lcd.print("OFF"); 120 break; 121 } 122 } 123 } 124 else if (digitalRead(rightPin) == HIGH) 125 { 126 //direction = -1.00; 127 lcd.setCursor(4,1); 128 lcd.print("CCW"); 129 lcd.setCursor(9,3); 130 lcd.println(servoRPM); 131 lcd.rightToLeft(); 132 lcd.print(" "); 133 lcd.leftToRight(); 134 lcd.setCursor(9,2); 135 lcd.println(lcdRPM); 136 lcd.rightToLeft(); 137 lcd.print(" "); 138 lcd.leftToRight(); 139 delay(5); 140 stepper.enableOutputs(); 141 stepper.setSpeed(-stepperRPM); 142 stepper.run(); 143 while(stepper.isRunning()) 144 { 145 speedSensor(); 146 stepper.setSpeed(-1*stepperRPM); 147 stepper.run(); 148 if(digitalRead(buttonPin[1]) == HIGH) 149 { 150 stepper.setSpeed(1500); 151 stepper.run(); 152 } 153 if(digitalRead(rightPin)==LOW) 154 { 155 stepper.disableOutputs(); 156 lcd.setCursor(4,1); 157 lcd.print("OFF"); 158 break; 159 } 160 } 161 } 162} 163// =========================== set input flags =========================== 164void setButtonFlags() 165{ 166 for(int i=0; i < n; i++) 167 { 168 buttonState[i] = digitalRead(buttonPin[i]); 169 delay[1]; 170 if(buttonState[i] != lastButtonState[i]) 171 { 172 if(buttonState[i] == HIGH) 173 { 174 //Serial.print("Button: "); 175 //Serial.println(buttonPin[i]); 176 buttonFlags[i] = HIGH; 177 } 178 } 179 lastButtonState[i] = buttonState[i]; 180 } 181} 182// =========================== resolve button flags =========================== 183void resolveButtonFlags() 184{ 185 for(int i = 0; i < n; i++) 186 { 187 if(buttonFlags[i] == HIGH) 188 { 189 buttonAction(i); 190 buttonFlags[i] = LOW; 191 printScreen(); 192 } 193 } 194} 195// =========================== button action =========================== 196void buttonAction(int button) 197{ 198 if (button == 0) 199 { 200 if (currentX == x-1) 201 { 202 currentX = 0; 203 } 204 else 205 { 206 currentX++; 207 } 208 } 209 if (button == 1) 210 { 211 if (currentX == 0) 212 { 213 if(currentTPI == nTPI-1) 214 { 215 currentTPI = 0; 216 } 217 else 218 { 219 currentTPI++; 220 } 221 } 222 else if (currentX == 1) 223 { 224 if(currentPitch == nPitch-1) 225 { 226 currentPitch = 0; 227 } 228 else 229 { 230 currentPitch++; 231 } 232 } 233 } 234} 235// =========================== print screen =========================== 236void printScreen() 237{ 238 lcd.clear(); 239 lcd.print(screens[currentX][0]); 240 lcd.setCursor(0,1); 241 lcd.print(screens[currentX][1]); 242 lcd.setCursor(0,2); 243 lcd.print(screens[currentX][2]); 244 lcd.setCursor(0,3); 245 lcd.print(screens[currentX][3]); 246 lcd.setCursor(14,0); 247 lcd.print(screens[currentX][4]); 248 if(currentX == 0) 249 { 250 lcd.setCursor(4,0); 251 lcd.println(tpi[currentTPI]); 252 lcd.rightToLeft(); 253 lcd.print(" "); 254 lcd.leftToRight(); 255 lcd.setCursor(4,1); 256 lcd.print("OFF"); 257 } 258 else if (currentX == 1) 259 { 260 lcd.setCursor(6,0); 261 lcd.println(pitch[currentPitch]); 262 lcd.rightToLeft(); 263 lcd.print(" "); 264 lcd.leftToRight(); 265 lcd.setCursor(4,1); 266 lcd.print("OFF"); 267 } 268} 269// =========================== Speed Sensor loop =========================== 270void speedSensor() 271{ 272 time = millis(); 273 int currentSensorState = digitalRead(SensorPin); 274 275 if (currentSensorState != lastSensorState) 276 { 277 lastDebounceTime = millis(); 278 } 279 280 if ((millis() - lastDebounceTime) > debounceDelay) 281 { 282 if (currentSensorState != sensorState) 283 { 284 sensorState = currentSensorState; 285 if (sensorState == LOW) 286 { 287 calculateRPM(); // Real RPM from sensor 288 if (currentX == 0) 289 { 290 englishCalc(); 291 } 292 else if (currentX == 1) 293 { 294 metricCalc(); 295 } 296 } 297 } 298 } 299 lastSensorState = currentSensorState; 300 lcdRPM = RPM; 301} 302 303// =========================== RPM calculation =========================== 304void calculateRPM() 305{ 306 startTime = lastDebounceTime; 307 lnTime = startTime - endTime; 308 RPM = 60000.00 / (startTime - endTime); 309 endTime = startTime; 310} 311// =========================== send speed data =========================== 312void sendData() 313{ 314 unsigned long currentMillis = millis(); 315 if(currentMillis - prevMillis >= interval) 316 { 317 prevMillis = currentMillis; 318 lcd.setCursor(9,2); 319 lcd.println(lcdRPM); 320 lcd.rightToLeft(); 321 lcd.print(" "); 322 lcd.leftToRight(); 323 lcd.setCursor(9,3); 324 lcd.println(servoRPM); 325 lcd.rightToLeft(); 326 lcd.print(" "); 327 lcd.leftToRight(); 328 lcdRPM = 0; 329 } 330} 331// =========================== english stepper speed =========================== 332void metricCalc() 333{ 334 stepperRPM = 1.529 * 0.315 * 400 * RPM * pitch[currentPitch] / 60.00; 335 servoRPM = 0.482 * RPM * pitch[currentPitch]; 336} 337// =========================== metric stepper speed =========================== 338void englishCalc() 339{ 340 stepperRPM = 1.529 * 8.00 * 400.00 * RPM / ( tpi[currentTPI] * 60.00 ); 341 servoRPM = 1.529 * 8.00 * RPM / tpi[currentTPI]; 342}
LeadScrewV2
arduino
With rapid mode
1// included libraries 2// liquid crystal l2c library and lcd function 3#include 4 <LiquidCrystal_I2C.h> 5LiquidCrystal_I2C lcd(0x27, 20, 4); 6#include <AccelStepper.h> 7AccelStepper 8 stepper(AccelStepper::DRIVER,10,9); 9// Speed sensor variables 10const int SensorPin 11 = 2; // the number of the IR sensor input pin 12int sensorState; // 13 the current state from the input pin 14int lastSensorState = LOW; // the previous 15 InputState from the input pin 16long lastDebounceTime = 0.00; // the last time 17 the output pin was toggled 18long debounceDelay = 1.00; // the debounce time; 19 increase if the output flickers 20long time; 21long endTime; 22long startTime; 23volatile 24 float RPM = 0.00; 25float stepperRPM = 0.00; 26float lnTime = 0.00; 27int lcdRPM 28 = 0; 29int lastRPM = 0; 30 31//button variables 32const int n = 2; 33const 34 int buttonPin[n] = {3,4}; 35int buttonState[n] = {LOW,LOW}; 36int lastButtonState[n]= 37 {LOW,LOW}; 38bool buttonFlags[n] = {LOW, LOW}; 39 40//three way switch variables 41const 42 int leftPin = 6; 43const int rightPin = 5; 44 45//screen variables 46const int 47 x = 2; 48int currentX = 0; 49int currentY = 0; 50String screens[x][5] = {{"TPI","Dir","LatheRPM 51 ","ServoRPM","tpi/mm"}, {"Pitch","Dir","LatheRPM","ServoRPM","tpi/mm"}}; 52 53//user 54 input variables 55const int nTPI = 18; 56const int nPitch = 11; 57int tpi[nTPI] 58 = {256, 128, 64, 56, 48, 40, 32, 24, 20, 18, 16, 14, 13, 12, 11, 10, 9, 8}; 59int 60 currentTPI = 0; 61float pitch[nPitch]={0.40, 0.45, 0.50, 0.70, 0.70, 0.80, 1.00, 62 1.25, 1.50, 2.00, 2.50}; 63int currentPitch = 0; 64//String dir[2] = {"CW ","CCW"}; 65//String 66 currentDir = "CW"; 67 68//internal timer to send data 69unsigned long prevMillis 70 = 0; 71const long interval = 1000; 72 73//lathe parameters 74float servoRPM 75 = 0.00; 76//int direction = 1; 77 78// =========================== setup =========================== 79void 80 setup() 81{ 82 lcd.init(); 83 lcd.backlight(); 84 lcd.begin(20,4); 85 Serial.begin(9600); 86 87 for(int i=0; i < n; i++) 88 { 89 pinMode(buttonPin[i], INPUT); 90 //Serial.print("Button: 91 "); 92 //Serial.println(buttonPin[i]); 93 } 94 pinMode(SensorPin, INPUT); 95 96 endTime = 0; 97 printScreen(); 98 // stepper stuff 99 stepper.setEnablePin(8); 100 101 stepper.disableOutputs(); 102 stepper.setMaxSpeed(2250); 103 stepper.setAcceleration(300); 104} 105// 106 =========================== main loop =========================== 107void loop() 108{ 109 110 setButtonFlags(); 111 resolveButtonFlags(); 112 sendData(); 113 speedSensor(); 114 115 if (digitalRead(leftPin) == HIGH) 116 { 117 //direction = 1; 118 lcd.setCursor(4,1); 119 120 lcd.print("CW "); 121 lcd.setCursor(9,3); 122 lcd.println(servoRPM); 123 124 lcd.rightToLeft(); 125 lcd.print(" "); 126 lcd.leftToRight(); 127 128 lcd.setCursor(9,2); 129 lcd.println(lcdRPM); 130 lcd.rightToLeft(); 131 132 lcd.print(" "); 133 lcd.leftToRight(); 134 delay(5); 135 stepper.enableOutputs(); 136 137 stepper.setSpeed(stepperRPM); 138 stepper.run(); 139 while(stepper.isRunning()) 140 141 { 142 speedSensor(); 143 stepper.setSpeed(stepperRPM); 144 stepper.run(); 145 146 if(digitalRead(buttonPin[1]) == HIGH) 147 { 148 stepper.setSpeed(-1500); 149 150 stepper.run(); 151 } 152 if(digitalRead(leftPin)==LOW) 153 { 154 155 stepper.disableOutputs(); 156 lcd.setCursor(4,1); 157 lcd.print("OFF"); 158 159 break; 160 } 161 } 162 } 163 else if (digitalRead(rightPin) == 164 HIGH) 165 { 166 //direction = -1.00; 167 lcd.setCursor(4,1); 168 lcd.print("CCW"); 169 170 lcd.setCursor(9,3); 171 lcd.println(servoRPM); 172 lcd.rightToLeft(); 173 174 lcd.print(" "); 175 lcd.leftToRight(); 176 lcd.setCursor(9,2); 177 178 lcd.println(lcdRPM); 179 lcd.rightToLeft(); 180 lcd.print(" "); 181 182 lcd.leftToRight(); 183 delay(5); 184 stepper.enableOutputs(); 185 stepper.setSpeed(-stepperRPM); 186 187 stepper.run(); 188 while(stepper.isRunning()) 189 { 190 speedSensor(); 191 192 stepper.setSpeed(-1*stepperRPM); 193 stepper.run(); 194 if(digitalRead(buttonPin[1]) 195 == HIGH) 196 { 197 stepper.setSpeed(1500); 198 stepper.run(); 199 200 } 201 if(digitalRead(rightPin)==LOW) 202 { 203 stepper.disableOutputs(); 204 205 lcd.setCursor(4,1); 206 lcd.print("OFF"); 207 break; 208 209 } 210 } 211 } 212} 213// =========================== set input flags =========================== 214void 215 setButtonFlags() 216{ 217 for(int i=0; i < n; i++) 218 { 219 buttonState[i] 220 = digitalRead(buttonPin[i]); 221 delay[1]; 222 if(buttonState[i] != lastButtonState[i]) 223 224 { 225 if(buttonState[i] == HIGH) 226 { 227 //Serial.print("Button: 228 "); 229 //Serial.println(buttonPin[i]); 230 buttonFlags[i] = HIGH; 231 232 } 233 } 234 lastButtonState[i] = buttonState[i]; 235 } 236} 237// =========================== 238 resolve button flags =========================== 239void resolveButtonFlags() 240{ 241 242 for(int i = 0; i < n; i++) 243 { 244 if(buttonFlags[i] == HIGH) 245 { 246 247 buttonAction(i); 248 buttonFlags[i] = LOW; 249 printScreen(); 250 251 } 252 } 253} 254// =========================== button action =========================== 255void 256 buttonAction(int button) 257{ 258 if (button == 0) 259 { 260 if (currentX 261 == x-1) 262 { 263 currentX = 0; 264 } 265 else 266 { 267 currentX++; 268 269 } 270 } 271 if (button == 1) 272 { 273 if (currentX == 0) 274 { 275 276 if(currentTPI == nTPI-1) 277 { 278 currentTPI = 0; 279 } 280 281 else 282 { 283 currentTPI++; 284 } 285 } 286 else if 287 (currentX == 1) 288 { 289 if(currentPitch == nPitch-1) 290 { 291 currentPitch 292 = 0; 293 } 294 else 295 { 296 currentPitch++; 297 } 298 299 } 300 } 301} 302// =========================== print screen =========================== 303void 304 printScreen() 305{ 306 lcd.clear(); 307 lcd.print(screens[currentX][0]); 308 309 lcd.setCursor(0,1); 310 lcd.print(screens[currentX][1]); 311 lcd.setCursor(0,2); 312 313 lcd.print(screens[currentX][2]); 314 lcd.setCursor(0,3); 315 lcd.print(screens[currentX][3]); 316 317 lcd.setCursor(14,0); 318 lcd.print(screens[currentX][4]); 319 if(currentX == 320 0) 321 { 322 lcd.setCursor(4,0); 323 lcd.println(tpi[currentTPI]); 324 325 lcd.rightToLeft(); 326 lcd.print(" "); 327 lcd.leftToRight(); 328 329 lcd.setCursor(4,1); 330 lcd.print("OFF"); 331 } 332 else if (currentX 333 == 1) 334 { 335 lcd.setCursor(6,0); 336 lcd.println(pitch[currentPitch]); 337 338 lcd.rightToLeft(); 339 lcd.print(" "); 340 lcd.leftToRight(); 341 342 lcd.setCursor(4,1); 343 lcd.print("OFF"); 344 } 345} 346// =========================== 347 Speed Sensor loop =========================== 348void speedSensor() 349{ 350 time 351 = millis(); 352 int currentSensorState = digitalRead(SensorPin); 353 354 if (currentSensorState 355 != lastSensorState) 356 { 357 lastDebounceTime = millis(); 358 } 359 360 if 361 ((millis() - lastDebounceTime) > debounceDelay) 362 { 363 if (currentSensorState 364 != sensorState) 365 { 366 sensorState = currentSensorState; 367 if 368 (sensorState == LOW) 369 { 370 calculateRPM(); // Real RPM from sensor 371 372 if (currentX == 0) 373 { 374 englishCalc(); 375 } 376 377 else if (currentX == 1) 378 { 379 metricCalc(); 380 } 381 382 } 383 } 384 } 385 lastSensorState = currentSensorState; 386 lcdRPM = 387 RPM; 388} 389 390// =========================== RPM calculation =========================== 391void 392 calculateRPM() 393{ 394 startTime = lastDebounceTime; 395 lnTime = startTime - 396 endTime; 397 RPM = 60000.00 / (startTime - endTime); 398 endTime = startTime; 399} 400// 401 =========================== send speed data =========================== 402void 403 sendData() 404{ 405 unsigned long currentMillis = millis(); 406 if(currentMillis 407 - prevMillis >= interval) 408 { 409 prevMillis = currentMillis; 410 lcd.setCursor(9,2); 411 412 lcd.println(lcdRPM); 413 lcd.rightToLeft(); 414 lcd.print(" "); 415 416 lcd.leftToRight(); 417 lcd.setCursor(9,3); 418 lcd.println(servoRPM); 419 420 lcd.rightToLeft(); 421 lcd.print(" "); 422 lcd.leftToRight(); 423 424 lcdRPM = 0; 425 } 426} 427// =========================== english stepper speed 428 =========================== 429void metricCalc() 430{ 431 stepperRPM = 1.529 432 * 0.315 * 400 * RPM * pitch[currentPitch] / 60.00; 433 servoRPM = 0.482 * RPM 434 * pitch[currentPitch]; 435} 436// =========================== metric stepper speed 437 =========================== 438void englishCalc() 439{ 440 stepperRPM = 1.529 441 * 8.00 * 400.00 * RPM / ( tpi[currentTPI] * 60.00 ); 442 servoRPM = 1.529 * 8.00 443 * RPM / tpi[currentTPI]; 444}
leadScrewV1
arduino
1// included libraries 2// liquid crystal l2c library and lcd function 3#include 4 <LiquidCrystal_I2C.h> 5LiquidCrystal_I2C lcd(0x27, 20, 4); 6#include <AccelStepper.h> 7AccelStepper 8 stepper(AccelStepper::DRIVER,10,9); 9// Speed sensor variables 10const int SensorPin 11 = 2; // the number of the IR sensor input pin 12int sensorState; // 13 the current state from the input pin 14int lastSensorState = LOW; // the previous 15 InputState from the input pin 16long lastDebounceTime = 0.00; // the last time 17 the output pin was toggled 18long debounceDelay = 5.00; // the debounce time; 19 increase if the output flickers 20long time; 21long endTime; 22long startTime; 23volatile 24 float RPM = 0.00; 25float stepperRPM = 0.00; 26float lnTime = 0.00; 27int lcdRPM 28 = 0; 29int lastRPM = 0; 30 31//button variables 32const int n = 2; 33const 34 int buttonPin[n] = {3,4}; 35int buttonState[n] = {LOW,LOW}; 36int lastButtonState[n]= 37 {LOW,LOW}; 38bool buttonFlags[n] = {LOW, LOW}; 39 40//three way switch variables 41const 42 int leftPin = 6; 43const int rightPin = 5; 44 45//screen variables 46const int 47 x = 2; 48int currentX = 0; 49int currentY = 0; 50String screens[x][5] = {{"TPI","Dir","LatheRPM 51 ","ServoRPM","tpi/mm"}, {"Pitch","Dir","LatheRPM","ServoRPM","tpi/mm"}}; 52 53//user 54 input variables 55const int nTPI = 18; 56const int nPitch = 11; 57int tpi[nTPI] 58 = {256, 128, 64, 56, 48, 40, 32, 24, 20, 18, 16, 14, 13, 12, 11, 10, 9, 8}; 59int 60 currentTPI = 0; 61float pitch[nPitch]={0.40, 0.45, 0.50, 0.70, 0.70, 0.80, 1.00, 62 1.25, 1.50, 2.00, 2.50}; 63int currentPitch = 0; 64//String dir[2] = {"CW ","CCW"}; 65//String 66 currentDir = "CW"; 67 68//internal timer to send data 69unsigned long prevMillis 70 = 0; 71const long interval = 1000; 72 73//lathe parameters 74float servoRPM 75 = 0.00; 76//int direction = 1; 77 78// =========================== setup ======================================== 79void 80 setup() 81{ 82 lcd.init(); 83 lcd.backlight(); 84 lcd.begin(20,4); 85 Serial.begin(9600); 86 87 for(int i=0; i < n; i++) 88 { 89 pinMode(buttonPin[i], INPUT); 90 //Serial.print("Button: 91 "); 92 //Serial.println(buttonPin[i]); 93 } 94 pinMode(SensorPin, INPUT); 95 96 endTime = 0; 97 printScreen(); 98 // stepper stuff 99 stepper.setEnablePin(8); 100 101 stepper.disableOutputs(); 102 stepper.setMaxSpeed(2502); 103 stepper.setAcceleration(300); 104} 105// 106 =========================== main loop ==================================== 107void 108 loop() 109{ 110 setButtonFlags(); 111 resolveButtonFlags(); 112 sendData(); 113 114 speedSensor(); 115 if (digitalRead(leftPin) == HIGH) 116 { 117 //direction 118 = 1; 119 lcd.setCursor(4,1); 120 lcd.print("CW "); 121 lcd.setCursor(9,3); 122 123 lcd.println(servoRPM); 124 lcd.rightToLeft(); 125 lcd.print(" "); 126 127 lcd.leftToRight(); 128 lcd.setCursor(9,2); 129 lcd.println(lcdRPM); 130 131 lcd.rightToLeft(); 132 lcd.print(" "); 133 lcd.leftToRight(); 134 135 delay(5); 136 stepper.enableOutputs(); 137 stepper.setSpeed(stepperRPM); 138 139 stepper.run(); 140 while(stepper.isRunning()) 141 { 142 speedSensor(); 143 144 stepper.setSpeed(stepperRPM); 145 stepper.run(); 146 if(digitalRead(leftPin)==LOW) 147 148 { 149 stepper.disableOutputs(); 150 lcd.setCursor(4,1); 151 152 lcd.print("OFF"); 153 break; 154 } 155 } 156 } 157 else 158 if (digitalRead(rightPin) == HIGH) 159 { 160 //direction = -1.00; 161 lcd.setCursor(4,1); 162 163 lcd.print("CCW"); 164 lcd.setCursor(9,3); 165 lcd.println(servoRPM); 166 167 lcd.rightToLeft(); 168 lcd.print(" "); 169 lcd.leftToRight(); 170 171 lcd.setCursor(9,2); 172 lcd.println(lcdRPM); 173 lcd.rightToLeft(); 174 175 lcd.print(" "); 176 lcd.leftToRight(); 177 delay(5); 178 stepper.enableOutputs(); 179 180 stepper.setSpeed(-stepperRPM); 181 stepper.run(); 182 while(stepper.isRunning()) 183 184 { 185 speedSensor(); 186 stepper.setSpeed(-1*stepperRPM); 187 stepper.run(); 188 189 if(digitalRead(rightPin)==LOW) 190 { 191 stepper.disableOutputs(); 192 193 lcd.setCursor(4,1); 194 lcd.print("OFF"); 195 break; 196 197 } 198 } 199 } 200} 201// =========================== set input flags =========================== 202void 203 setButtonFlags() 204{ 205 for(int i=0; i < n; i++) 206 { 207 buttonState[i] 208 = digitalRead(buttonPin[i]); 209 delay[1]; 210 if(buttonState[i] != lastButtonState[i]) 211 212 { 213 if(buttonState[i] == HIGH) 214 { 215 //Serial.print("Button: 216 "); 217 //Serial.println(buttonPin[i]); 218 buttonFlags[i] = HIGH; 219 220 } 221 } 222 lastButtonState[i] = buttonState[i]; 223 } 224} 225// =========================== 226 resolve button flags ========================= 227void resolveButtonFlags() 228{ 229 230 for(int i = 0; i < n; i++) 231 { 232 if(buttonFlags[i] == HIGH) 233 { 234 235 buttonAction(i); 236 buttonFlags[i] = LOW; 237 printScreen(); 238 239 } 240 } 241} 242// =========================== button action =============================== 243void 244 buttonAction(int button) 245{ 246 if (button == 0) 247 { 248 if (currentX 249 == x-1) 250 { 251 currentX = 0; 252 } 253 else 254 { 255 currentX++; 256 257 } 258 } 259 if (button == 1) 260 { 261 if (currentX == 0) 262 { 263 264 if(currentTPI == nTPI-1) 265 { 266 currentTPI = 0; 267 } 268 269 else 270 { 271 currentTPI++; 272 } 273 } 274 else if 275 (currentX == 1) 276 { 277 if(currentPitch == nPitch-1) 278 { 279 currentPitch 280 = 0; 281 } 282 else 283 { 284 currentPitch++; 285 } 286 287 } 288 } 289} 290// =========================== print screen =========================== 291void 292 printScreen() 293{ 294 lcd.clear(); 295 lcd.print(screens[currentX][0]); 296 297 lcd.setCursor(0,1); 298 lcd.print(screens[currentX][1]); 299 lcd.setCursor(0,2); 300 301 lcd.print(screens[currentX][2]); 302 lcd.setCursor(0,3); 303 lcd.print(screens[currentX][3]); 304 305 lcd.setCursor(14,0); 306 lcd.print(screens[currentX][4]); 307 if(currentX == 308 0) 309 { 310 lcd.setCursor(4,0); 311 lcd.println(tpi[currentTPI]); 312 313 lcd.rightToLeft(); 314 lcd.print(" "); 315 lcd.leftToRight(); 316 317 lcd.setCursor(4,1); 318 lcd.print("OFF"); 319 } 320 else if (currentX 321 == 1) 322 { 323 lcd.setCursor(6,0); 324 lcd.println(pitch[currentPitch]); 325 326 lcd.rightToLeft(); 327 lcd.print(" "); 328 lcd.leftToRight(); 329 330 lcd.setCursor(4,1); 331 lcd.print("OFF"); 332 } 333} 334// =========================== 335 Speed Sensor loop =========================== 336void speedSensor() 337{ 338 time 339 = millis(); 340 int currentSensorState = digitalRead(SensorPin); 341 342 if (currentSensorState 343 != lastSensorState) 344 { 345 lastDebounceTime = millis(); 346 } 347 348 if 349 ((millis() - lastDebounceTime) > debounceDelay) 350 { 351 if (currentSensorState 352 != sensorState) 353 { 354 sensorState = currentSensorState; 355 if 356 (sensorState == LOW) 357 { 358 calculateRPM(); // Real RPM from sensor 359 360 if (currentX == 0) 361 { 362 englishCalc(); 363 } 364 365 else if (currentX == 1) 366 { 367 metricCalc(); 368 } 369 370 } 371 } 372 } 373 lastSensorState = currentSensorState; 374 lcdRPM = 375 RPM; 376} 377 378// =========================== RPM calculation =========================== 379void 380 calculateRPM() 381{ 382 startTime = lastDebounceTime; 383 lnTime = startTime - 384 endTime; 385 RPM = 60000.00 / (startTime - endTime); 386 endTime = startTime; 387} 388// 389 =========================== send speed data =========================== 390void 391 sendData() 392{ 393 unsigned long currentMillis = millis(); 394 if(currentMillis 395 - prevMillis >= interval) 396 { 397 prevMillis = currentMillis; 398 lcd.setCursor(9,2); 399 400 lcd.println(lcdRPM); 401 lcd.rightToLeft(); 402 lcd.print(" "); 403 404 lcd.leftToRight(); 405 lcd.setCursor(9,3); 406 lcd.println(servoRPM); 407 408 lcd.rightToLeft(); 409 lcd.print(" "); 410 lcd.leftToRight(); 411 412 lcdRPM = 0; 413 } 414} 415// =========================== english stepper speed 416 ======================== 417void metricCalc() 418{ 419 stepperRPM = 1.529 * 0.315 420 * 400 * RPM * pitch[currentPitch] / 60.00; 421 servoRPM = 0.482 * RPM * pitch[currentPitch]; 422} 423// 424 =========================== metric stepper speed ========================= 425void 426 englishCalc() 427{ 428 stepperRPM = 1.529 * 8.00 * 400.00 * RPM / ( tpi[currentTPI] 429 * 60.00 ); 430 servoRPM = 1.529 * 8.00 * RPM / tpi[currentTPI]; 431}
Downloadable files
Spindle VFD Schematic
Spindle VFD Schematic
Spindle VFD Schematic
Spindle VFD Schematic
Servo Controller Schematic
Servo Controller Schematic
Comments
Only logged in users can leave comments