While loop wont exit

Im trying to control the direction (clockwise and counterclockwise) of a stepper (28BJY) with a dual H-bridge made out of transistor. I tried this but what it is doing is that if I write a '1' in the serial monitor, it rotates clockwise but if I write a 0 it wont change anything. It can only take the '0' if a reset the arduino and write '0' instead, but again, if I write a '1', it wont take it. Is there a problem with the code?
Im a newbie so if you could help me by being really detailed, id appreciate it.
Thank you



int coila1 = 5;
int coila2 = 4;
int coilb1 = 3;
int coilb2 = 2;
int dir;
void setup()
{
pinMode(coila1, OUTPUT);
pinMode(coila2, OUTPUT);
pinMode(coilb1, OUTPUT);
pinMode(coilb2, OUTPUT);
Serial.begin(9600);
}


void loop()
{
  dir = Serial.read();
  if (dir == '1') 
  {
Serial.println("clockwise");
while (dir == '1')
{
digitalWrite(coila1, HIGH);
digitalWrite(coila2, HIGH);
digitalWrite(coilb1, LOW);
digitalWrite(coilb2, LOW);
delay(10);

digitalWrite(coila1, LOW);
digitalWrite(coila2, HIGH);
digitalWrite(coilb1, HIGH);
digitalWrite(coilb2, LOW);
delay(10);

digitalWrite(coila1, LOW);
digitalWrite(coila2, LOW);
digitalWrite(coilb1, HIGH);
digitalWrite(coilb2, HIGH);
delay(10);

digitalWrite(coila1, HIGH);
digitalWrite(coila2, LOW);
digitalWrite(coilb1, LOW);
digitalWrite(coilb2, HIGH);
delay(10);

}
}

if (dir == '0')
{
Serial.println("counterclockwise");
while (dir == '0')
{
digitalWrite(coila1, HIGH);
digitalWrite(coila2, LOW);
digitalWrite(coilb1, LOW);
digitalWrite(coilb2, LOW);
delay(10);

digitalWrite(coila1, LOW);
digitalWrite(coila2, LOW);
digitalWrite(coilb1, HIGH);
digitalWrite(coilb2, LOW);
delay(10);

digitalWrite(coila1, LOW);
digitalWrite(coila2, HIGH);
digitalWrite(coilb1, LOW);
digitalWrite(coilb2, LOW);
delay(10);

digitalWrite(coila1, LOW);
digitalWrite(coila2, LOW);
digitalWrite(coilb1, LOW);
digitalWrite(coilb2, HIGH);
delay(10);

}
}
}


Once you get into either while loop, you're stuck because the condition is based on the value of dir and you don't read it again in the loop, so they will run forever.

1 Like

I tried adding dir = Serial.read(); in the while loop and it prints correctly but now the stepper wont rotate at all.

Try using serial.available to find out if there is anything to read.

When you do that and it doesn't work, change the line ending in your serial monitor to none.

Hi,
How your serial monitor is configured:

  1. No line ending,
  2. Newline
  3. Carriage return
  4. Both NL & CR

Select "No line ending" and test your sketch again.

RV mineirin

clockwise for '1', counterclockwise for '0' and stop for any other character.

int coila1 = 5;
int coila2 = 4;
int coilb1 = 3;
int coilb2 = 2;
int dir;

void setup()
{
  pinMode(coila1, OUTPUT);
  pinMode(coila2, OUTPUT);
  pinMode(coilb1, OUTPUT);
  pinMode(coilb2, OUTPUT);
  Serial.begin(9600);
}


void loop()
{
  if (Serial.available())
  {
    char newDir = Serial.read();

    if (newDir != '\n' && newDir != '\r')
    {
      dir = newDir;
      if (dir == '1')
        Serial.println("clockwise");
      if (dir == '0')
        Serial.println("counterclockwise");
    }
  }

  // Move in the last select3ed direction
  if (dir == '1')
  {
    digitalWrite(coila1, HIGH);
    digitalWrite(coila2, HIGH);
    digitalWrite(coilb1, LOW);
    digitalWrite(coilb2, LOW);
    delay(10);

    digitalWrite(coila1, LOW);
    digitalWrite(coila2, HIGH);
    digitalWrite(coilb1, HIGH);
    digitalWrite(coilb2, LOW);
    delay(10);

    digitalWrite(coila1, LOW);
    digitalWrite(coila2, LOW);
    digitalWrite(coilb1, HIGH);
    digitalWrite(coilb2, HIGH);
    delay(10);

    digitalWrite(coila1, HIGH);
    digitalWrite(coila2, LOW);
    digitalWrite(coilb1, LOW);
    digitalWrite(coilb2, HIGH);
    delay(10);
  }
  else if (dir == '0')
  {
    digitalWrite(coila1, HIGH);
    digitalWrite(coila2, LOW);
    digitalWrite(coilb1, LOW);
    digitalWrite(coilb2, LOW);
    delay(10);

    digitalWrite(coila1, LOW);
    digitalWrite(coila2, LOW);
    digitalWrite(coilb1, HIGH);
    digitalWrite(coilb2, LOW);
    delay(10);

    digitalWrite(coila1, LOW);
    digitalWrite(coila2, HIGH);
    digitalWrite(coilb1, LOW);
    digitalWrite(coilb2, LOW);
    delay(10);

    digitalWrite(coila1, LOW);
    digitalWrite(coila2, LOW);
    digitalWrite(coilb1, LOW);
    digitalWrite(coilb2, HIGH);
    delay(10);
  }
}

to explain a little more: there is a buffer that receives the serial characters

Serial.available() looks up if there is something in the buffer and keeps the buffers content.

Serial.read() takes one character out of the buffer and then the buffer is empty until a new character arrives.
Makes sense to take it out of the buffer because the buffer has limited space.
But it means once you have executed a Serial.read() the only way to deal again with the received character is to store it in a variable.

if (Serial.read())

takes the character out of the buffer evaluates the if-condition and then the character is gone.

to re-use the character you have to code

myChar = Serial.read(); // store character into variable "myChar"

if (myChar == 'a") ....

best regards Stefan