Motor Infiniate loop syntax help.

Hello all,

I'd appreciate any thoughts here. I'm trying to set up an infinite loop to auto reverse a stepper motor.
I'm very new with coding in general so I'm having trouble knowing what to look for on Google and the Arduino pages for examples. So even pointers to other projects that do similar things would be greatly appreciated.

My question is in regards to if/else/then type logic of the sketch.
I'm using the ladyada motor shield and feel confidant about getting the motors going –– its the structure of the logic that I need help with.

I'm looking for a sketch which starts the motor turning in a forward direction until the motor reaches a maximum position sensor (say a magnetic reed switch set to one of the available digital input pins), then motion will pause and then reverse until the minimum position sensor is found - repeat.

It occurs to me that I may also have problems when the motor leaves the min or max position as the switch that was on will suddenly turn off.

Seems simple enough, though I don't know where to start.
Many thanks in advance.

Well, the infinite loop is taken care of by "loop()".
So, set your initial direction in "setup ()", and use "loop" to monitor the end-stop switches,, flipping the value of direction.

I'm sorry AWOL :-[
I don't have it quite it together enough to run with what your suggesting. Could you be more explicit? A simple example would help me greatly.
Forgive the pan-scripting jargon but Im after something like:

if $max_pos = 1 then set $direction to REVERSE
if $min_pos = 1 then set $direction to FORWARD

This is the foundation which Im working off, it loops, but Im not sure how to integrate sensors.:

#include <AFMotor.h>

AF_Stepper motor(200, 2); 
// we get the pulse per degree of a stepper by dividing 360 by the motor rating ie 360/7.5=48 or in this case 360/1.8=200.
// common stepper ratings are 1.8, 3.6 & 7.5

void setup() {
  Serial.begin(9600);     // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  motor.setSpeed(1);  // 1 - 10 rpm  - cannot go below 1 
  // Example  
  //  motor.step(100, FORWARD, SINGLE); // #ofsteps, direction, (SINGLE, INTERLEAVE, DOUBLE, MICROSTEP) 
  //  motor.release(); // Motor free spins
  //  delay(1000);
}

void loop() {

 motor.step(500, FORWARD, INTERLEAVE); 
 delay(1000);
 motor.step(500, BACKWARD, INTERLEAVE);
 delay (1000); 

}

I'd say you're mostly there

void setup () 
{
...
...
  direction = FORWARD;
}

void loop() {

if (FORWARD == direction) {
  if (digitalRead (fwdEndStop)) {
    direction = REVERSE;
    delay (1000);
  }
} else {
  if (digitalRead (revEndStop)) {
    direction = FORWARD;
    delay (1000);
  }

}
motor.step(5, direction, INTERLEAVE);  // or some other small increment. Like 1.
}

AWOL, et al

Thanks so much for your help. - this is what I've got so far, but its not validating. I think that it refers to the value of 'direction' being changed in the 'if/else" logic. I will also have to use analog pins as digital ins, and am confused about the pullup resistors. Could anybody tell me if that part is looking ok?

Many, many thanks.

The error message:

"error: redefinition of 'void setup()'
 In function 'void setup()':
error: redefinition of 'void setup()' In function 'void loop()':"

The sketch:

void setup() 
{

  int fwdEndStop = 14; //analog pins
  int revEndStop = 15; //analog pins
  pinMode(fwdEndStop, INPUT);  
  digitalWrite(fwdEndStop, HIGH); //Confused - do I need to engage the pullup resistors on analog pins if they are set as inputs?
  pinMode(revEndStop, INPUT);  
  digitalWrite(revEndStop, HIGH); //Confused - do I need to engage the pullup resistors on analog pins if they are set as inputs? 
  int val = 0; 
  direction = FORWARD;

}

void loop() 
{

if (FORWARD == direction) {
  if (digitalRead (fwdEndStop)) {
    direction = REVERSE;
    delay (1000);
  }
} else {
  if (digitalRead (revEndStop)) {
    direction = FORWARD;
    delay (1000);
  }

}
motor.step(5, direction, INTERLEAVE);  // or some other small increment. Like 1.
}

The code you posted is not what you compiled, or is not all of it, or you wouldn't get the errors you said..
First, "direction" has not been defined (before "setup ()").
Second, in the bit you quoted, "motor" is not defined.

#include <AFMotor.h>

AF_Stepper motor(200, 2);

#define FORWARD 0 // whatever
#define REVERSE (!FORWARD)  //whatever else

