Stepper motor control

Hi - I'm new to the forum - please excuse any mistakes or dumb questions.
I'm running a unipolar motor which has 200 steps per revolution. I use the stepper library to drive it and the hardware interface and code works fine. Driving in either direction I can set speed, step value etc. What I want to do is add a counter to allow me to move the motor and count each stop point. when I have reached a set number of "stops" I want to stop the stepper motor until a pushbutton is pressed, after which the process will repeat.
Can anybody point me in the right direction?
Thanks in advance
David

The normal protocol is to post your existing code (use the # button above) and then someone may be able to show you how to modify what you have to what you might need to accomplish your goal. Asking for a complete sketch from scratch probably won't get a lot of responses.

Good luck

Lefty

Thanks lefty - I wasn't sure how to post the code. I don't want complete code, just an example of using a counter procedure. Hopefully posting the code will get e some responses.
Thanks again
David

I don't seem to be able to attache the code so I've reproduced it below.

#include <stepper.h>

#define motorSteps 200 // sets the number of steps of the motor

#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11

int x = 0

Stepper myStepper (motorSteps,MotorPin1,motorPin2,motorPin3,motorPin4);

void setup() {
myStepper.setSpeed(60);

}

void loop(){
myStepper.step(10);
delay(1000);
x = x+1;
if (x=35) {x=0;}
}

I have removed the serial.begin and serial.println sections just to reduce my typing work for this post but when I include them to debug the code I can see the value of x increase and the stepper motor counts forward as required. However, what I want to do is step the motor forward, wait for a suitable time period and repeat until a value of say 35 loops has been reached. I then want to stop the stepper motor. I have tried resetting the value of myStepper.step to 0 when the value of x = 35 but this simply adds the value of myStepper together ( if I set the second value to 10 for example, after 35 loops of step value 10 I get 35 loops of step value 10+10.

Hope this makes sense - I am very new to this and am really learning through experimentation.
Thanks
David

if (x=35) {x=0;}

This is a problem. This evaluates x = 35, which is an assignment statement, assigning the value of 35 to x. An assignment statement, of other than 0, always evaluates to true. So, x will always be reset to 0.

If you want to step the motor some number of times, say 50, and then wait for a while, and then do it again:

int i = 0;

void loop()
{
   myStepper.step(1); // Or, however many steps...
   i++;
   delay(10);  // Wait a short while for the stepper to get there, if needed
   if(i == 50)
   {
       delay(10000); // Wait for 10 seconds
       i = 0;
   }
}

If you want the motor to move some number of steps when a button is pushed:

int oldState = LOW;

void loop()
{
   int newState = digitalRead(buttonPin);
   if(newState != oldState) // The button was pressed or released
   {
       if(newState == HIGH)
       {
           // Loop to step however many times you want
       }
   }
   oldState = newState;
}

Normally, you need to debounce a switch (delay a small period of time after the switch changes state), but, because there will be a delay while the motor steps, thedebouncing is taken care of.

Hi Paul,
Thanks very much for the help - it's really appreciated. I have just joined the Arduino community and am trying to re-educate myself and I've trawled through the posts and have seen your advise and responses several times.
I will try it tonight when I get home.
Best Regards
David

Hi Paul,
Whilst looking for possible solutions to my problem I found the post "SIMPLE CYCLE COUNTER". This also seems to have possibilities as a solution to my problem. This requires the addition of:
#define CYCLE_TIMES 50L // to give me say 50 cycles

and the addition in the loop section of:

for (long i = 0; i<CYCLE_TIMES;++i)

and:

for (ii) {i}

after the last statement in the loop.

I think I understand the first statements but do not understand what happens with "for (ii) {i} and unfortunately it has no explanation comments. Can you help me understand the logic behind these statements as I think they would be useful in future.
thanks
David

for (;;}{;}

is an infinite loop - those aren't "i"s they're semicolons.

It could also be written:

for (;;}{}

or for (;;};

I don't think that other thread is relevant. In that thread, the poster wanted loop to run some set number of times, and then stop.

The "solution" there was to make loop go into an endless loop after the desired number of calls to loop, so that it never ended.

That is not what you want to do.

Thanks Groove.
That explains it
Cheers
David

Thanks Paul,
Appreciate the explanation.
Cheers
David