Hello
I’m hitting a wall with a project. I’m coding control for a motorised camera slider. I have two limit switches that turn off power to the stepper when closed. I have tried code that tells the stepper to move a particular number of steps, which the program executes regardless of the state of the limit switches.
So I know what I need to do, more or less, but am not sure how exactly to go about it. In order for the limits to be effective, I need a stepping function that moves one full step, and in my loop I need to call this function as many times as I need to move the slider carraige around.
I’m using the L293d because I have now successfully bricked three A4988 boards for different but similar reasons and have no more.
I know the problem is in my singleStep() function. I’m using a bipolar stepper (4 wires). For now I’ve got a delay() between on / off states in the step function, which I intend to replace with delaymicrosecond() once I get it working at all.
For now, with this code, the motor jitters back and forth and then stalls.
Do I need to do something like this? http://www.tigoe.net/pcomp/code/circuits/motors/stepper-motors/
Step wire 1wire 2wire 3wire 4
1 High low high low
2 low high high low
3 low high low high
4 high low low high
#include <Stepper.h>
int in1Pin = 16;
int in2Pin = 17;
int in3Pin = 14;
int in4Pin = 13;
Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);
int en1Pin = 3;
int en2Pin = 4;
int limit1 = 18; //slider limit switch left, motor end
int limit2 = 19; // slider limit switch right, idler pulley end
#define goLEFT 0
#define goRIGHT 1
void setup()
{
pinMode(limit1, INPUT_PULLUP);
pinMode(limit2, INPUT_PULLUP);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
pinMode(en1Pin, OUTPUT);
pinMode(en2Pin, OUTPUT);
// this line is for Leonardo's, it delays the serial interface
// until the terminal window is opened
while (!Serial);
Serial.begin(9600);
motor.setSpeed(10);
}
boolean leftLimit() {
return digitalRead(limit1);
}
boolean rightLimit() {
return digitalRead(limit2);
}
void eStop() {
if ( leftLimit() == 0 || rightLimit() == 0) {
disengage();
}
}
void singleStep() {
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, HIGH);
digitalWrite(in4Pin, LOW);
delay(7);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, HIGH);
delay(7);
}
void loop() {
engage();
eStop();
singleStep();
}
void engage() {
digitalWrite(en1Pin, HIGH);
digitalWrite(en2Pin, HIGH);
}
void disengage() {
digitalWrite(en1Pin, LOW);
digitalWrite(en2Pin, LOW);
}