0

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]
asked Apr 1, 2020 at 4:49
4
  • code, such as this, makes no sense ... if (digitalRead(2) == LOW) { digitalWrite(2 , LOW); } else { digitalWrite(2, LOW); } ... that whole block can be replaced by digitalWrite(2, LOW); Commented Apr 1, 2020 at 5:07
  • 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. Commented Apr 1, 2020 at 13:18
  • Plus your indentation is a mess. Pick an indentation style and then follow it religiously. As it is the code is quite hard to read. Commented Apr 1, 2020 at 18:47
  • Gerben has provided a solution, your observation have been noted and accepted, thanks Commented Apr 3, 2020 at 3:09

1 Answer 1

0

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
}
answered Apr 1, 2020 at 13:24
2
  • 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. Commented 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. Commented Apr 3, 2020 at 14:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.