Odd Serial Monitor Output, Only After Upload.

Hello,

I am using an Arduino Due, and I am receiving strange characters in the first two lines of the serial monitor, and only after uploading to it. If I clear the monitor and reset the device without an upload (using the on-board reset button), it will restart and run just fine without any strangeness, every time.

It also appears to be showing stuff from my (trashy) Serial monitor prompt function, as well as this strangeness. But, prior to an upload about 15 uploads ago, it was just two characters (that I can't remember now. Maybe a Y and maybe the Euro symbol.). Thing is, the code is working fine after this. No other strange behavior and my positioning algorithm seems good to go.

It started after I changed the Baud rate, and I know it's somehow related. I've tried another speed and the original again. 9600 doesn't seem to produce anything, but 115200 and 19200 do. I also have a hunch it's related to the serial buffer having last been used for receiving the uploaded code, but I can't seem to find an answer to point me in the right direction, so I gotta ask for a bit of help here.

A BIT OF CONTEXT: Most of this code isn't actually my project code. I just got my Arduino Due yesterday, but I got started on the code over a month ago when I ordered it. I just took my position algorithm and slammed it into the Arduino IDE for testing purposes. I figured it would be a bad idea to throw a thousand lines of untested code right into operation, so I'm going through it all and hammering out the real world hardware bugs, like this... So, my problem isn't mission critical, yet... And so far, it's not actually affecting anything... However, I'll be tackling serial communication with motor controllers soon, and that's when I'm worried this might creep into the mission critical stuff, so ignoring it would be foolish on my part. Better to understand it now, so that I'm not spending a week trying to debug why my motor controllers are going wacky.

Also, I'll take any feedback anyone is willing to offer on the position algorithm, as it is the code that is in my main project. It's working, quite accurately, but if there are better more reliable ways, I'm all ears.... And, the Serial Prompt function is also kind of temporary. The plan is to actually handle operator control with a Windows 10 Executable at some point. So, that's a dumpster fire I'll worry about putting out once I get to it. (I have a feeling I'll be posting here again).

Thank You

/*
const int consoleBaud = 115200;

#define encoderChA_AUP 52
#define encoderChB_AUP 53

volatile int direction_AUP = 0;

volatile int rawEncoderPosition_AUP = 0; 

int posMoment = rawEncoderPosition_AUP;

const int encoderPulsesPerRev = 28;

volatile int revCount_AUP = 0;

bool consolePrompt_YN(String consoleMessage) {
    
    Serial.println();
    Serial.println("----------------");
    Serial.println(consoleMessage);
    Serial.println("----------------");
    Serial.println("[Y] for YES, [N] for NO or [B] to GO BACK");
    Serial.println("----------------");
    int operatorInput = -1;
  do {
      operatorInput = Serial.read();
  }while (operatorInput < 0);
    Serial.println("Your selection was");
    Serial.print(operatorInput);
    if (operatorInput == 'Y' || operatorInput == 'y') { 
      Serial.println("You chose, Yes...");
      return true;
      }
    if (operatorInput == 'N' || operatorInput == 'n') {
      Serial.println("You chose, No...");
      return false;
      }
    if (operatorInput == 'B' || operatorInput == 'b') { 
      Serial.println("Going Back...");
      return false; 
      }
  Serial.println("Unrecognized Input, try again.");
  Serial.println("----------------");
  Serial.println();
  return false;
}
// <--- A_UP ----------------------------------------->
/*ChA*/    void increment_ChA_AUP() {
           bool encA = digitalRead(encoderChA_AUP);
           bool encB = digitalRead(encoderChB_AUP);
           if ((encA && !encB) || (!encA && encB)) {
              ++rawEncoderPosition_AUP;
              if (rawEncoderPosition_AUP >= encoderPulsesPerRev) {
                rawEncoderPosition_AUP = 0; 
                ++revCount_AUP;             
              }
              direction_AUP = 1;
           }else {
              if (rawEncoderPosition_AUP <= 0) {
                rawEncoderPosition_AUP = encoderPulsesPerRev;
                --revCount_AUP;
              }
            --rawEncoderPosition_AUP;
            direction_AUP = -1;
            }
      } 
