Serial write to monitor one time and stop with a condition.

I have been trying to avoid serial write from slowing my program down. When my motor is in position I would like to print it's position one time to the serial monitor and then stop serial print . With the code below My motor no longer slows down, but the serial monitor keeps scrolling down. Is there a simple solution to print a message and then stop. any help would be greatly appreciated. also is there a good tutorial out there, I have been looking?

}
    if (target.distanceToGo()== 0){Serial.println(newPosition);
    Serial.println(Desired_position);
    
  }

Without the full code we can't see what's wrong. Is there some other serial.print in the code?

Well, after it's in position it stays there? So your if() condition is still true and it keeps printing. You need another variable to keep track of whether you have printed or not.

Try this...

bool WasPrinted = false;

    if (target.distanceToGo()== 0 && !WasPrinted) {
      WasPrinted = true;    
      Serial.println(newPosition);
      Serial.println(Desired_position);  
    }

Note how I also fixed up your braces and indenting - it's not easy to read a multi-line if() statement when you put some of the code on the same line.

Now if you need this to print again, say after the motor has moved, then you will need some way of setting WasPrinted back to false somewhere else in the program.

Thanks Morgan.

Ok it seemed simple when I looked at it. but it is reacting the same way. Forgive me If I did not implement correctly. I am fairly new to programming. The full code is below.

