Interrupting a Bipolar Motor in a while loop

So I have a program to control a motor in several different ways. What I need help with is while continuous operation of the motor in a while loop, how do i send a command through serial to break the loop? Here is my code and the problem area is in case 'F'

const int Step = 6;
const int Dir = 7;
int option;
int turns;
int steps;
int i;
char f;
char r;
char s;

void setup()
{
pinMode(Step,OUTPUT);
pinMode(Dir,OUTPUT);
Serial.begin(9600);
}

void loop()
{
Serial.println("Direction:");
Serial.println("f = Forward");
Serial.println("r = Reverse");
Serial.println("s = Stop");
Serial.println("F = Continuous Forward");
Serial.println("R = Continuous Reverse");
Serial.println("b = Return to Previous Position\n");
while(!Serial.available()){}
option = Serial.read();

switch(option)
{
case'f':
Serial.println("Forward Motion Selected\n");
digitalWrite(Dir, HIGH);

Serial.println("Enter Turns\n");
while (Serial.available() == 0);
turns = Serial.parseInt();

for(i=0; i<(turns*200); i++)
{
digitalWrite(Step,HIGH);
delayMicroseconds(500);
digitalWrite(Step,LOW);
delayMicroseconds(500);
steps++;
}
Serial.print(steps);
break;

case 'r':
Serial.println("Reverse Motion Selected\n");
digitalWrite(Dir, LOW);

Serial.println("Enter Turns\n");
while (Serial.available() == 0);
turns = Serial.parseInt();

for(i=0; i<(turns*200); i++)
{
digitalWrite(Step,HIGH);
delayMicroseconds(500);
digitalWrite(Step,LOW);
delayMicroseconds(500);
steps--;
}
Serial.print(steps);
break;

case 's':
Serial.println("Stop");
digitalWrite(Dir, LOW);
delayMicroseconds(500);
digitalWrite(Step, LOW);
delayMicroseconds(500);
break;

case 'F':
Serial.println("Continuous Forward\n");
digitalWrite(Dir, HIGH);
while(option == 'F')
{
digitalWrite(Step,HIGH);
delayMicroseconds(500);
digitalWrite(Step,LOW);
delayMicroseconds(500);
steps++;
}
Serial.print(steps);

case 'R':
Serial.println("Continuous Forward\n");
digitalWrite(Dir, LOW);
while(option == 'R')
{
digitalWrite(Step,HIGH);
delayMicroseconds(500);
digitalWrite(Step,LOW);
delayMicroseconds(500);
steps++;
}
Serial.print(steps);

case 'b':
Serial.println("Moving to Previous Position\n");

if(steps > 0)
{
digitalWrite(Dir, LOW);
turns = steps;

for(i=0; i<(turns); i++)
{
digitalWrite(Step,HIGH);
delayMicroseconds(500);
digitalWrite(Step,LOW);
delayMicroseconds(500);
steps--;
}
}

if(steps < 0)
{
digitalWrite(Dir, HIGH);
turns = steps;

for(i=0; i<(-turns); i++)
{
digitalWrite(Step,HIGH);
delayMicroseconds(500);
digitalWrite(Step,LOW);
delayMicroseconds(500);
steps--;
}
}
Serial.flush();
break;
}
}

If you want a responsive system do not use WHILE or FOR for anything that takes longer than a few microseconds.

Allow the loop() function to do the iteration for you and use an IF test to check if the thing should terminate.

Have a look at the demo Several Things at a Time

...R

Awesome, thanks guys I will tweak this thing. Didn't realize there wasn't a check function that could be thrown in the while loop.

OK so I've gotten a little farther. I've taken out the delays and while loops and replaced them with for loops. Now i'm trying to create an object that is called in the switch statement, call it case 'F', that continuously runs the motor until a serial command 's' is given. Can anyone give me any advice?

const int Step = 6;
const int Dir = 7;
int option;
int turns;
int count;
int totalTurns;
int i;
int j;

