loop 4 times panobot

I am new to programming and trying to figure out how to stop a loop after executed 4 times.
I am working on a panobot (motorized panoramic head). What the code does is rotate a steppermotor 90 degrees and take a photo. After 4 90 degree rotations and photographs i want it to stop. I do not know how to achieve that.
This is the code i have so far:

#define SHUTTER_PIN 7
int Distance = 0;  // Record the number of steps we've taken

void setup() {                

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);

}

void loop()   

{
  digitalWrite(9, HIGH);
  delayMicroseconds(60);          
  digitalWrite(9, LOW); 
  delayMicroseconds(60);
  Distance = Distance + 1;   // record this step
 
  // Check to see if we are at the end of our move

if (Distance == 21481)   //  (stepper with 26,8:1 gearbox and microstepper shield (1:16)

  {

    Distance = 0;
    // Now pause for a second
    delay(1000);  
    digitalWrite(SHUTTER_PIN, LOW);
    delay(2000);  // take schot
    digitalWrite(SHUTTER_PIN, HIGH); 
  }
 
}

have a look at the for loop - http://www.arduino.cc/en/Reference/For -

or something like this

int maxshots = 4;
int shots = 0;

void setup()
{
  ...
}

void loop()
{
  if (shots < maxshots)
  {
    rotate(90);
    take_shot();
    shots = shots + 1;   // or shots++;  in short
  }
  // do other things here
}


void rotate(int degrees)
{
  ... // rotate code
}


void take_shot()
{
  .. here the code to take a shot
}

Dag Rob,

I changed the code but the stepper isn’t moving… Am i missing something?

#define SHUTTER_PIN 7
#define STEPPER_PIN 8
int maxshots = 4;
int shots = 0;
int Distance = 0;  // Record the number of steps we've taken

void setup() {                
   
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);

}

void loop()
{
  if (shots < maxshots)
  {
    rotate();
    shots = shots + 1;   // or shots++;  in short
  }
  // do other things here
}


void rotate()
{
  digitalWrite(STEPPER_PIN, HIGH);
  delayMicroseconds(60);          
  digitalWrite(STEPPER_PIN, LOW); 
  delayMicroseconds(60);
  Distance = Distance + 1;   // record this step
 
  // Check to see if we are at the end of our move

  if (Distance == 21481)  // 4 fotos per omwenteling

   {

    Distance = 0;
    // Now pause for a second
    delay(1000);  
    digitalWrite(SHUTTER_PIN, LOW);
    delay(2000);  // lengte schots
    digitalWrite(SHUTTER_PIN, HIGH); 
  }

}

What the code does is rotate a steppermotor 90 degrees and take a photo.

How is your stepper motor connected to the Arduino? Why are you not using the Stepper library?

Holding a pin HIGH or LOW like that looks weird.

I changed the code but the stepper isn't moving.. Am i missing something?

Yes, you are. Debug statements.

The stepper is connected to a big easy stepper shield. I am just starting with arduino and electronics, so everything is new to me..
For this moment the stepper is working, but it keeps going on (rotate 90 degr. take a picture, rotates, picture..) until i disconnect the power. What i wand is that it stops after 4 movements of 90 degrees and 4 pictures. Next step is a better code and more features.. I am learning.. :wink:

Next step is a better code and more features.

No. The next step is to learn to use Serial.begin(), Serial.print(), and Serial.println() to debug your program.

Oké, you're right. But can you put me in the right direction to achieve where I'm asking for with some modifications on the code i have?

But can you put me in the right direction to achieve where I'm asking for with some modifications on the code i have?

No, I can't. I can't see anything wrong with it. I'd start with some debugging, to determine if rotate() is called at all. If not, you have one problem. If it is, you have another.

When i remove: shots = shots + 1; it works, but of course it will not stop after 4 times.

Your control logic does not make sense.

In loop() you will call rotate() four times and then stop calling it. In rotate you step the motor one position and check whether the total number of steps has reached 21481. (Of course it hasn’t - it will never get past four.)

You need to decide whether you’re going to have rotate move the stepper to the required position and return when the movement is completed, or call it repeatedly until the movement is completed (in which case you need to provide some way for loop() to determine when the movement is completed).

If this is all your sketch ever needs to do, then the simplest approach would be along these lines (pseudocode):

for(int shot = 0; shot < TotalShots; shot++)
{
    for(int step = 0; step < StepsPerShot; step++)
    {
        stuff to make the stepper move one step
    }
    stuff to take a shot
}

while(true) {} // execution does not proceed past this point

Thanks for the help! it works. The Serial.println tip was also very useful.

#define SHUTTER_PIN 7
#define DIRECTION_PIN 8
#define STEPPER_PIN 9
int TotalShots = 4;
long StepsPerShot = 85924 / TotalShots; // 85924 is the total amound of steps te complete a rotation (200 x 26,7:1 planetgear x 16 microsteps)

void setup() {                

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  Serial.begin(9600);
  Serial.println(StepsPerShot);

}

void loop()  { 

for(int shot = 0; shot < TotalShots; shot++)
{
    for(int steps = 0; steps < StepsPerShot; steps++)
    {
      digitalWrite(STEPPER_PIN, HIGH);
      delayMicroseconds(60); // speed of the stepper         
      digitalWrite(STEPPER_PIN, LOW); 
      delayMicroseconds(60);
    }
    delay(1000);  
    digitalWrite(SHUTTER_PIN, LOW);
    delay(2000);  // take shot
    digitalWrite(SHUTTER_PIN, HIGH); 

  }


while(true) {} // execution does not proceed past this point  

}

What the code does is rotate a steppermotor 90 degrees and take a photo.

it works.

So now we are curious for the photos

just kidding :wink:

see www.hanshermans.nl :wink:
still a long way to go to complete the panobot..