Hey. I'm working on an x-y rotary table program and i'm stuck and need a little guidance. I'm making some progress, but i've reached a dead end, so i'd figure i'd finally ask for help after a week of no luck. Here's what i have going on. I'm making a gear cutter for wood gears. I have a rotary table similar to a record player that is connected to my mill/drill press. The first program will turn the gear to the correct angle, and my drill press drills the hole for the base of the tooth, and goes on to the next position. I have this part pretty much figured out.
The second program will be similar to the first program, but instead of drilling holes, a stepper motor will be connected to my milling table and the rest of the gear tooth will be cut out either using an end mill, or a scroll saw with a round blade. I haven't decided on which cutting method i'm going to use yet, but either way the program will be the same.
below is my code. it's just an experiment and i've been using different variations on code, so please don't criticize too much since i'm new and trying to figure it out. I've borrowed different parts of code, but i have it working for the most part.
Here's how it's supposed to work. When i turn it on. stepper1 rotates constantly until it, or I hit the home button, and then it stops, and this will be home.. I put this in the loop so i have the option of rehoming it anytime without hitting the reset button. And then it waits until a button is pressed to go forward or backward. I can make both steppers take the appropriate steps, which is basically my first program listed below. It spins, and then drills, and then repeats when i push the button. But i would like to modify this code so when i push the button, i get a blocking function where both steppers move together to cut the gear profile. And that's where i'm having problems. the MultiStepper functions are a recent addition trying to get both steppers working in unison, and i'm unsure if this is correct, or if i could do it strictly from accelstepper, and if so, how is this done?
any help would be appreciated. The question is in the loop below the function that has ????
Another issue i have is with the code. when i upload or start the program frest and i press the button at home the very first time it cycles through rest of the code, and then it stops like it's supposed too. And then i can restart the home and stop it on a dime. but i assume i'm missing a line of code at the end of the while loop that stops it from cycling rest of the code before stopping at home? I could work with this extra movement at the beginning, but it is something i'd like to fix eventually.
thanks for any input
#include <Stepper.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
Stepper stepper1(6400, 4,3);
Stepper stepper2(3200, 11,12);
boolean MultiStepper::addStepper ( AccelStepper & stepper1 );
boolean MultiStepper::addStepper ( AccelStepper & stepper2 );
MultiStepper steppers;
const int dirPin = 3;
const int stepPin = 4;
const int enPin = 5;
// Stepper 2
const int dirPin2 = 11;
const int stepPin2 = 12;
const int enPin2 = 13;
const int switchOne = 8; //forward switch
const int switchTwo = 9; //backward switch
int home_switch = 10; // Pin 10 connected to Home Switch (MicroSwitch)
int direction; // Variable to set Rotation (CW-CCW) of the motor
int steps; // Used to set HOME position after Homing is completed
int p1buttonState = 0; // current state of the button
int lastp1buttonState = 0; // previous state of the button
int p2buttonState = 0; // current state of the button
int lastp2buttonState = 0; // previous state of the button
bool bPress = false;
bool isForward = false;
bool isBackward = false;
void setup() {
stepper1.setSpeed(100);
stepper2.setSpeed(100);
pinMode( home_switch, INPUT_PULLUP);
pinMode( switchOne, INPUT_PULLUP);
pinMode( switchTwo, INPUT_PULLUP);
pinMode(enPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(enPin2, OUTPUT);
digitalWrite(enPin,HIGH);
digitalWrite(enPin2,HIGH);
}
void loop() {
while (digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(dirPin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(stepPin, HIGH);
delayMicroseconds(100); // Delay to slow down speed of Stepper
digitalWrite(stepPin, LOW);
delayMicroseconds(100); //???? at startup, it continues and cycles the rest of the code
// before stopping. Usually i just click the home button
// once so it will cycle early, and then i let it hit the home
// switch, but this is wrong.
}
isForward = false;
isBackward = false;
p1buttonState = digitalRead(switchOne);
p2buttonState = digitalRead(switchTwo);
if (p1ButtonPress()) {
delay(500);
}
if (p2ButtonPress()) {
delay(500);
}
if( isForward ){
delay(100);
stepper1.step(6400); // ???? This is where i'd like it to runToPosition, or a similar blocking function.
// Each stepper will need to make about 4-5 small movements,
// and then returns to the waiting spot awaiting a button press
// before going to the next step.
stepper2.step(3200);
delay(2000);
}
if( isBackward ) {
delay(100); // ???? This will be just like above code, but in reverse.
stepper1.step(-6400);
stepper2.step(-3200);
delay(100);
}
}
bool p1ButtonPress()
{
bool isPress = false;
// compare the p1buttonState to its previous state
if (p1buttonState != lastp1buttonState) {
// if the state has changed, increment the counter
if (p1buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
isPress = true;
} else {
// if the current state is LOW then the button went from on to off:
isForward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp1buttonState = p1buttonState;
return isPress;
}
bool p2ButtonPress()
{
bool isPress = false;
// compare the p1buttonState to its previous state
if (p2buttonState != lastp2buttonState) {
// if the state has changed, increment the counter
if (p2buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
isPress = true;
} else {
// if the current state is LOW then the button went from on to off:
isBackward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp2buttonState = p2buttonState;
return isPress;
}