Serial Monitor read values only one time

Hello,
I want to control DC motor from serial monitor and I have magnetic reed switch as position feedback and control.
I connected DC motor to DC motor driver using H-Bridge ( pins 11, 12 & 13) and the reed switch to pin 2.

I want to control it by getting new position values (NP) from serial monitor and compare it with the updated reed switch counter by moving left or right and I add BREAKING to stop the motor at the accurate position to cancel motor inertia

Finally, it is working okay for only one loop and when I'm try to send new position value, no thing is happening.

I tried while , if and for but while only working as if continue to move endless.

Below is my code , Thanks in advance :smiley:

const int  ReedPin = 2;    


int ReedCounter = 0;   
int ReedState = 0;         
int lastReedState = 0; 
int NP=0 ;

int Out_A1 = 11;
int Out_A2 = 12;
int Out_EN = 13; 

void setup() {
  
    pinMode(Out_A1, OUTPUT);
    pinMode(Out_A2, OUTPUT);
    pinMode(Out_EN, OUTPUT);
    digitalWrite(Out_A1, LOW); 
    digitalWrite(Out_A2, LOW); 
    digitalWrite(Out_EN, LOW);
    pinMode(ReedPin, INPUT_PULLUP);
 
  Serial.begin(9600);
}

void loop() {
  
 while (Serial.available() > 0){

 NP = Serial.parseInt();
Serial.println(NP);
 
 
  
  while ( NP > ReedCounter) {
    RIGHT();
   
  }
  while (NP < ReedCounter){
    LEFT();
  }
  
  while (NP == ReedCounter)
  {
   BREAKING();
  }
 
    
}
}


void RIGHT(){
  digitalWrite(Out_A2, LOW);
  digitalWrite(Out_A1, HIGH); 
  digitalWrite(Out_EN, HIGH);
  
    ReedState = digitalRead(ReedPin); 

   if ((ReedState != lastReedState) && (ReedState == HIGH))
  {
      ReedCounter++;
       Serial.print("number of Reed Pulses:  ");
      Serial.println(ReedCounter);
    
  }
    lastReedState = ReedState;  

}


void LEFT(){
  digitalWrite(Out_A1, LOW); 
  digitalWrite(Out_A2, HIGH);
  digitalWrite(Out_EN, HIGH);
      ReedState = digitalRead(ReedPin); 

  if ((ReedState != lastReedState) && (ReedState == HIGH))
   {
      ReedCounter--;
    Serial.print("number of Reed Pulses:  ");
      Serial.println(ReedCounter);
    
  }
    lastReedState = ReedState;  

}


