0

counter to end loop
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

counter to end loop

by keithg on Tue Feb 03, 2009 10:24 am

I want to write code that will stop a stepper motor after x number of a back/forth cycles. Speed and distance are functioning but ending the sequence is confusing to this non-programmer. Do I use an "if" or "for" to accomplish this? Where and how do I plug it into:

/*
Stepper Motor Controller
language: Wiring/Arduino

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.

The motor moves 100 steps in one direction, then 100 in the other.

Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe

*/

// define the pins that the motor is attached to. You can use
// any digital I/O pins.

#include <Stepper.h>

#define motorSteps 211 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define ledPin 13

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);

void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(60);

// Initialize the Serial port:
Serial.begin(9600);

// set up the LED pin:
pinMode(ledPin, OUTPUT);
// blink the LED:
blink(3);
}

void loop() {
// Step forward 100 steps:
Serial.println("Forward");
myStepper.step(100);
delay(500);

// Step backward 100 steps:
Serial.println("Backward");
myStepper.step(-5'00);
delay(500);

}

// Blink the reset LED:
void blink(int howManyTimes) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Re: counter to end loop

by jarv on Tue Feb 03, 2009 6:13 pm

I would just create a function that takes the numer of times you want to execute this, for example to do it 20 times:
Code: Select all | TOGGLE FULL SIZE
void loop () {
   step(20);
}


void step(int x) {

  int cnt=0;
  for (cnt=0;cnt<x;cnt++) {
 // Step forward 100 steps:
   Serial.println("Forward");
   myStepper.step(100);
   delay(500);

// Step backward 100 steps:
    Serial.println("Backward");
    myStepper.step(-5'00);
    delay(500);
  }
}
projects (avr/electronics/programming) - http://jarv.org
jarv
 
Posts: 49
Joined: Tue Jan 02, 2007 5:48 pm

Re: counter to end loop

by keithg on Tue Feb 03, 2009 8:54 pm

Thank you for the code. I am apparently not including it to the existing sketch properly because the motor continues running. A simple error may be obvious but I lack the experience to find it.
would you please review this again?


/*
Stepper Motor Controller
language: Wiring/Arduino

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.

The motor moves 100 steps in one direction, then 100 in the other.

Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe

*/

// define the pins that the motor is attached to. You can use
// any digital I/O pins.

#include <Stepper.h>

#define motorSteps 211 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define ledPin 13

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);

void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(60);

// Initialize the Serial port:
Serial.begin(9600);


}

void loop () {
step(10);
}


void step(int x) {

int cnt=0;
for (cnt=0;cnt<x;cnt++) {
// Step forward 100 steps:
Serial.println("Forward");
myStepper.step(100);
delay(500);

// Step backward 100 steps:
Serial.println("Backward");
myStepper.step(-100);
delay(500);
}
}
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Re: counter to end loop

by mtbf0 on Tue Feb 03, 2009 11:23 pm

the problem with the solution offered is that loop runs, (as you might expect), in a loop. so every time loop runs, your stepper motor will repeat its routine 20 times.

i suggest that you initialize a counter in setup(), for instance
Code: Select all | TOGGLE FULL SIZE
  count = 20;
then test the counter in loop
Code: Select all | TOGGLE FULL SIZE
void loop (void) {
  if (count) {
    count--;
    // Step forward 100 steps:
    Serial.println("Forward");
    myStepper.step(100);
    delay(500);

    // Step backward 100 steps:
    Serial.println("Backward");
    myStepper.step(-100);
    delay(500);
  }
}
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: counter to end loop

by keithg on Wed Feb 04, 2009 8:42 am

Works great! Saved me hours of hair pulling trial and error. If I were thinking ahead, I should have asked about reset code as well. The battery powered stepper moves lights along a salvaged printer track that guide a photovore robot for a game designed by gifted fourth grade students. I have assumed a restart switch should be wired to the battery power. Which do you feel is the better choice; power switch or reset button as part of the sketch?
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Re: counter to end loop

by karlgg on Wed Feb 04, 2009 4:52 pm

At the moment, the reset button already on the Arduino (and extended to the motor shield) would restart your sketch automatically, after a short delay. You don't have to add or change anything, unless the button is inconveniently out of reach.

If you use the solder-point for pin 2 (the silver hole near the pin) or any of A0-5 on the other side, you could add more buttons to add/change behaviors... Including resetting the counter to "20" or whatever you want when a button is pressed.
I think I am, therefore I am... I think.

karlgg
 
Posts: 212
Joined: Sat Dec 27, 2008 2:41 pm
Location: Anthony, NM

Re: counter to end loop

by keithg on Wed Feb 04, 2009 10:13 pm

The stepper is finally running according to plan. I will present it to our students tomorrow. Thank you so much!
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Please be positive and constructive with your questions and comments.