Serial screen monitor

Hi:

I'm developing a motor position control and is almost done. I would like to monitor the variables in order to tune the algorithm but for some reason when I use the serial.println option, the control changes its behavior and sometimes it goes out of control.

Is it that using the serial monitor absorb "steal" some computation capaticity from the arduino?

Serial output is buffered. If the buffer is full and you try to print more, serial.print blocks until there is space. So, if you’re doing a lot of printing, especially at a low baud rate, you may find that there are delays occurring, which possibly are causing you issues.

Hard to say without seeing the code though.

wildbill:
Serial output is buffered. If the buffer is full and you try to print more, serial.print blocks until there is space. So, if you’re doing a lot of printing, especially at a low baud rate, you may find that there are delays occurring, which possibly are causing you issues.

Hard to say without seeing the code though.

Here is the code

const int encoder0PinA = 2;
const int encoder0PinB = 3;
int MotorIN_A = 8; 
int MotorIN_B = 7;
int MotorPWM = 11; 

volatile double Motor_Position = 0;

double Angle = 0;

double Input = 0;
double Setpoint = 0;
double Output = 0;
double Error = 0;
volatile unsigned long lastTime;
volatile double errSum, lastErr;
double dT = 0;
double Kp = 2;
double Ki = 0.00015;

int SampleTime = 1;

void setup() {

  pinMode(encoder0PinA, INPUT); 
  pinMode(encoder0PinB, INPUT);

  attachInterrupt(0, doEncoder, CHANGE);

  Serial.begin (9600);
}

void loop(){

  Angle = (double)Motor_Position * 0.5389221556; 
  compute();

  if(Output > 0) {
    analogWrite(MotorPWM, Output);
    CCW();
  }
  else if(Output < 0){
    analogWrite(MotorPWM, -Output);
    CC();
  }
  else{
    STOP();
  }

  if( Angle < 1 & Angle > -1){ 
    STOP();
  }


}


void doEncoder(){
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)){
    Motor_Position++;
  }
  else {
    Motor_Position--;
  }
}

void CC(){
  digitalWrite(MotorIN_A,HIGH);
  digitalWrite(MotorIN_B,LOW);
}

void CCW(){
  digitalWrite(MotorIN_A,LOW);
  digitalWrite(MotorIN_B,HIGH);
}

void STOP(){
  analogWrite(MotorPWM, 0);
  digitalWrite(MotorIN_A,LOW);
  digitalWrite(MotorIN_B,LOW);
}

void compute(){
  
  unsigned long now = millis(); 
  dT = (double)(now - lastTime);
  
  if(dT >= SampleTime){  
  double Error = Setpoint - Angle;
  errSum += (Error * dT);

  Output = Kp * Error + Ki * errSum;
  
  if(Output>200){Output = 200;}
  else if(Output <-200){Output = -200;}
  else{}

  lastTime = now;
  }
}
  Serial.begin (9600);

Stone age speed. Pick up the pace! 115200 is supported.

  Angle = (double)Motor_Position * 0.5389221556;

The cast is unnecessary. The number of digits after the decimal point is silly. The Arduino can't store a value that accurately. The limit is 6 or 7 digits of precision.

Since you aren't actually writing anything to the serial port, and change in behavior when the Serial Monitor is opened has to come from the fact that the Arduino is reset in the process.

By the cast do you mean the "(double)"?