I am currently working on a project with some friends. In short, we want to create a setup where when a button is pressed a thermal printer prints out a random sentence.
I am currently trying to get my code to work and to debug it with the Serial Monitor, but it prints out weird stuff.
The baud rate is the same for the monitor and the Serial.begin().
int checkSum[] = {-1, -1};
String stringPool[] =
{
"Satz 1", "Satz 2",
"Satz 3", "Satz 4",
"Satz 5", "Satz 6",
"Satz 7", "Satz 8",
"Satz 9", "Satz 10",
"Satz 11", "Satz 12",
"Satz 13", "Satz 14",
"Satz 15", "Satz 16",
"Satz 17", "Satz 18",
"Satz 19", "Satz 20",
"Satz 21", "Satz 22"
};
String chooseRandom ()
{
bool isMaybeThreeTimes;
int randomInt;
do
{
randomInt = random( 0, sizeof( stringPool ) -1 );
// first and further random number chosen
if( checkSum[0] == -1 ) { checkSum[0] = randomInt; }
else
{
checkSum[1] = checkSum[0];
checkSum[0] = randomInt;
}
if( randomInt == checkSum[0] && randomInt == checkSum[1] )
{
isMaybeThreeTimes = true;
}
else
{
isMaybeThreeTimes = false;
}
Serial.print("Current random value: " + randomInt);
delay(1000);
Serial.print("Last random value: " + checkSum[0]);
delay(1000);
Serial.print("Last last random value: " + checkSum[1]);
delay(1000);
Serial.print("Three times in row?: " + isMaybeThreeTimes);
delay(1000);
} while ( isMaybeThreeTimes );
return stringPool[randomInt];
}
void buttonPressed()
{
String stringToPrint = chooseRandom();
// Send the string to the adafruit printer.
Serial.println(stringToPrint);
}
void setup()
{
Serial.begin(19200);
delay(2000);
Serial.println("Setup finished");
randomSeed(analogRead(0));
}
void loop()
{
buttonPressed();
}
Any ideas why this is happening? I switched the baud rates changed delays and changed the places on where and how often and in which frequeny the method buttonPressed() is called.
sizeof( stringPool )
is not what you guess, if you want 22 or a similar number."Current random value: " + randomInt;
only works if randomInt is 0, and does never append something to the text, but increases the pointer to that text, which looks like truncating the first characters.