Code
#include <AccelStepper.h> // for stepper motor control
#include <Wire.h> // for I2C device
#include <Keypad.h> // for keypad (obviously)
#include <LiquidCrystal_I2C.h> // for LCD I2C adaptor
// Define stepper motor connections and motor interface type.
// Motor interface type must be set to 1 when using a driver:
// stepper 1-5 are spools, stepper 6 loads the wire and 7 loads the pins
#define stepPin1 14
#define dirPin1 15
#define enPin1 16
#define stepPin2 17
#define dirPin2 18
#define enPin2 19
#define stepPin3 20
#define dirPin3 21
#define enPin3 22
#define stepPin4 23
#define dirPin4 24
#define enPin4 25
#define stepPin5 26
#define dirPin5 27
#define enPin5 28
#define stepPin6 29
#define dirPin6 30
#define enPin6 31
#define stepPin7 32
#define dirPin7 33
#define enPin7 34
#define stepPin8 32
#define dirPin8 33
#define enPin8 34
#define stepPin7 32
#define dirPin7 33
#define enPin7 34
#define stepPinID1 48
#define dirPinID1 49
#define enPinID1 50
#define stepPinID2 51
#define dirPinID2 52
#define enPinID2 53
#define motorInterfaceType 1
#define sensorPin1 3
#define sensorPin2 4
#define sensorPin3 5
#define sensorPin4 6
#define sensorPin5 7
#define L1 8
#define L2 9
#define functionPin 43
#define stopItPin 44
#define R1 47// crimp solenoid valve pin
#define conversion 450 // ratio of accelstepper units to inches
#define conversionID 148 // 500 steps = 3.37" +-.02 so 148 steps = 1"
int check1 = 0;
// Create a new instance of the AccelStepper class:
AccelStepper stepper1 = AccelStepper(motorInterfaceType, stepPin1, dirPin1);
AccelStepper stepper2 = AccelStepper(motorInterfaceType, stepPin2, dirPin2);
AccelStepper stepper3 = AccelStepper(motorInterfaceType, stepPin3, dirPin3);
AccelStepper stepper4 = AccelStepper(motorInterfaceType, stepPin4, dirPin4);
AccelStepper stepper5 = AccelStepper(motorInterfaceType, stepPin5, dirPin5);
AccelStepper stepper6 = AccelStepper(motorInterfaceType, stepPin6, dirPin6);
AccelStepper stepper7 = AccelStepper(motorInterfaceType, stepPin7, dirPin7);
AccelStepper stepperID1 = AccelStepper(motorInterfaceType, stepPinID1, dirPinID1);
AccelStepper stepperID2 = AccelStepper(motorInterfaceType, stepPinID2, dirPinID2);
// Define the pins connected to the keypad
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {38, 37, 36, 35};
byte colPins[COLS] = {42, 41, 40, 39};
// Create the Keypad object
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 20, 2);
void loadWires(){
// Run rear motor (ID1) to load wire into place
digitalWrite(enPinID1, LOW);
stepperID1.moveTo(5.6*conversionID);
stepperID1.runToPosition();
stepperID1.setCurrentPosition(0);
digitalWrite(enPinID1, HIGH);
}
void dispelWires(){
// run rear motor (ID1) to respool wire
digitalWrite(enPinID1, LOW);
stepperID1.moveTo(-7*conversionID);
stepperID1.runToPosition();
stepperID1.setCurrentPosition(0);
digitalWrite(enPinID1, HIGH);
// run front motor (ID2) to get rid of wire
digitalWrite(enPinID2, LOW);
stepperID2.moveTo(3000);
stepperID2.runToPosition();
stepperID2.setCurrentPosition(0);
digitalWrite(enPinID2, HIGH);
}
void loadPins(){
digitalWrite(enPin7, LOW); // activate motor
stepper7.moveTo(400/6); // rotate 1/6 of a rotation (400 steps/)
stepper7.runToPosition();
digitalWrite(enPin7,HIGH); // disable stepper
}
float readDecimalNumber() {
// lcd.print("Set Length"); // SET LENGTH OF WIRE
Serial.println("Set Length");
String number = "";
bool decimalEntered = false;
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '3') { // If '*' is pressed, it represents the decimal point
if (!decimalEntered) { // Ensure only one decimal point is entered
number += '.';
decimalEntered = true;
}
} else if (key == '#') { // If '#' is pressed, exit the loop
break;
} else { // If any other key is pressed, add it to the number
number += key;
}
}
}
Serial.println(number);
float output = number.toFloat();
float unspoolLength = output*conversion;
return unspoolLength;
}
void sixthRotation() {
// Move 1/6 of a revolution
digitalWrite(enPin7, LOW); // activate motor
stepper7.moveTo(533);
stepper7.runToPosition();
digitalWrite(enPin7,HIGH); // disable stepper
}
void function() {
// press the function button
digitalWrite(functionPin, LOW);
delay(1500);
digitalWrite(functionPin, HIGH);
}
void respool() {
digitalWrite(dirPin1,LOW);
while(digitalRead(sensorPin1) == 1){
digitalWrite(stepPin1,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin1,LOW);
delayMicroseconds(500);
}
stepper1.setCurrentPosition(0);
digitalWrite(dirPin2,LOW);
while(digitalRead(sensorPin2) == 1){
digitalWrite(stepPin2,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin2,LOW);
delayMicroseconds(500);
}
stepper2.setCurrentPosition(0);
digitalWrite(dirPin3,LOW);
while(digitalRead(sensorPin3) == 1){
digitalWrite(stepPin3,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin3,LOW);
delayMicroseconds(500);
}
stepper3.setCurrentPosition(0);
digitalWrite(dirPin4,LOW);
while(digitalRead(sensorPin4) == 1){
digitalWrite(stepPin4,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin4,LOW);
delayMicroseconds(500);
}
stepper4.setCurrentPosition(0);
digitalWrite(dirPin5,LOW);
while(digitalRead(sensorPin5) == 1){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
stepper5.setCurrentPosition(0);
}
void wireFeedForCrimp() {
if(digitalRead(L1) == 1 && check1 == 0){ // Check for first limit switch
check1 = 1;// Will continue code, even if wire is short enough to release L1
}
if(check1 == 1){ // Start motor so that it will grab wire when it reaches the gears
stepper6.setSpeed(250); // This speed may need to be adjusted
stepper6.runSpeed();
if(digitalRead(L2) == 1){ // Once second limit switch is tripped
stepper6.stop();
delay(100);
stepper6.setMaxSpeed(150);
stepper6.setAcceleration(50);
stepper6.setCurrentPosition(0);
stepper6.moveTo(1020); // Change this value to change how much wire is fed for crimping
while(stepper6.distanceToGo() != 0){
stepper6.run();
}
delay(1000);
digitalWrite(R1, LOW); // Crimp the pin
delay(4000);
digitalWrite(R1, HIGH);
stepper6.setMaxSpeed(800); // Expel the wire
stepper6.setSpeed(750);
int time1 = 0;
int time2 = 0;
while(digitalRead(L1) == 1){ // These two while loops expel the wire until it is free of the stepper motor regardless of the wire length
stepper6.runSpeed();
}
time1 = millis();
while(time2 < 3000){
stepper6.runSpeed();
time2 = millis()-time1;
}
// Need to add wire output functionality here(max speed ejection???)
delay(4000); // process delay; wire is now gone
check1 = 0; // If wire is gone, this should successfully reset the loop
}
}
}
void setup() {
Serial.begin(9600);
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
stepper3.setCurrentPosition(0);
stepper4.setCurrentPosition(0);
stepper5.setCurrentPosition(0);
stepper6.setCurrentPosition(0);
stepper7.setCurrentPosition(0);
// set speeds and accelerations of all motors
stepper1.setMaxSpeed(1000);
stepper1.setAcceleration(10000);
digitalWrite(enPin1, HIGH);
stepper2.setMaxSpeed(1000);
stepper2.setAcceleration(10000);
digitalWrite(enPin2, HIGH);
stepper3.setMaxSpeed(1000);
stepper3.setAcceleration(10000);
digitalWrite(enPin3, HIGH);
stepper4.setMaxSpeed(1000);
stepper4.setAcceleration(10000);
digitalWrite(enPin4, HIGH);
stepper5.setMaxSpeed(1000);
stepper5.setAcceleration(10000);
digitalWrite(enPin5, HIGH);
stepper6.setMaxSpeed(1000);
stepper6.setAcceleration(10000);
digitalWrite(enPin6, HIGH);
stepper7.setMaxSpeed(1000);
stepper7.setAcceleration(10000);
digitalWrite(enPin7, HIGH);
stepperID1.setMaxSpeed(1000);
stepperID1.setAcceleration(10000);
digitalWrite(enPinID1, HIGH);
stepperID2.setMaxSpeed(1000);
stepperID2.setAcceleration(10000);
digitalWrite(enPinID2, HIGH);
Serial.println("Motor Setup Complete");
// initialize lcd
// lcd.backlight();
// lcd.init();
// lcd.clear();
Serial.println("LCD Setup Complete");
// initialize integrated driver relay
pinMode(enPinID1, OUTPUT);
digitalWrite(enPinID1,HIGH);
pinMode(enPinID2, OUTPUT);
digitalWrite(enPinID2,HIGH);
pinMode(functionPin, OUTPUT);
digitalWrite(functionPin, HIGH);
Serial.println("Setup Complete");
}
void loop() {
//lcd.setCursor(0,0);
//lcd.print("Enter Motor:");
Serial.println("Enter Motor");
// lcd.setCursor(0,1);
// lcd.print("1,2,3,4,5");
char customKey = '\0';
// Wait for the user to input a valid motor number
while (customKey < '1' || customKey > '5') {
customKey = keypad.getKey();
}
// USER SELECTS 1
if(customKey == '1') {
stepper1.moveTo(10200);
stepper1.runToPosition();
}
// USER SELECTS 2
else if(customKey == '2') {
stepper2.moveTo(10200);
stepper2.runToPosition();
}
// USER SELECTS 3
else if (customKey == '3') {
stepper3.moveTo(10000);
stepper3.runToPosition();
}
// USER SELECTS 4
else if(customKey == '4') {
stepper4.moveTo(10000);
stepper4.runToPosition();
}
// USER SELECTS 5
else if(customKey == '5') {
stepper5.moveTo(10000);
stepper5.runToPosition();
}
delay(1000);
loadWires();
delay(2000);
function();
delay(10000);
dispelWires();
respool();
}