So I've been experimenting with sample bounce sketch that is included in the Accell Stepper Library. Please see below.
I have a questions about the logic and mechanics. So my driver and motor are set up to 4 Microsteps -- i.e. 800 steps/rev.
If I set the below initial position to 800 and run the code, here's what happens:
-servo rotates 1 revolution in one direction
-servo rotates 2 revolutions in the opposite direction.
If I wanted to use this code in a physical mechanism, I couldn't correct? Because it would first go, for example, 1 revolution forward, then 2 revolution back...past my starting point.
What type of operation(s) can I add so that the second action is to only go 1 revolution back? After I home, can I first send it 1 revolution, then run the bounce code?
Does that make sense?
//
// Make a single stepper bounce from one limit to another
//
// Define a stepper and the pins it will use
#include <AccelStepper.h>
//direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper(1, 9, 8);
int pos = 1600;
void setup()
{
stepper.setMaxSpeed(5000);
stepper.setAcceleration(2000);
}
void loop()
{
if (stepper.distanceToGo() == 0)
{
delay(500);
pos = -pos;
stepper.moveTo(pos);\
}
stepper.run();
}
If I set the below initial position to 800 and run the code, here's what happens:
-servo rotates 1 revolution in one direction
-servo rotates 2 revolutions in the opposite direction.
The AccelStepper moveTo function uses absolute coordinates. The first move is from 0 to 800, 800 steps or 1 rev. The next move is from 800 to -800, 1600 steps or 2 revs. Next -800 to 800 and so on.
@groundFungus thanks so much!!!! So easy, i'm embarrassed.
So I'd like to share my code if anyone wants to do something similar. I'm 3D printing an oscillation mechanism on a rack and pinion gear. I wanted to use a homing function, then "ping pong" from one end to another by prescribing the number of definite steps from one end to another. Parts are almost done. Can't wait to mount everything.
Thanks again!
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepper(1, 9, 8);
// Define the Pins used
#define home_switch 3 // Pin 9 connected to Home Switch (MicroSwitch)
int directionPin = 9; //Direction pin
int stepPin = 8; //Stepper pin
// Stepper Travel Variables
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
int pos = 800;
void setup() {
pinMode(home_switch, INPUT_PULLUP);
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepper.setMaxSpeed(100); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run(); // Start moving the stepper
delay(5);
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepper.moveTo(initial_homing);
stepper.run();
initial_homing++;
delay(5);
}
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(5000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper.setAcceleration(1000.0); // Set Acceleration of Stepper
}
void loop()
{
if (stepper.distanceToGo() == 0)
{
delay(500);
pos = -pos;
stepper.move(pos); \
}
stepper.run();
}
So I wanted to try a variant on my code and mechanism below. I have the homing code working. And I have the oscillation working with relative moves.
I've added a second optical switch to act as a stop switch. In other words, if the switch is made, the motor will stop. I've added code to the end of the void loop that essentially reads the stop switch if it's HIGH and then writes the motor pin to LOW. This does not work. Nothing actually happens.
Any ideas on how to make this second limit switch stop the stepper?
Thanks!!
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepper(1, 9, 8);
// Define the Pins used
#define home_switch 3 // Pin 3 connected to Home Switch (OptoSwitch)
#define stop_switch 2 // Pin 2 connected to Stop Switch (OptoSwitch)
int directionPin = 9; //Direction pin
int stepPin = 8; //Stepper pin
// Stepper Travel Variables
long initial_homing = -1; // Used to Home Stepper at startup
long kill_homing = -1; // Used to Home Stepper at stop
int pos = 3200;
void setup() {
pinMode(home_switch, INPUT_PULLUP);
pinMode(stop_switch, INPUT_PULLUP);
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepper.setMaxSpeed(100); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run(); // Start moving the stepper
delay(5);
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepper.moveTo(initial_homing);
stepper.run();
initial_homing++;
delay(5);
}
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(4000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper.setAcceleration(1000.0); // Set Acceleration of Stepper
}
void loop()
{
if (stepper.distanceToGo() == 0)
{
delay(500);
pos = -pos;
stepper.move(pos);
}
stepper.run();
if (digitalRead(stop_switch) == LOW)
{
digitalWrite(stepPin, LOW);
}
}