/*ChB*/ void increment_ChB_AUP() {
          bool encA = digitalRead(encoderChA_AUP);
          bool encB = digitalRead(encoderChB_AUP);
          if ((encA && encB) || (!encA && !encB)) {
            ++rawEncoderPosition_AUP;
            if (rawEncoderPosition_AUP >= encoderPulsesPerRev) {
              rawEncoderPosition_AUP = 0;
              ++revCount_AUP;
            }
          direction_AUP = 1;
        } else{
          if (rawEncoderPosition_AUP <= 0) {
            rawEncoderPosition_AUP = encoderPulsesPerRev;
            --revCount_AUP;
          }
          --rawEncoderPosition_AUP;
        }
      }


void setup() {
  pinMode(encoderChA_AUP, INPUT_PULLUP);
  pinMode(encoderChB_AUP, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(encoderChA_AUP), increment_ChA_AUP, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoderChB_AUP), increment_ChB_AUP, CHANGE);
  Serial.begin(consoleBaud);
}

void loop() {
  int cancelCommand = -1;
  if(consolePrompt_YN("Run Sequence?")){
    do{
      if(posMoment < rawEncoderPosition_AUP || posMoment > rawEncoderPosition_AUP){
        Serial.println(rawEncoderPosition_AUP * direction_AUP);
        posMoment = rawEncoderPosition_AUP;
      }
      cancelCommand = Serial.read();
    }while(cancelCommand != 'n');
  } 
}

Just to add: I'm turning the motor manually for this test, so only it's encoder is connected.

Nothing else, except a reliable USB cable is connected. No external power source for it, yet.

This happens also with an Arduino Uno and has to do with something left in a buffer or so. Uploading the sketch uses the same serial port. Therefor it only happens to that serial port and not with the other serial ports.

Serial ports: Serial - Arduino Reference

When you press the reset button and everything is okay, then your sketch is okay.

Sometimes I do this to be able to recognize the start:

void setup()
{
  Serial.begin( 9600);
  Serial.println();
  Serial.println( "----------------------------");
  Serial.println( "The sketch has started.");
  Serial.println( "----------------------------");
}

We don't use a while-loop and wait for input. Every advanced project requires that multiple things can be done at the same time. That can be with millis(), but then the long delay() and the while-loops that could wait forever have to go.

So, I owe you beer or something, Koepel.

I kept coming back to this thread to defend my use of the while loops, because I was 'working on other more important code in my super secret precious source file', and 'I hadn't taken the time to learn UI fundamentals and serial packet protocols yet,' and you know.... the usual, yadda yadda yadda... poor me, computers are hard.

Then I came here to make another excuse, because interfacing with my Arduino through the serial monitor sucked and I had nothing better to do than hope the .ino would grow a magical working UI, and as I read your reply again, I remembered the golden rule: if I wanted it my way, I needed to suck it up and learn how to make it, so with a small pool worth of coffee, I did. And oh boy, am I glad I finally did.

I just threw all that garbage out, and completely rebuilt from the ground up (over and over again as I graduated from shitty quick tutorials to eventually referencing the actual reference materials) Now, it's using some quick conditional branching to read a serial byte, only when available, construct it, only when new, and set a command flag only once it's complete. The actual commands are now handled by conditional branches, which are only tested if the command flag is set. So, my main loop is never tied up in anything, and it's amazing. My eyes hath been opened!

Now I've got all of my command coming from my C# Windows GUI, and because I took some extra time and developed a simple protocol for command codes and values on both ends, I have retained a functional serial monitor for output if I need it. Lots of work to do yet, but finally getting this down feels a lot like getting the saddle on the horse.

Great advice and thanks for your reply! :sunglasses:

PS: Honest to God, I've always hated While loops. I literally felt dirty for even using them... But in my defense, Code Satan made me do it.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.