0

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.

Here a picture of the output: enter image description here

  • 1
    sizeof( stringPool ) is not what you guess, if you want 22 or a similar number. – DataFiddler Jan 10 at 11:45
  • 2
    "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. – DataFiddler Jan 10 at 11:51
2
int checkSum[] = {-1, -1};

const 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"
                      };

const String& chooseRandom () {
  bool isMaybeThreeTimes;
  int randomInt;

  do {
      randomInt = random( 0, sizeof(stringPool) / sizeof(stringPool[0]));
      isMaybeThreeTimes = (randomInt == checkSum[0] && randomInt == checkSum[1] );

      Serial.print("Current random value: "); Serial.println(randomInt);
      delay(1000);
      Serial.print("Last random value: "); Serial.println(checkSum[0]);
      delay(1000);
      Serial.print("Last last random value: "); Serial.println(checkSum[1]);
      delay(1000);
      Serial.print("Three times in row?: "); 
      Serial.println(isMaybeThreeTimes?"Yes":"No");
      delay(1000);
      checkSum[1] = checkSum[0]; // for next run
      checkSum[0] = randomInt;
  } while (isMaybeThreeTimes);
  return stringPool[randomInt];
}

void buttonPressed()
{  // has nothing to do with button presses, just a test
  const String& stringToPrint = chooseRandom();  // waits 3 seconds
  // 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();
}

Some fixes, not tested. Edit: some more fixes, tested.

  • Thanks. Would you mind explaining what was wrong with my code? – Cedric Jansen Jan 10 at 12:48
  • see my comments to your post. Mainly you have a strange idea about the + operator. "Hello" +1 is "ello" of course :) – DataFiddler Jan 10 at 12:55
  • Oh I see. I thought I could contencate the int and the string with the + operator. I haven't worked with c++ or c yet ( not sure what language is used here ) and have not a lot of training working with pointers ( the &/* stuff). I just used my Java background and tried to get it to work. I will better look into some syntax tutorials for arduino! Thanks again. – Cedric Jansen Jan 10 at 13:03
  • 1
    it's c++ and the main difference to Java is that you should not: use Arduino String objects for texts. – DataFiddler Jan 10 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.