Stepper control with the "for"/"while" loop

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
rynde
 
Posts: 7
Joined: Wed Apr 25, 2012 5:14 pm

Stepper control with the "for"/"while" loop

Post by rynde »

Hello there,

I´m constructing a Time lapse rigg where a platform is traveling on a rail of approx. 1,5m/4,92ft. The platform is motorized by a stepper motor and controlled by a Motor Shield Kit v1.1 from Adafruit on top of an Arduino UNO.
The problem is that i´m fairly new to programming logic and have now got stuck in code...

My stepper can do 200 steps/rev,So to travel the <1,5m/4,92ft> it takes the stepper ~3900 steps
What I want to do is to take 150 pictures in these 3900 steps, which makes one picture every 26 steps.

I have implemented a ´for´ loop and this is where i´m not up to speed... I just don´t have top experience in loop commands. Don´t know where to look for errors.
Its good when I run it through the verifier but when I run the program on the rigg it just skips the 26 steps (plus the adding process) and goes directly to: motor.step(3900, BACKWARD, SINGLE);

The code i´m currently trying to make work is:

[Edit - moderator - use "Code" button when submitting code]

Code: Select all

#include <AFMotor.h>           

AF_Stepper motor(200, 2);   // 360/1.8 degrees.per.step = 200 steps/rev and If you're using M1 and M2, its port 1. If you're using M3 and M4 its port 2

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps = 1,2Kb/s . This is how fast the connection can read and write bits on the wire.
  pinMode(2, OUTPUT); 

  motor.setSpeed(10);  // 10 rpm   

}

void loop() 
{
  
  int x = 26;
  for (int i = 0; i < 3900; i = i+x)          // Starts the counter on 0 and adds a "26 step" each loop for 150 times up to 3900, witch is the stop of the line...
  {
  
    motor.step(i, FORWARD, SINGLE);  // i steps is how many steps you'd like it to take. direction is either FORWARD or BACKWARD and the step type is SINGLE, DOUBLE. INTERLEAVE or MICROSTEP. "Single" means single-coil activation
    delay(3000);                                  // Waits 3 sec due to vibrations to set
    digitalWrite(2, HIGH);                   // sends an impulse to the Canon 60D to take an image
    delay(1000);                                 // waits for a second
    digitalWrite(2, LOW);                    // turn impulse off
    delay(2000);
    if(i == 3900);
      {
      motor.step(3900, BACKWARD, SINGLE); 
      delay(300000000);                        // wait for a long time 
      break;                                          // Exit the loop
      }
   }
 }
Any Ideas where to I have gone wrong with the for loop, cause I´m just stuck :-(

Thanks... / Dave
Last edited by rynde on Wed May 16, 2012 6:26 am, edited 2 times in total.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper control problem with the "for" loop

Post by adafruit_support_bill »

// Starts the counter on 0 and adds a "26 step" each loop for 150 times up to 3900, witch is the stop of the line...
In spite of the comment. I don't see any 26-step logic in there.

rynde
 
Posts: 7
Joined: Wed Apr 25, 2012 5:14 pm

Re: Stepper control problem with the "for" loop

Post by rynde »

adafruit_support wrote:
// Starts the counter on 0 and adds a "26 step" each loop for 150 times up to 3900, witch is the stop of the line...
In spite of the comment. I don't see any 26-step logic in there.
Ok, Thanks for pointing in one direction.... Needs it :)

I guess I have to work on my logic programing skills... :?
I tried looking at Arduino´s reference webpage on for-loops but somehow it didn´t do it for me.

I thought that:

int x = 26;
for (int i = 0; i < 3900; i = i+x)


:Was the logic, but now it seems I have a thing or two to look up before my logic is in place :shock: :(

Thanks anyway :wink:

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper control problem with the "for" loop

Post by adafruit_support_bill »

int x = 26;
for (int i = 0; i < 3900; i = i+x)
I missed the x=26 on my first look. That's a step in the right direction, but not quite what you need.

Unless I missed something, you need to take all 3900 steps. And every 26th step you need to take an exposure. I think what you need is something like:

Code: Select all

for (int i = 0; i < 3900; i++) 
{
  // step motor

  if (i % 26 == 0)
  {
    // take exposure
  }
}

rynde
 
Posts: 7
Joined: Wed Apr 25, 2012 5:14 pm

Re: Stepper control problem with the "for" loop

Post by rynde »

Unless I missed something, you need to take all 3900 steps. And every 26th step you need to take an exposure. I think what you need is something like:

Code: Select all

for (int i = 0; i < 3900; i++) 
{
  // step motor

  if (i % 26 == 0)
  {
    // take exposure
  }
}

Thanks for the effort. :D
Though It did not make the camera go 26 steps - Taking a picture - going 26 steps and so forth until 3900.....( as you correctly understood) It seemed to add steps every time and it didn´t start with 26 steps. Maybe I did something wrong there. :( :wink:
But this made me look at the i++ operator and after searching I found what I needed... A While loop which lets you repeat something for x times

Code: Select all

void loop() 
{
  int var = 0;
  while(var < 4)    // do something repetitive 4 times
  {
    motor.step(26, FORWARD, SINGLE);
    delay(3000);
    digitalWrite(2, HIGH);   // set the LED on
    delay(1000);              // wait for a second
    digitalWrite(2, LOW);    // set the LED off
    delay(2000); 
    var++;
  
  }
motor.step(104, BACKWARD, SINGLE);
delay(100000000);
}
Thanks for all help // Dave :)

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper control problem with the "for" loop

Post by adafruit_support_bill »

A 'for' loop is really just a 'while' loop with the control variable increment built in. Either can do the job here.

The loop I posted earlier should work with calls to "motor.onestep()". But you will probably get smoother operation doing it as you did with "motor.step(26, FORWARD, SINGLE);". To do that, you just need to change the loop increment.

Code: Select all

for (int i = 0; i < 3900; i+=26)
{
    motor.step(26, FORWARD, SINGLE);
    // take exposure
}

koachkletus
 
Posts: 19
Joined: Thu Mar 22, 2012 1:28 am

Re: Stepper control problem with the "for" loop

Post by koachkletus »

I'm working on a project similar to this and was just wondering what type camera (setup) and what you are using to trigger your setup. In my particular project I'm looking to trigger a webcam at x amount of motor steps.

rynde
 
Posts: 7
Joined: Wed Apr 25, 2012 5:14 pm

Re: Stepper control problem with the "for" loop

Post by rynde »

The setup I´m using is a Canon 60D witch is triggered by the program code: ( digitalWrite(2, HIGH); // set the LED on pin #2 on and the: delay(1000); // waits for a second for time to take exposure) as seen in the code in the earlier posts.
The 60D gets the signal through the 2,5mm remote trigger jacket as seen in the image below

Image

More pics here: https://www.dropbox.com/sh/bcpqfevz95l4 ... eLapsePics

rynde
 
Posts: 7
Joined: Wed Apr 25, 2012 5:14 pm

Re: Stepper control problem with the "for" loop

Post by rynde »

Made a 7 sec test video from all 150 pics I took on my test run with a Mac OSX program (free, just google Time Lapse Assembler).

Here´s the Youtube video:

http://www.youtube.com/watch?v=7XaH-5jWQhk

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper control problem with the "for" loop

Post by adafruit_support_bill »

Nice build! What are you using for bearings on the rails?

rynde
 
Posts: 7
Joined: Wed Apr 25, 2012 5:14 pm

Re: Stepper control problem with the "for" loop

Post by rynde »

I just bought the smallest roller wheels I could find in my local hard ware store ,about $1 each

Image

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”