Arduino Freezes on Serial Connection. ... Opening the Serial Monitor window

its vary odd ... wanted some Input on this ...

Every other time i open the serial monitor it locks up and no response close it hit the arduino ide lil serial monitor button and it works this time ..

5 mins later locks up sitting idle .

close it.
open it window opens blank white serial monitor with normal input line type in a letter for a response
i get nothing .

when it opens it should message the first print line every other time it does .. every other time it does not ?????

when i do get my opening message of port test it works for a few mins responding with serial prints from my case code then quits unexpectedly and has to be reopened twice to get a n input response again.

what the heck i'm on the most recent ide 1.89

below is the code im running on a arduino r3 uno from arduino legit board not a copy

#include <Servo.h>
const int ledPin =  13;
int pulse = 12;
int dir = 11;
int t1 = A1;
int t2 = A2;
Servo myservo1;  // create servo
Servo myservo2;  // create servo
//HX711 scale2(7, 8);
//stepper intagers
int acel = 900;
int dcel = 500;
int temp1 = 0;
int temp2 = 0;
int m1 = 0;
int m2 = 0;
String stringOne = "";
int j1 = 0;// jog wheel enable
// saw ints
int RPM = 0;
int DM = 0;
int incomingByte;
int x1 = 0;
int a1 = 0;
int b2 = 0;
int count = 0;
int diff = 0;
int analogValue;
//encoder intagers
//Electrical stats ints
int AMPLOAD = A0;
int ledPinST = 0;
int tempdspeed = 0;
// communacations value stores
String inString = "";
float gotoByte = 0;
float gotoabs = 0;
long tempsteps = 0;
long steps = 0;
long xspeed = 700;
long tempxspeed = 700;
long aspeed = 1200;
long dspeed = 1500;
byte estop;
int checkindex = 0;
byte inByte;
void setup() {
  Serial.begin (9600);
  checkindex = 1;
  pinMode(ledPin, OUTPUT);
  pinMode (dir, OUTPUT);
  pinMode (12, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (AMPLOAD, INPUT);
  myservo1.attach(2);
  myservo2.attach(3);
  m1 = 92;
  m2 = 92;
  temp1 = 92;
  temp2 = 92;
  delay(50);
  Serial.begin(9600);
  while (!Serial) {
    // wait for serial port to connect. Needed for native USB port only
  }
  delay(2);
  Serial.println("serial port test");
  Serial.println("Responding");
}
void loop() {
  if (j1 == 1) {
    jogwheel();
    Serial.println("k");
  }
  if (Serial.available()) {
    inByte = Serial.read(); // read the byte
    inString = inByte;
    Serial.println(inString); // send what you got
    switch (inByte) {// do stuff with byte
      case 'a':// SETTING ACCELERATION FROM REMOTE SERIAL
        aspeed = Serial.parseInt();
        Serial.println("k");
        break;
      case 'c':// CLEAR ALL STORED VALUES AND REST TO ZERO
        Serial.println("k");
        break;
      case 'g':// SETTING STEPS FROM REMOTE SERIAL
        gotoByte = Serial.parseFloat();
        Serial.println("k");
        break;
      case 's':// SET THE DECEL SPEED VIA REMOTE SERIAL
        dspeed = Serial.parseInt();
        dcel = dspeed;
        tempdspeed = dcel;
        Serial.println("k");
        break;
      case 'f':
        xspeed = Serial.parseFloat();
        tempxspeed = xspeed;
        break;
      case 'k':// debugging print all var to serial monitor
        Serial.println("k");
        Serial.println("Port2 Active");
        Serial.println("Port2 sending all variables");
        Serial.print("Port2 LED State =");
        Serial.println(ledPin);
        Serial.print("Port2 pulse State =");
        Serial.println(pulse);
        Serial.print("Port2 Dir State =");
        Serial.println(dir);
        Serial.print("  Port2 t1 =  ");
        Serial.println(t1);
        Serial.print("  Port2 t2 = ");
        Serial.println(t2);
        Serial.print("  Port2 Acel setting =  ");
        Serial.println(acel);
        Serial.print("  Port2 Decel setting =  ");
        Serial.println(dcel);
        Serial.print("  Port2 temp var is = ");
        Serial.println(temp1);
        Serial.print("  Port2 temp2 var is ");
        Serial.println(temp2);
        Serial.print("  Port2 m1 =  ");
        Serial.println(m1);
        Serial.print("  Port2 m2 =  ");
        Serial.println(m2);
        Serial.print("  Port2 string 1 = ");
        Serial.println(stringOne);
        Serial.print("  Port2 j1 =   ");
        Serial.println(j1);
        Serial.print("  Port2 rpm =  ");// jog wheel enable
        Serial.println(RPM);
        Serial.print("  Port2 Dm =  ");
        Serial.println(DM);
        Serial.print("  Port2 INcoming byte =  ");
        Serial.println( incomingByte);
        Serial.print("  Port2 X1 =  ");
        Serial.println(x1);
        Serial.print("  Port2 a1 = ");
        Serial.println(a1);
        Serial.print("  Port2 b2 =  ");
        Serial.println(b2);
        Serial.print("  Port2 count = ");
        Serial.println(count);
        Serial.print("  Port2 diff =  ");
        Serial.print("  Port2  ledpinst =");
        Serial.println(ledPinST);
        Serial.print("  Port2 temp speed = ");
        Serial.println(tempdspeed);
        Serial.print("  Port2 inbyte =  ");
        Serial.println(inByte);
        Serial.print("  Port2 in string = ");// incoming serial byte
        Serial.println(inString);
        Serial.print("  Port2 gotobyte =  ");
        Serial.println( gotoByte);
        Serial.print("  Port2 gotoabs = ");
        Serial.println( gotoabs);
        Serial.print("  Port2 temp step =");
        Serial.println( tempsteps);
        Serial.print("  Port2 step =");
        Serial.println( steps);
        Serial.print("  Port2 xspeed = ");
        Serial.println( xspeed);
        Serial.print("  Port2 temp x speed = ");
        Serial.println(tempxspeed);
        Serial.print("  Port2  a speed = ");
        Serial.println(aspeed);
        Serial.print("  Port2 d speed = ");
        Serial.println(dspeed);
        Serial.print("  Port2 z2 speed =  ");
        Serial.print("  Port2 estop =  ");
        Serial.println(estop);
        Serial.print("  Port2 check index =  ");
        Serial.println(checkindex);
        Serial.println("Send all Compleat");
        break;
      case 'X':
        myservo1.write(m1);
        myservo2.write(m2);
        Serial.print(" All motor stop");
        steps = 0;
        DM = 0;
        m1 = 92;
        m2 = 92;
        myservo1.write(m1);
        myservo2.write(m2);
        break;
      case 'Z':// more serial Debugging
        if (ledPinST == 0) {
          // turn LED on:
          ledPinST = 1;
          digitalWrite(ledPin, HIGH);
        }
        else
        {
          ledPinST = 0;
          digitalWrite(ledPin, LOW);
        }
        break;
    }
  }
}

void motors() {

}
void checkload() {
  a1 = x1;
}
void jogwheel() {
  Serial.println("k");
}
void jogup() {
  Serial.println("k");
}
void jogdown() {
  Serial.println("k");
}

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

You have a lot of Serial.print() statements in loop() and a low baud rate. If that piece of code repeats quickly it may overwhelm the Serial Output Buffer causing the Arduino to block because the buffer is being filled more quickly than it can be emptied.

...R

Thanks Robin i was not aware of this S class string issue Not sure i'm going to need a string at all .

so this may help ..

but these things are what causing Issues for the serial monitor to load and connect when the serial monitor is pressed on the ide ..

i have it less frequent today .. so now every 4th time i open the serial monitor it locks up the connection nothing comes thru or in/out .

restart the monitor and it runs fine ..
then on the forth open close cycle ... frezz again

ez8mm:
but these things are what causing Issues for the serial monitor to load and connect when the serial monitor is pressed on the ide ..

i have it less frequent today .. so now every 4th time i open the serial monitor it locks up the connection nothing comes thru or in/out .

restart the monitor and it runs fine ..
then on the forth open close cycle ... frezz again

Your description of the problem is not at all clear to me.

For example, why are you opening and closing the Serial Monitor. If you are using an Uno, Mega or nano that will cause the Arduino to reset. What Arduino are you using?

You need to describe in fine detail the exact steps that somebody would need to follow to replicate what you are doing. Write each step on a separate line. Start with the assumption that the Arduino IDE is not running and the Arduino board is not connected to anything.

Also, and very important, if you have made ANY changes to the program compared to what is in your Original Post, please post the latest version in your next Reply. Please do not overwrite the code in the Original Reply as it may be useful to compare the two.

...R