How to start and stop my stepper motor program

Hi, i am very new to arduino and so far i have about 70% of my code working, however, i want my main loop gain to be able to pause and then continue from when it left off. The program controls a stepper motor. The code is below :

#include <Stepper.h>

int delaylength = 5; 
unsigned  int average;
const int NumAverages = 10;
volatile boolean flag = false;
double currentstepnumber=0;
float angle;
double stepsPerRevolution=200;
int buttonPin = 30; 
boolean buttonState= HIGH ;


int storeAnalogValue() {     // fuction to store and average analog read for a 6 times 
 
 unsigned long accumulator = 0;
  for(int i=0; i<NumAverages; i++) {
    accumulator += analogRead(A5);
    average= accumulator/NumAverages;
  }
}
void enableChannel(int pinA, int pinB) 

{
  digitalWrite(pinA, LOW);  //ENABLE CH A because brake is off so current passes through coil (set low)
  digitalWrite(pinB, HIGH); //DISABLE CH B because brake is on so current passes does not through coil (set high)
  
}
void motorpinchange()// flag function to show  when pin has changed so it can be intterrupted and read
{
  flag = true;
}

void setPinDirection(int pin, int direction_pin, int pin_level) // i.e. setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255(-255) spein backwards(100% duty cycle)
}

void setup() {
  Serial.begin(115200);
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW 
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW 
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  pinMode(buttonPin, INPUT_PULLUP);
  
}

void loop(){
   
  buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
    Serial.println(buttonState);
  do{
  
   if(currentstepnumber ==200){
      currentstepnumber= 0;
       }
      
    enableChannel(9, 8); // Enable CH A
   setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
    if (flag){
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13),motorpinchange ,CHANGE);
      }
       
   
   storeAnalogValue();
   Serial.println(average);
   currentstepnumber ++;
     angle = ((currentstepnumber)*360)/(stepsPerRevolution);
     Serial.println(angle);
   delay(delaylength);
   
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
       if (flag){
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13),motorpinchange ,CHANGE);
      }
   
     storeAnalogValue();
    Serial.println(average);
    currentstepnumber ++;
     angle = ((currentstepnumber)*360)/(stepsPerRevolution);
     Serial.println(angle);
    delay(delaylength);
   
    
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
       if (flag){
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13),motorpinchange ,CHANGE);
      }
      
    storeAnalogValue();
    Serial.println(average);
    currentstepnumber ++;
     angle = ((currentstepnumber)*360)/(stepsPerRevolution);
     Serial.println(angle);
    delay(delaylength);
   
     
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
       if (flag){
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13),motorpinchange ,CHANGE);
      }
      
    storeAnalogValue(); 
    Serial.println(average);
    currentstepnumber ++;
     angle = ((currentstepnumber)*360)/(stepsPerRevolution);
     Serial.println(angle);
    delay(delaylength); 
   //Serial.println(buttonState);
   buttonState = digitalRead(buttonPin);
   //Serial.println(buttonState);
  }while(buttonState!=LOW);
}
}
 unsigned long accumulator = 0;
  for(int i=0; i<NumAverages; i++) {
    accumulator += analogRead(A5);
    average= accumulator/NumAverages;
  }

That's a pretty poor attempt at an average.

 unsigned long accumulator = 0;
  for(int i=0; i<NumAverages; i++) {
    accumulator += analogRead(A5);
  }
  average= accumulator/NumAverages;
  }

is a better approach.

Don't use DO WHILE (or WHILE or FOR) unless it can complete in under a millisec. Let loop() take care of the repetition - that's what it is for.

The use of long WHILE or FOR loops prevents a program from being responsive.

Have a look at the demo Several Things at a Time.

...R

then what is the right way to average?

uche_ofili7:
then what is the right way to average?

Didn't you read my reply?
Sum all the readings, then divide.

uche_ofili7:
then what is the right way to average?

Save each value in a circular buffer as it is obtained. Then iterating across the buffer at any time using FOR will only take a few microsecs.

A circular buffer is just an array with enough spaces for the number of items you want to average. Save each item in the next space. When the index gets to the end set it back to 0.

...R

I am in urgent need as i doing a final year project and I am stuck on how to implement the next functionality i want on my code

I am trying to achieve 2 things :
1)Press a push button A which starts the my motor and makes it move in one direction whilst taking analog read values at each step and then serial printing that and the angle which is an arithmetic operation of currentstepnumber.

2)Then i press another push button B which will reverse the motor and take it back to the initial position it started from; so kind of like a hardware reset so that i always know my starting point.

