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);
}
}
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
}
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);
}
}
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..
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.
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