int fwdEndStop = 14; //analog pins
int revEndStop = 15; //analog pins
int val = 0;
int  direction = FORWARD;

void setup()
{

  pinMode(fwdEndStop, INPUT);
  digitalWrite(fwdEndStop, HIGH); //Confused - do I need to engage the pullup resistors on analog pins if they are set as inputs?
  pinMode(revEndStop, INPUT);
  digitalWrite(revEndStop, HIGH); //Confused - do I need to engage the pullup resistors on analog pins if they are set as inputs?
  motor.setSpeed(1);  // 1 - 10 rpm  - cannot go below 1
}

void loop()
{

if (FORWARD == direction) {
  if (digitalRead (fwdEndStop)) {
    direction = REVERSE;
    delay (1000);
  }
} else {
  if (digitalRead (revEndStop)) {
    direction = FORWARD;
    delay (1000);
  }

}
motor.step(5, direction, INTERLEAVE);  // or some other small increment. Like 1.
}

[EDIT] NB there is no motor "STOP" command in this sketch -you need to put one before each "delay(1000)"

Awol, et all

Serious thanks are in order. Never expected anyone to be interested enough to stick it through this far. Learning lots.

Here is the sketch so far. Compiles just fine now. Not to be contrary but what I posted was verbatim and error remains. That said I realize that I omitted a line from the Lady Ada sketch I was working from regarding motor.

AWOL -
I have to say that Im terribly confused by #define
Specifically what does the '0' represent in
#define FORWARD 0// whatever? :-?

Also can someone tell me why this is necessary?

[EDIT] NB there is no motor "STOP" command in this sketch  -you need  to put one before each "delay(1000)"

I assume that your refering to something like
"motor.run(RELEASE); "

Many many thanks.

#include <AFMotor.h>

AF_Stepper motor(200, 2);

#define FORWARD 0// whatever
#define REVERSE (!FORWARD )  //whatever else
int fwdEndStop = 14; //analog pins
int revEndStop = 15; //analog pins
int direction = FORWARD;

void setup() 
{  
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  motor.setSpeed(1);

  pinMode(fwdEndStop, INPUT);
  digitalWrite(fwdEndStop, HIGH); //Confused - do I need to engage the pullup resistors on analog pins if they are set as inputs?
  pinMode(revEndStop, INPUT);
  digitalWrite(revEndStop, HIGH); //Confused - do I need to engage the pullup resistors on analog pins if they are set as inputs?
}

void loop()
{

if (FORWARD == direction) {
  if (digitalRead (fwdEndStop)) {
    direction = REVERSE;
    delay (1000);
  }
} else {
  if (digitalRead (revEndStop)) {
    direction = FORWARD;
    delay (1000);
  }

}
motor.step(5, direction, INTERLEAVE);  // or some other small increment. Like 1.
}

I have to say that Im terribly confused by #define
Specifically what does the '0' represent in
#define FORWARD 0// whatever? :-?

The #define directive simply replaces the token right after the #define with the preceding one.

This will result in something like:

int direction = FORWARD;

Being EXACTLY the same as:

int direction = 0;

Because the text (/token) FORWARD has been replaced by 0.

Also can someone tell me why this is necessary?

This is not necessary, but it helps keeping the code readable and modifiable.

The same can be done by doing:

const byte FORWARD = 0;
const byte BACKWARD = 1;

Or even:

enum MotorStates = {
  FORWARD,
  BACKWARD
}

It's just a tool, not a necessity. :slight_smile:

As AlphaBeta said, it just helps remember what zero or one or 3.14 means in a particular situation or context.

I'm an old-school 'C' programmer, so I tend to use "#define" a lot, but the new-fangled C++ has ways around using it quite so much.

It can save you program space too:

#define fwdEndStop 14

or

const int fwdEndStop = 14;

will both usually compile to a (slightly) shorter program than

int fwdEndStop = 14;

This isn't normally as much of a consideration as readability, but if you're scrabbling around for those last few bytes of memory, it could be useful.

"#define" introduces a macro replacement; it doesn't itself use any run-time memory at all, whereas
"int fwdEndStop;" reserves two bytes of RAM.
"#define" can also be used to implement small code segments, like "abs", which could be declared as

#define abs (x)  (((x)< 0) ? -(x):(x) )

So every time the compiler sees "abs (index);" in a sketch, it replaces it with the short code segment

(((index)< 0) ? -(index): (index))

which may or may not be meaningful...