In isolation (73 years old) and trying to get my macro photo stacking rail to return to start without a limit switch. I know the number of steps taken, so it is just a matter of reversing the motor. My code (copied and pasted from other sites) works like a dream (10 steps, fire an IR camera trigger) and continues up to when I want to return (line 73) taking 100 shots or steps however many I want - no problem.
I started a void return loop (and terminated the loop) but the coding has me beat. I have attached my code (might look terrible, but it works). Can anyone help out this old guy and give me the code to counter clockwise 1000 steps?
Thanks fellas David
[code]
#include <AccelStepper.h>
#include <MultiStepper.h>
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin
int Distance = 0; // records the number of steps we have taken
byte numRepeats = 0;
byte maxRepeats = 100;
int smFirePin = 13; //Trigger pin
#define fire 13
void setup(){
/*Sets all pin to output; the microcontroller will send them(the pins) bits, it will not expect to receive any bits from thiese pins.*/
pinMode(2, OUTPUT); //Direction Pin
pinMode(3, OUTPUT); //Step Pin
pinMode(13, OUTPUT); //LED pin
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(13,LOW);
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
Serial.begin(9600); //Open Serial connection for debugging
}
void loop(){
if (numRepeats < maxRepeats) {
move1Step();
Distance = Distance + 1; //record this step to see if we are et the end of our move
if (Distance == 10)
{
if (digitalRead(2) == LOW)
{
digitalWrite(2 , LOW);
}
else
{
digitalWrite(2, LOW);
}
Distance = 0;
numRepeats ++;
delay(3000);
{
trigger();
}
}
}
}
void move1Step() {
digitalWrite(3, HIGH);
delayMicroseconds(750);
digitalWrite(3, LOW);
delayMicroseconds(750);
}
// Power on the IR trigger circuit to fire the camera
void trigger()
{
delay(1000); // wait for vibration to settle
digitalWrite(fire, HIGH); // take the picture
delay(2000); // wait for that to happen
digitalWrite(fire, LOW); // disable the IR circuit again
}
// Advance the motor by one step
void step()
{
digitalWrite(step, HIGH); //Trigger one step forward
delay(1);
digitalWrite(step, LOW); //Pull step pin low so it can be triggered again
delay(1);
}
void return();
{
digitalWrite(smDirectionPin, HIGH);
/*Slowly turns the motor 1600 steps*/
for (int i = 0; i <200; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(700);
digitalWrite(smStepPin, LOW);
delayMicroseconds(700);
}
while(1) { }
}
[/code]
1 Answer 1
I cleaned up your code a bit. Removed duplicate and unused code. I used a for
-loop to make it run for 100 repetitions.
Feel free to let me know if you have any more questions, or if there are things that aren't clear.
// You aren't using these libraries, so I commented them out
//#include <AccelStepper.h>
//#include <MultiStepper.h>
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin
int smFirePin = 13; //Trigger pin
byte maxRepeats = 100;
void setup()
{
/*Sets all pin to output; the microcontroller will send them(the pins) bits, it will not expect to receive any bits from thiese pins.*/
pinMode(smDirectionPin, OUTPUT); //Direction Pin
pinMode(smStepPin, OUTPUT); //Step Pin
pinMode(smFirePin, OUTPUT); //Fire pin
digitalWrite(smDirectionPin, LOW);
digitalWrite(smStepPin, LOW);
digitalWrite(smFirePin,LOW);
Serial.begin(9600); //Open Serial connection for debugging
}
void loop()
{
digitalWrite(smDirectionPin, LOW); // set stepper motor to run forwards
for( byte i=0; i<maxRepeats; i++ ) // run the following section 100 times
{
//move 10 steps forward
for( byte j=0; j<10; j++ )
{
move1Step();
}
// wait and take a photo
delay(3000);// there is already a delay in the `trigger` function, so this like could be removed
trigger();
}
// return the stepper motor to the starting position
digitalWrite(smDirectionPin, HIGH); // set stepper motor to run in reverse
for( byte i=0; i<maxRepeats; i++ ) // run the following section 100 times
{
//move 10 steps backwards
for( byte j=0; j<10; j++ )
{
move1Step();
}
}
// Do nothing
while(1){ }
}
void move1Step()
{
digitalWrite(smStepPin, HIGH);
delayMicroseconds(750);
digitalWrite(smStepPin, LOW);
delayMicroseconds(750);
}
// Power on the IR trigger circuit to fire the camera
void trigger()
{
delay(1000); // wait for vibration to settle
digitalWrite(smFirePin, HIGH); // take the picture
delay(2000); // wait for that to happen
digitalWrite(smFirePin, LOW); // disable the IR circuit again
}
-
Gerben, I will follow your advice on deleting the delay, you should publish this, many in micro photography would be grateful, works like a charm. Fantastic.Tazdave– Tazdave2020年04月03日 03:05:58 +00:00Commented Apr 3, 2020 at 3:05
-
If you delete the delay, you might have to increase the
delay(1000)
. As currently it waits 4 seconds to prevent any movement from vibrations. You'd have to see how short the values can be, or just be a bit more patient. Glad it helped you.Gerben– Gerben2020年04月03日 14:33:26 +00:00Commented Apr 3, 2020 at 14:33
if (digitalRead(2) == LOW) { digitalWrite(2 , LOW); } else { digitalWrite(2, LOW); }
... that whole block can be replaced bydigitalWrite(2, LOW);
return
is a reserved word, so you should rename the function to, for example,returnHome
. You also need to remove the;
at the end of that line of code.