I am working on making a device to automate a tapping operation for some small metal parts. I have an Arduino Uno as the controller. I am using a NEMA 23 stepper motor with a TB6600 stepper controller (step and dir pins) This is attached to a round disc with cutouts for the metal parts that are to be tapped.
The stepper rotates 90 degrees, then a relay is activated which turns on the tapping head, then when the tappnig is finished (checked by a hall effect sensor), the table rotates 90 deg again and taps...etc. I have a homing function at startup that uses an optical sensor to detect the correct start position of the table (small slit in the table). There is a run/stop switch and pushbuttons to activate the tap or cycle the table independantly.
This all works as it is now. But what happens over time is the table starts to loose position. I think this is because there are ocassional burrs in the parts that sometimes get hung up as they slide on the table and that causes a missed step here or there. Eventually things get too far out of alignment. Seems that since I have an optical sensor for initially homing the table, I should use that to verify the table has moved to the correct position. But I'm struggling to think of how to integrate that into my sketch.
I have the code broken up into functions:
home(); performes the initial homing.
cycle(); rotates the stepper 50 steps (90 deg).
tap(); activates the relay that activates the tap.
The loop function then just checks the position of the sensors and buttons and calls the appropriate function.
The thing I can't get my head around is how to change the cycle function to use the optical sensor instead of just stepper.move(50);
Also, when the next cycle needs to start and the table needs to move again, the optical sensor will already be showing that the table is in the correct position. How do I move it until the optical sensor input (homeSwitch) goes low then stops on high again?
I was so proud of working out what I have so far...this seems like such a simple thing to get hung up on.
Here is the code:
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
//DEFINE PINS
//STEPPER PINS
const int stepPin = 2;
const int dirPin = 3;
//<<<-----INPUT PINS
const int runSwitch = 4;
const int tapSense = 5;
const int cycleButton = 7;
const int tapButton = 8;
const int homeSwitch = 9;
//------>>>OUTPUT PINS
const int tapRelay = 6;
//DECLARATIONS
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
LiquidCrystal_I2C lcd(0x27, 16, 2); //I2C address, 16 column, 2 rows
//VARIABLES
int runState = 0;
int tapState = 0;
int cycleButtonState = 0;
int tapButtonState = 0;
int stepsMoved = 0;
int cycleCount = 0;
int homeState = 0;
long initial_homing = -1;
void setup() {
//Pin Setups
pinMode(runSwitch, INPUT_PULLUP);
pinMode(tapSense, INPUT_PULLUP);
pinMode(tapRelay, OUTPUT);
pinMode(cycleButton, INPUT_PULLUP);
pinMode(tapButton, INPUT_PULLUP);
pinMode(homeSwitch, INPUT_PULLUP);
//LCD Setup
lcd.init();
lcd.backlight();
lcd.clear();
// Stepper Settings
stepper.setMaxSpeed(1000);
stepper.setAcceleration(10000);
stepper.setSpeed(1000);
stepper.setCurrentPosition(0);
//HOME STEPPER
home();
//SET LCD TEXT
lcd.setCursor(0, 1);
lcd.print("CYCLE:");
}
void loop() {
//READ SWITCHES SENSORS & BUTTONS
runState = digitalRead(runSwitch);
tapState = digitalRead(tapSense);
cycleButtonState = digitalRead(cycleButton);
tapButtonState = digitalRead(tapButton);
//IF RUN=ON AND TAP=READY, CYCLE TABLE
if (runState == LOW
&& tapState == LOW) {
cycle();
}
//IF STEPPER HAS MOVED, RUN=IS ON, AND TAP=READY...ACTIVATE TAP
if (stepper.distanceToGo() == 0
&& tapState == LOW
&& runState == LOW) {
tap();
}
//IF RUN=OFF, AND TAP=READY, AND TAP BUTTON=PRESSED...ACTIVATE TAP
if (runState == HIGH
&& tapState == LOW
&& tapButtonState == LOW) {
tap();
}
//IF RUN=OFF, AND TAP=READY, AND CYCLE BUTTON=PRESSED...CYCLE TABLE
if (runState == HIGH
&& tapState == LOW
&& cycleButtonState == LOW) {
cycle();
}
}
//------------------HOMING FUCNTION-----------------
void home() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HOMING...");
while (digitalRead(homeSwitch)) {
stepper.moveTo(initial_homing);
initial_homing--;
stepper.run();
delay(5);
}
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(100.0); //set slower for better accuracy
stepper.setAcceleration(100.0);
initial_homing = 1;
stepper.setCurrentPosition(0);
lcd.clear();
lcd.print("HOMING COMPLETED");
delay(2000);
}
//----------------RUN CYCLE--------------------------
void cycle() {
stepper.move(50);
lcd.setCursor(0, 0);
lcd.print("MOVING ");
while (stepper.distanceToGo() != 0) {
stepper.run();
}
if (stepper.distanceToGo() == 0) {
lcd.setCursor(0, 0);
lcd.print(" ");
}
}
//------------------TAPPING FUNCTION-------------------
void tap() {
digitalWrite(tapRelay, HIGH); //ACTIVATE TAP HEAD
lcd.setCursor(0, 0);
lcd.print("TAPPING");
delay(1000);
digitalWrite(tapRelay, LOW);
cycleCount = cycleCount + 1; //ADD 1 to CYCLE COUNT
lcd.setCursor(7, 1);
lcd.print(cycleCount);
delay(1000);
}
Pic of the wiring schematic and a pic of the rotary table CAD: