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');
}
}