# Stop Counting Motor Steps In Void Loop

In my code below, I have it that when I press the button, (state == 1), within that if function the motor runs. And when it is off, the motor does not run.

The issue I am running into is even after the motor is off, it continues to count steps in the void loop. So after a period of time when pushing the button again, it will either go forward or backward depending on where it is in the loop.

What I need my code to do is stop counting steps once I turn the motor off so it picks back up where it left off in the void loop when the button is pressed again.

Thank you.

``````void loop(){

/*Here we are calling the rotate function to turn the stepper motor*/
/*One revolution of this motor is 1600 microsteps*/
/*driver is 8 microstep/step operation*/
/*Motor runs at 200 steps for every 360 degree rotation*/

rotate(-1000, 0.15); //The motor rotates 1000 microsteps (5/8 of a revolution) clockwise with a speed of 0.15
delay(750);
rotate(1000, 0.15); //The motor rotates 1000 microsteps (5/8 of a revolution) counter clockwise with a speed of 0.15
delay(750);

}
void rotate(int steps, float speed){

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (val == HIGH && old_val == LOW) {
state = 1-state;
}

old_val=val;

if (state == 1) {

/*This section looks at the 'steps' argument and stores 'HIGH' in the 'direction' variable if */
/*'steps' contains a positive number and 'LOW' if it contains a negative.*/
int direction;

if (steps > 0){
direction = HIGH;
}else{
direction = LOW;
}

speed = 1/speed * 70; //Calculating speed
steps = abs(steps); //Stores the absolute value of the content in 'steps' back into the 'steps' variable

digitalWrite(smDirectionPin, direction); //Writes the direction (from our if statement above), to the EasyDriver DIR pin

/*Steppin'*/
for (int i = 0; i < steps; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(smStepPin, LOW);
delayMicroseconds(speed);
}

digitalWrite(smEnablePin, LOW); //Enabling the motor, so it will move when asked to
cycle = count++;
// picture loop
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12);
u8g.drawStr( 0, 10, "Cycle");
u8g.drawStr( 0, 20, "Count:");
u8g.setPrintPos(40, 20);
u8g.print(cycle);
u8g.drawStr( 0, 35, "Lux:");
u8g.setPrintPos(25, 35);
u8g.print(event.light);
} while ( u8g.nextPage() );

} else{

digitalWrite(smEnablePin, HIGH); //Disabling the motor, so it will move when asked to
// picture loop
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12);
u8g.drawStr( 0, 10, "Cycle");
u8g.drawStr( 0, 20, "Count:");
u8g.setPrintPos(40, 20);
u8g.print(cycle);
u8g.drawStr( 0, 35, "Lux:");
u8g.setPrintPos(25, 35);
u8g.print(event.light);
} while( u8g.nextPage() );

}

}
``````

Set a flag when you stop the motor. Make counting dependant on the state of the flag.

Would I put the flag within the void loop or void rotate?

Yeowch! You have used a function but that function does everything! It reads inputs, makes decisions, drives outputs and writes stuff on the screen.

Functions should have simple names with 1, 2 or 3 words in the name. The name should describe what the function does. It should not do anything else. If it does something else (like writing to the screen) that's called a "side effect" and it's as bad as a medical side effect.

Start by reading Planning and Implementing an Arduino Program

Then look at your program again. Can you name each of the operations that occurs? Make a function for those: readInputs() decideWhenToRun() rotateMotor() displayScreen() and so on.