So far I have managed to make the motor turn in one direction when i hold pushbuttonA, and I have also made the motor turn in the other direction when i hold PushButton B. I want to change this so I only press PushbuttonA once and it moves in one direction and when I press PushbuttonA again it stops. Then Finally when I press Push buttonB, the stepper turns back to the position it was before push button A was pressed the first time.
I will highly appreciate any help .with this code because i am a beginner and though pseudo code is useful, i find it hard to visualise. Lastly I am using the arduino made motor shield which is based on a L298 full bridge to control the stepper

Thank you.

int motordelaylength = 5;

unsigned  int average;

const int NumAverages = 8;

volatile boolean flag = false;

double currentstepnumberA = 0;

double currentstepnumberB = 0;

float angle;

double stepsPerRevolution = 200;

const int buttonPin = 30;

boolean buttonState = HIGH ;

boolean buttonState2 = HIGH;

int measuredelaylength = 1;

const int buttonPin2 = 31;




int storeAnalogValue() {     // fuction to store and average analog read for a 6 times

  unsigned long accumulator = 0;
  for (int i = 0; i < NumAverages; i++)
 {
    accumulator += analogRead(A8);
  }
  average = accumulator / NumAverages;

  delay(measuredelaylength);
}

void enableChannel(int pinA, int pinB)

{
  digitalWrite(pinA, LOW);  //ENABLE CH A because brake is off so current passes through coil (set low)

  digitalWrite(pinB, HIGH); //DISABLE CH B because brake is on so current passes does not through coil (set high)


}
void motorpinchange()// flag function to show  when pin has changed so it can be intterrupted and read

{
  flag = true;
}

void setPinDirection(int pin, int direction_pin, int pin_level) // i.e. 

setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255(-255) spein backwards(100% duty cycle)
}

void setup() {
  Serial.begin(115200);

  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW

  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW

  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A

  pinMode(8, OUTPUT); //brake (disable) CH B

  pinMode(buttonPin, INPUT_PULLUP);

  pinMode(buttonPin2, INPUT_PULLUP);

}

void loop() {

  buttonState = digitalRead(buttonPin);

  buttonState2 = digitalRead(buttonPin2);

  if (buttonState == LOW) {

      if (currentstepnumberA == 200) {

        currentstepnumberA = 0;
      }

      enableChannel(9, 8); // Enable CH A

      setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A

      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }


      storeAnalogValue();
      Serial.println(average);
      currentstepnumberA ++;
      currentstepnumberB ++;
      angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);

      enableChannel(8, 9); // Enable CH B
      setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }

      storeAnalogValue();
      Serial.println(average);
      currentstepnumberA ++;
      currentstepnumberB ++;
      angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);


      enableChannel(9, 8); // Enable CH A
      setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }

      storeAnalogValue();
      Serial.println(average);
      currentstepnumberA ++;
      currentstepnumberB ++;
      angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);


      enableChannel(8, 9); // Enable CH B
      setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }

      storeAnalogValue();
      Serial.println(average);
      currentstepnumberA ++;
      currentstepnumberB ++;
      angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);

      buttonState = digitalRead(buttonPin);

    } 



   if (buttonState2 == LOW) {
   // while (currentstepnumberB != 0);
   // {
      enableChannel(9, 8); // Enable CH A
      setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }


     storeAnalogValue();
     Serial.println(average);
     currentstepnumberB --;
      angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);

      enableChannel(8, 9); // Enable CH B
      setPinDirection(13, 11,   HIGH); //Sets direction of CH B and Moves CH B
      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }

      storeAnalogValue();
      Serial.println(average);
      currentstepnumberB --;
      angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);


      enableChannel(9, 8); // Enable CH A
      setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }

      storeAnalogValue();
      Serial.println(average);
      currentstepnumberB --;
      angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);

      enableChannel(8, 9); // Enable CH B
      setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
      if (flag) {
        flag = false;
        detachInterrupt(digitalPinToInterrupt(12));
        attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
      }

      storeAnalogValue();
      Serial.println(average);
      currentstepnumberB --;
      angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
      Serial.println(angle);
      delay(motordelaylength);
    }
  }
  if (buttonState == LOW) {

Rather than move when the pin is LOW, move it when the pin becomes low, that is, when the pin transitions between HIGH and LOW.

I think there's a state-change example provided by the IDE.

Hi groove, I am not quite sure what you mean nad the comments in the example are not good enough

uche_ofili7:
Hi groove, I am not quite sure what you mean

I can't express it more simply.