void forward()
{
char s;
Serial.println("Continuous Forward\n");
digitalWrite(Dir, HIGH);

for(j=0; j<400; j++)
{
if(j == 0)
{
digitalWrite(Step,HIGH);
}

else if(j == 399)
{
digitalWrite(Step,LOW);
}
else if(Serial.read() = s)
{
break;
}
}
}

void setup()
{
pinMode(Step,OUTPUT);
pinMode(Dir,OUTPUT);
Serial.begin(9600);
}

void loop()
{
Serial.println("Controls:");
Serial.println("f = Forward");
Serial.println("r = Reverse");
Serial.println("s = Stop");
Serial.println("F = Continuous Forward");
Serial.println("R = Continuous Reverse");
Serial.println("b = Return to Previous Position");
Serial.println("z = Return to Zero Position\n");
while(!Serial.available()){}
option = Serial.read();

switch(option)
{
case'f':
count = 0;
digitalWrite(Dir,HIGH);
Serial.println("Advancing, Please Enter The Number Of Turns:");
while (Serial.available() == 0);
turns = Serial.parseInt();
count = turns;

for(i=0; i<(turns*200); i++)
{

for(j=0; j<400; j++)
{

if(j == 0)
{
digitalWrite(Step,HIGH);
}

else if(j == 399)
{
digitalWrite(Step,LOW);
}
}
}

Serial.print(count);
Serial.println("\n");
break;

case'r':
count = 0;
digitalWrite(Dir,LOW);
Serial.println("Reversing, Please Enter The Number Of Turns:");
while (Serial.available() == 0);
turns = Serial.parseInt();
count = -turns;

for(i=0; i<(turns*200); i++)
{

for(j=0; j<400; j++)
{

if(j == 0)
{
digitalWrite(Step,HIGH);
}

else if(j == 399)
{
digitalWrite(Step,LOW);
}
}
}
Serial.print(count);
Serial.println("\n");
break;

case 'b':
Serial.println("Moving to Previous Position.\n");

if(count > 0)
{
digitalWrite(Dir, LOW);

for(i=0; i<(count*200); i++)
{

for(j=0; j<400; j++)
{

if(j == 0)
{
digitalWrite(Step,HIGH);
}

else if(j == 399)
{
digitalWrite(Step,LOW);
}
}
}
}

if(count < 0)
{
digitalWrite(Dir, HIGH);

for(i=0; i<(-count*200); i++)
{

for(j=0; j<400; j++)
{

if(j == 0)
{
digitalWrite(Step,HIGH);
}

else if(j == 399)
{
digitalWrite(Step,LOW);
}
}
}
}
count = -count;
break;

case 'z':
Serial.println("Moving to Zero Position.\n");

if(totalTurns > 0)
{
digitalWrite(Dir, LOW);

for(i=0; i<(totalTurns*200); i++)
{

for(j=0; j<400; j++)
{

if(j == 0)
{
digitalWrite(Step,HIGH);
}

else if(j == 399)
{
digitalWrite(Step,LOW);
}
}
}
}

if(totalTurns < 0)
{
digitalWrite(Dir, HIGH);

for(i=0; i<(-totalTurns*200); i++)
{

for(j=0; j<400; j++)
{

if(j == 0)
{
digitalWrite(Step,HIGH);
}

else if(j == 399)
{
digitalWrite(Step,LOW);
}
}
}
}

count = -totalTurns;
break;
}
totalTurns += count;
Serial.println("Current Position:");
Serial.print(totalTurns);
Serial.println("\n");
}

@michaelkm8, Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

...R

Here's the code tagged version. So the reason i'm using the for loops is because i'm using 'while(!Serial.available()){}' which stops the loop when it starts over or else i'd just get a bunch of serial.println printed out continuously. So if the loops are a problem how do I get it to not print out the statements every time?

const int Step = 6;
const int Dir = 7;
int option;
int turns;
int count;
int totalTurns;
int i;
int j;

void forward()
{
 int breakout;
 Serial.println("Advancing Continuously\n");
 digitalWrite(Dir, HIGH);

 for(j=0; j<400; j++)
 {
  
  if(j == 0)
  {
   digitalWrite(Step,HIGH);
  }

  else if(j == 399)
  {
   digitalWrite(Step,LOW);
  }
  
  else if()
  {
   break;
  }
  }
 }