[quote]
#include <[color=#CC6600]AccelStepper[/color].h>
#include <[color=#CC6600]Encoder[/color].h>
[color=#CC6600]Encoder[/color] myEnc(2, 3);

[color=#CC6600]AccelStepper[/color] target(1, 6, 8); [color=#7E7E7E]// pin 6 = pulse, pin 8 = direction[/color]
[color=#CC6600]const[/color] [color=#CC6600]int[/color] buttonRT = 11;
[color=#CC6600]const[/color] [color=#CC6600]int[/color] buttonBK = 10;
[color=#CC6600]const[/color] [color=#CC6600]int[/color] buttonLT = 9;
[color=#CC6600]int[/color] Desired_position = 0;


[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
  target.[color=#CC6600]setMaxSpeed[/color](4000);
  target.[color=#CC6600]setSpeed[/color](4000);
  target.[color=#CC6600]setMinPulseWidth[/color](220);
  target.[color=#CC6600]setAcceleration[/color](2000);
  [color=#CC6600]pinMode[/color](buttonRT, [color=#006699]INPUT_PULLUP[/color]);
  [color=#CC6600]pinMode[/color](buttonBK, [color=#006699]INPUT_PULLUP[/color]); 
  [color=#CC6600]pinMode[/color](buttonLT, [color=#006699]INPUT_PULLUP[/color]);
  
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
}
[color=#CC6600]long[/color] oldPosition  = -999;
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color](){ 
   [color=#7E7E7E]// encoder section[/color]
  [color=#CC6600]long[/color] newPosition = myEnc.[color=#CC6600]read[/color]();
  [color=#CC6600]if[/color] (newPosition != oldPosition) {
    oldPosition = newPosition;
  }
    bool WasPrinted = [color=#CC6600]false[/color];

    [color=#CC6600]if[/color] (target.[color=#CC6600]distanceToGo[/color]()== 0 && !WasPrinted) {
      WasPrinted = [color=#CC6600]true[/color];    
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](newPosition);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](Desired_position);  
    }
   
  
  
    [color=#7E7E7E]// position control[/color]
   {[color=#CC6600]while[/color] ([color=#CC6600]digitalRead[/color](buttonRT) == [color=#006699]LOW[/color]) {Desired_position = 400;}
   [color=#7E7E7E]//wired N.O.[/color]
    [color=#CC6600]while[/color] ([color=#CC6600]digitalRead[/color](buttonBK) == [color=#006699]HIGH[/color]) {Desired_position = 0;}
    [color=#7E7E7E]//wired N.C.[/color]
    [color=#CC6600]while[/color] ([color=#CC6600]digitalRead[/color](buttonLT) == [color=#006699]LOW[/color]) {Desired_position = -400;}}
    [color=#7E7E7E]//wired N.O.[/color]
      
   
  
  target.[color=#CC6600]moveTo[/color](Desired_position);
  target.[color=#CC6600]setSpeed[/color](4000);
  target.[color=#CC6600]runSpeedToPosition[/color]();
    
     
   [color=#7E7E7E]// read the input pin:[/color]
  [color=#CC6600]int[/color] buttonState1 = [color=#CC6600]digitalRead[/color](buttonRT);
  [color=#CC6600]int[/color] buttonState2 = [color=#CC6600]digitalRead[/color](buttonBK);
  [color=#CC6600]int[/color] buttonState3 = [color=#CC6600]digitalRead[/color](buttonLT);
  [color=#7E7E7E]// print out the state of the button:[/color]
  [color=#7E7E7E]//Serial.print(buttonState1);[/color]
  [color=#7E7E7E]//Serial.print(buttonState2);[/color]
  [color=#7E7E7E]//Serial.print(buttonState3);[/color]
  [color=#7E7E7E]//Serial.print("Desired_position");[/color]
  [color=#7E7E7E]//Serial.print(Desired_position);[/color]
  [color=#7E7E7E]//delayMicroseconds(10); // delay in between reads for stability[/color]

}

 
 

[/quote]

Don't put code tags around a quote. Try to post your code again.

Ok it seemed simple when I looked at it. but it is reacting the same way. Forgive me If I did not implement correctly. I am fairly new to programming. The full code is below.

#include <AccelStepper.h>
#include <Encoder.h>
Encoder myEnc(2, 3);

AccelStepper target(1, 6, 8); // pin 6 = pulse, pin 8 = direction
const int buttonRT = 11;
const int buttonBK = 10;
const int buttonLT = 9;
int Desired_position = 0;


void setup() {
  target.setMaxSpeed(4000);
  target.setSpeed(4000);
  target.setMinPulseWidth(220);
  target.setAcceleration(2000);
  pinMode(buttonRT, INPUT_PULLUP);
  pinMode(buttonBK, INPUT_PULLUP); 
  pinMode(buttonLT, INPUT_PULLUP);
  
  Serial.begin(9600);
}
long oldPosition  = -999;
void loop(){ 
   // encoder section
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
  }
    bool WasPrinted = false;

    if (target.distanceToGo()== 0 && !WasPrinted) {
      WasPrinted = true;    
      Serial.println(newPosition);
      Serial.println(Desired_position);  
    }
   
  
  
    // position control
   {while (digitalRead(buttonRT) == LOW) {Desired_position = 400;}
   //wired N.O.
    while (digitalRead(buttonBK) == HIGH) {Desired_position = 0;}
    //wired N.C.
    while (digitalRead(buttonLT) == LOW) {Desired_position = -400;}}
    //wired N.O.
      
   
  
  target.moveTo(Desired_position);
  target.setSpeed(4000);
  target.runSpeedToPosition();
    
     
   // read the input pin:
  int buttonState1 = digitalRead(buttonRT);
  int buttonState2 = digitalRead(buttonBK);
  int buttonState3 = digitalRead(buttonLT);
  // print out the state of the button:
  //Serial.print(buttonState1);
  //Serial.print(buttonState2);
  //Serial.print(buttonState3);
  //Serial.print("Desired_position");
  //Serial.print(Desired_position);
  //delayMicroseconds(10); // delay in between reads for stability

}

Why didn't you just continue this in your other Thread which has all the background information?

...R

The last time It was slowing my motor down. The motor is working fine. This seems to be a more appropriate spot for this Question. I spent quit a bit of time on it and I am close to moving on. Since serial print takes up so much time I wanted to print a message once to keep the processor less busy.