Having real garbage issues with Serial.print. Im using the 2.3.2 version.
YES my Serial monitor is at 9600 (just like my code)
So I have a 3D array (only partially filled out) and global vars/const here :
const int NUM_ROOMS = 11;
const int MAX_STEPS = 7;
const short UP = 1;
const short DOWN = -1;
const short RIGHT = 10;
const short LEFT = -10;
char facingDirection = 'N';
/*
________________________
|-23 -13 -3 7 17 27 37 |
|-24 -14 -4 6 16 26 36 |
|-25 -15 -5 5 15 25 35 |
|-26 -16 -6 4 14 24 34 |
|-27 -17 -7 3 13 23 33 |
|-28 -18 -8 2 12 22 32 |
|-29 -19 -9 1 11 21 31 |
|-30 -20 -10 0 10 20 30 |
|____________^___________
(floor 2 could just be +100 or smthn)
*/
const int roomToRoom[NUM_ROOMS][NUM_ROOMS][MAX_STEPS]{
{
//Entrence
{}, //To Entrence
{ LEFT, DOWN }, //To R1
{ LEFT, LEFT, DOWN }, //To R2
{ LEFT, LEFT, UP, RIGHT }, //To R3
{ LEFT, LEFT, UP, LEFT }, //To R4
{ LEFT, LEFT, UP, UP, RIGHT }, //To R5
{ RIGHT, DOWN, RIGHT, DOWN, RIGHT }, //To R6
{ RIGHT, DOWN, RIGHT, DOWN, RIGHT, UP }, //To R7
{ RIGHT, DOWN, RIGHT, DOWN, RIGHT, DOWN }, //To R8
{ RIGHT, DOWN, RIGHT, DOWN, RIGHT, DOWN, LEFT }, //To R9
{ RIGHT, DOWN, RIGHT, DOWN, LEFT, DOWN }, //To R10
}
};
(Although it dosen't really matter, this code is a basis for a pathfinding code Im writing where the 3D array will hold directions from one room to any other room. The diagram shows how I want this to be done but its overall unimportant.)
Now I started with setup like this:
Serial.begin(9600);
Serial.println("START");
Serial.println();
for (int i = 0; i < NUM_ROOMS; i++) { //Prints all items
Serial.print("**********ROOM #");
Serial.print(i);
Serial.print("**********");
delay(100);
printArray(MAX_STEPS, 1, roomToRoom[0][i]);
Serial.println();
}
This works! Output here:
START
**********ROOM #0**********
0 0 0 0 0 0 0
**********ROOM #1**********
-10 -1 0 0 0 0 0
**********ROOM #2**********
-10 -10 -1 0 0 0 0
**********ROOM #3**********
-10 -10 1 10 0 0 0
**********ROOM #4**********
-10 -10 1 -10 0 0 0
**********ROOM #5**********
-10 -10 1 1 10 0 0
**********ROOM #6**********
10 -1 10 -1 10 0 0
**********ROOM #7**********
10 -1 10 -1 10 1 0
**********ROOM #8**********
10 -1 10 -1 10 -1 0
**********ROOM #9**********
10 -1 10 -1 10 -1 -10
**********ROOM #10**********
10 -1 10 -1 -10 -1 0
// For those curious what "printArray" is
void printArray(int xLength, int yLength, const int myArray[]) {
Serial.println("");
for (int i = 0; i < yLength; i++) {
for (int j = 0; j < xLength; j++) {
Serial.print(myArray[i * xLength + j]); // Adjusted indexing
Serial.print(" ");
delay(10);
}
Serial.println("");
}
}
All is fine and dandy right? I think the next step is making a function that converts 1 -> up/10 -> left and so on:
String cacPlaySound(int intDirection) { //Please ignore naming, its for later in the project
//Since I made a 2D map, N is up, E is left and so on...
switch (facingDirection) {
case 'N':
if (intDirection == 1) {
return "Fwd";
} else if (intDirection == 10) {
return "Rht";
facingDirection = 'E';
} else if (intDirection == -10) {
return "Lft";
facingDirection = 'W';
} else if (intDirection == -1) {
return "Bck";
}
break;
case 'S':
if (intDirection == -1) {
return "Fwd";
} else if (intDirection == -10) {
return "Rht";
facingDirection = 'W';
} else if (intDirection == 10) {
return "Lft";
facingDirection = 'E';
} else if (intDirection == 1) {
return "Bck";
}
break;
case 'E':
if (intDirection == 10) {
return "Fwd";
} else if (intDirection == -1) {
return "Rht";
facingDirection = 'S';
} else if (intDirection == 1) {
return "Lft";
facingDirection = 'N';
} else if (intDirection == -10) {
return "Bck";
}
break;
case 'W':
if (intDirection == -10) {
return "Fwd";
} else if (intDirection == 1) {
return "Rht";
facingDirection = 'N';
} else if (intDirection == -1) {
return "Lft";
facingDirection = 'S';
} else if (intDirection == 10) {
return "Bck";
}
break;
default:
return "ERROR 2";
break;
}
return "ERROR 1";
}
Turns out that was a mistake. New setup and output below:
void setup() {
Serial.begin(9600);
Serial.println("START");
Serial.println();
for (int i = 0; i < NUM_ROOMS; i++) { //For every room
Serial.print("************ROOM #");
Serial.print(i);
Serial.println("************");
for (int j = 0; j < MAX_STEPS; j++) { //For every step
Serial.print("step #"); // Print step #
Serial.print(j + 1);
Serial.print(" = ");
int myNextDirection = roomToRoom[0][i][j];
if (myNextDirection == 0) { //If there are no instructions, say so and move on
Serial.println("DONE");
delay(20);
continue;
}
Serial.print(myNextDirection); //Print # in the array (works)
Serial.print(", ");
Serial.print(cacPlaySound(myNextDirection)); //Print formmated direction (breaks)
Serial.println();
}
}
OUTPUT:
************ROOM #0************
step #1 = DONE
step #2= DONE
step #3 = DONE
step #4 = DONE
step #5 = DONE
step #6= DONE
step #7 = DONE
************ROOM #1*�$13#�V2�$13#�S���������aa�������$13#�T��$13#�X>�$13#�T
.... (NEVER ends)
"Well that sucks" I say "Lets try setting it to a set value and testing". Lo and behold, it breaks again:
//...
Serial.print(myNextDirection); //Print # in the array (works)
Serial.print(", ");
myNextDirection = 1; // <-------
Serial.print(cacPlaySound(myNextDirection)); //Print formmated direction (breaks)
/...
OUTPUT:
step #7 = DONE
************ROOM #6************
step #1 = 10,
step #2 = -1,
step #3 = 10,
step #4 = -1,
step #5 = 10,
step #6 = DONE
step #7 = DONE
************ROOM #7************
step #1 = 10,
step #2 = -1,
step #3 = 10,
step #4 = -1,
step #5 = 10,
step #6 = 1,
step #7 = DONE
************ROOM #8************
step #1 = 10,
step #2 = -1,
step #3 = 10,
step #4 = -1,
step #5 = 10,
step #6 = -1,
step #7 = DONE
************ROOM #9************
step #1 = 10,
step #2 = -1,
step #3 = 10,
step #4 = -1,
step #5 = 10,
step #6 = -1,
step #7 = -10,
************ROOM #10************
step #1 = 10,
step #2 = -1,
step #3 = 10,
step #4 = -1,
step #5 = -10,
step #6 = -1,
step #7 = DONE
//Cant even understand why it dosent work, there are defaults in the switch statement
Well lets try a less complicated function:
//(Same setup code as last time w/ set direction being 1)
String cacPlaySound(int intDirection) {
if (intDirection == 1) {
Serial.println("Up");
} else if (intDirection == 10) {
Serial.println("Rht");
} else if (intDirection == -1) {
Serial.println("Dwn");
} else if (intDirection == -10) {
Serial.println("Lft");
} else {
Serial.print("ERROR");
Serial.print("Was passed " + String(intDirection));
}
}
OUTPUT:
// # on left is diff b/c we only set value to 1 after printing actual value of array
************ROOM #0************
step #1 = DONE
step #2 = DONE
step #3 = DONE
step #4 = DONE
step #5 = DONE
step #6 = DONE
step #7 = DONE
************ROOM #1************
step #1 = -10, Up
step #2 = -1, Up
step #3 = DONE
step #4 = DONE
step #5 = DONE
step #6 = DONE
step #7 = DONE
************ROOM #2************
step #1 = -10, Up
step #2 = -10, Up
step #3 = -1, Up
step #4 = DONE
step #5 = DONE
step #6 = DONE
step #7 = DONE
//(and so on)
"Great! Now lets get rid of the manual set!". Whaddya know? Broken again:
OUTPUT:
************ROOM #0*****������
&ONE
step #2 = DONE
step #3 = DONE
step #4������
&ONE
step #6 = DONE
step #7 = DONE
*******���������****�$�$ = -10, Lft
�$S����������@�$'(a
#�Sa�a����@��X��
(Actually stops here)
Same thing if we go back to complicated function.
Also, YES my baud rate is 9600 on monitor.
I tried 115200 baud and I get same errors.
Anyone have any ideas? Im grasping at straws here. Only possible solution I can think of is my Arduino is broken but its been working fine until now. Ill get a new one tmrw.