void setup()
{
 pinMode(Step,OUTPUT);
 pinMode(Dir,OUTPUT);
 Serial.begin(9600);
}

void loop()
{
 Serial.println("Controls:");
 Serial.println("f = Forward");
 Serial.println("r = Reverse");
 Serial.println("s = Stop");
 Serial.println("F = Continuous Forward");
 Serial.println("R = Continuous Reverse");
 Serial.println("b = Return to Previous Position");
 Serial.println("z = Return to Zero Position\n");
 while(!Serial.available()){}
 option = Serial.read();

  switch(option)
  {
   case'f':
    count = 0;
    digitalWrite(Dir,HIGH);
    Serial.println("Advancing, Please Enter The Number Of Turns:");
    while (Serial.available() == 0);
    turns = Serial.parseInt();
    count = turns;

    for(i=0; i<(turns*200); i++)
    {

     for(j=0; j<400; j++)
     {

      if(j == 0)
      {
       digitalWrite(Step,HIGH);
      }

      else if(j == 399)
      {
       digitalWrite(Step,LOW);
      }
     }
    }

    Serial.print(count);
    Serial.println("\n");
    break;

   case'r':
    count = 0;
    digitalWrite(Dir,LOW);
    Serial.println("Reversing, Please Enter The Number Of Turns:");
    while (Serial.available() == 0);
    turns = Serial.parseInt();
    count = -turns;

    for(i=0; i<(turns*200); i++)
    {

     for(j=0; j<400; j++)
     {

      if(j == 0)
      {
       digitalWrite(Step,HIGH);
      }

      else if(j == 399)
      {
       digitalWrite(Step,LOW);
      }
     }
    }
    Serial.print(count);
    Serial.println("\n");
    break;

   case 'F':
    forward();
    break;

   case 'b':
    Serial.println("Moving to Previous Position.\n");

    if(count > 0)
     {
      digitalWrite(Dir, LOW);
      
      for(i=0; i<(count*200); i++)
       {

        for(j=0; j<400; j++)
         {

          if(j == 0)
           {
            digitalWrite(Step,HIGH);
           }

          else if(j == 399)
           {
            digitalWrite(Step,LOW);
           }
         }
       }
     }

    if(count < 0)
     {
      digitalWrite(Dir, HIGH);
      
      for(i=0; i<(-count*200); i++)
       {

        for(j=0; j<400; j++)
         {

          if(j == 0)
           {
            digitalWrite(Step,HIGH);
           }

          else if(j == 399)
           {
            digitalWrite(Step,LOW);
           }
         }
       }
     }
     count = -count;
     break;

   case 'z':
    Serial.println("Moving to Zero Position.\n");

    if(totalTurns > 0)
     {
      digitalWrite(Dir, LOW);
      
      for(i=0; i<(totalTurns*200); i++)
       {

        for(j=0; j<400; j++)
         {

          if(j == 0)
           {
            digitalWrite(Step,HIGH);
           }

          else if(j == 399)
           {
            digitalWrite(Step,LOW);
           }
         }
       }
     }

    if(totalTurns < 0)
     {
      digitalWrite(Dir, HIGH);
      
      for(i=0; i<(-totalTurns*200); i++)
       {

        for(j=0; j<400; j++)
         {

          if(j == 0)
           {
            digitalWrite(Step,HIGH);
           }

          else if(j == 399)
           {
            digitalWrite(Step,LOW);
           }
         }
       }
     }

     count = -totalTurns;
     break;
  }
 totalTurns += count;
 Serial.println("Current Position:");
 Serial.print(totalTurns);
 Serial.println("\n");
}[code]

[/code]

As well as the link I gave you in Reply #2, have a look at Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. See how it deals with printing.

For a more comprehensive example have a look at Planning and Implementing a Program

And the second example in this Simple Stepper Code illustrates the use of buttons and a stepper without using FOR

...R