void BREAKING(){
  digitalWrite(Out_A1, HIGH); 
  digitalWrite(Out_A2, HIGH);
  digitalWrite(Out_EN, HIGH);
  
delay(50);
  
  digitalWrite(Out_A1, LOW); 
  digitalWrite(Out_A2, LOW);
  digitalWrite(Out_EN, LOW);
   ReedState = digitalRead(ReedPin); 

  if ((ReedState != lastReedState) && (ReedState == HIGH))
   {
      ReedCounter++;
    Serial.print("number of EXTRA Reed Pulses:  ");
      Serial.println(ReedCounter);
    
  }
    lastReedState = ReedState;
}
 while (Serial.available() > 0){

 NP = Serial.parseInt();

If you put the { on a new line, and properly indented your code, you'd see that every happens in the while loop. That is wrong. The while loop should be an if statement, and should ONLY get a new value.

Moving the motor should be independent from getting a new position.

I add BREAKING to stop the motor

Braking would be more effective.

Groove:
Braking would be more effective.

Only if you want to start it again.

Thanks Groove and PaulS for your replies.
I'm trying to control satellite dish linear actuator like this:

I calculated how many pulses to get full extend and I'm trying to use the reed switch counter to remember the current position and compare it with the new coming values to move whether left or right.

I tried the new line and if statement but still not working.

I tried the new line and if statement but still not working.

I changed my code. I'm not going to show it to you.

It doesn't work. Why not?

Who knows? The code probably does something. You haven't said what you did. You haven't said what the code did.

Thanks PaulS for your help

Here is the code:

const int  ReedPin = 2;    


int ReedCounter = 0;   
int ReedState = 0;         
int lastReedState = 0; 
int NP=0 ;

int Out_A1 = 11;
int Out_A2 = 12;
int Out_EN = 13; 

void setup() {
  
    pinMode(Out_A1, OUTPUT);
    pinMode(Out_A2, OUTPUT);
    pinMode(Out_EN, OUTPUT);
    digitalWrite(Out_A1, LOW); 
    digitalWrite(Out_A2, LOW); 
    digitalWrite(Out_EN, LOW);
    pinMode(ReedPin, INPUT_PULLUP);
 
  Serial.begin(9600);
}

void loop() {
  
 while (Serial.available() > 0)
 {

 NP = Serial.parseInt();
Serial.println(NP);
 
 
  
  if ( NP > ReedCounter) {
    RIGHT();
   
  }
  else if (NP < ReedCounter){
    LEFT();
  }
  
  else if (NP == ReedCounter)
  {
   BRAKING();
  }
 
    
}

}


void RIGHT(){
  digitalWrite(Out_A2, LOW);
  digitalWrite(Out_A1, HIGH); 
  digitalWrite(Out_EN, HIGH);
  
    ReedState = digitalRead(ReedPin); 

   if ((ReedState != lastReedState) && (ReedState == HIGH))
  {
      ReedCounter++;
       Serial.print("number of Reed Pulses:  ");
      Serial.println(ReedCounter);
    
  }
    lastReedState = ReedState;  

}


void LEFT(){
  digitalWrite(Out_A1, LOW); 
  digitalWrite(Out_A2, HIGH);
  digitalWrite(Out_EN, HIGH);
      ReedState = digitalRead(ReedPin); 

  if ((ReedState != lastReedState) && (ReedState == HIGH))
   {
      ReedCounter--;
    Serial.print("number of Reed Pulses:  ");
      Serial.println(ReedCounter);
    
  }
    lastReedState = ReedState;  

}


void BRAKING(){
  digitalWrite(Out_A1, HIGH); 
  digitalWrite(Out_A2, HIGH);
  digitalWrite(Out_EN, HIGH);
  
delay(50);
  
  digitalWrite(Out_A1, LOW); 
  digitalWrite(Out_A2, LOW);
  digitalWrite(Out_EN, LOW);
   ReedState = digitalRead(ReedPin); 

  if ((ReedState != lastReedState) && (ReedState == HIGH))
   {
      ReedCounter++;
    Serial.print("number of EXTRA Reed Pulses:  ");
      Serial.println(ReedCounter);
    
  }
    lastReedState = ReedState;
}

It keep moving for ever and serial print for number of Reed Pulses is not working :S

I have update

I found the the problem was with the braking loop because it never end this loop

while (NP == ReedCounter)
  {
   BREAKING();
  }

So, I changed the code to:

 while ( NP > ReedCounter) {
    RIGHT();
   
  }
  while (NP < ReedCounter){
    LEFT();
  }
  
  if (NP == ReedCounter)
  {
   BRAKING();
  }

And now here is a new problem:
Now it is reading new values from serial monitor but it keep moving left and right for only one new value for example when I entered 5 I received the follow:
5
number of Reed Pulses: 1
number of Reed Pulses: 2
number of Reed Pulses: 3
number of Reed Pulses: 4
number of Reed Pulses: 5
0
number of Reed Pulses: 4
number of Reed Pulses: 3
number of Reed Pulses: 2
number of Reed Pulses: 1
number of Reed Pulses: 0

I'm coming closer and if there is any updates I will post directly :smiley:

And now here is a new problem:
Now it is reading new values from serial monitor but it keep moving left and right for only one new value for example when I entered 5 I received the follow:

How does that differ from what you expect?

First the NP value = 0

lets assume that the range is from 0 to 10
if I entered 2, is should move to 2 and wait the new value
then, if I entered 5 it should move by 3 only in the same direction but is I entered 1 it should move by 1 in the opposite direction.

So, I was expecting to move 5 only like:
"
5
number of Reed Pulses: 1
number of Reed Pulses: 2
number of Reed Pulses: 3
number of Reed Pulses: 4
number of Reed Pulses: 5
"
and the stop waiting for the new coming value.

I found it
the serial monitor was sending 2 values each time; my entered value and 0 , I faced this issue yesterday and till now can't fully understand it (ASCII)
I changed the serial monitor from new line to no line ending and now it is working good.

Thanks a lot PaulS. :smiley: :smiley: