I've hit a Whammy: Nothing happens after typing in a reserved command

I'm writing a program that simulates the technical operations of the game show Press Your Luck. However, whenever I have the program in "game mode" and try to send the string "cashandspin" to the Serial Monitor after enabling the buttonPressed flag, nothing happens. I'm completely stumped because all other reserved strings function just fine. Can anyone identify the culprit in the provided code? Thanks!

Code:


 bool active;
 const int buzzer = 2;
 const int button = 8;
 const int LED = 4;
 String answer;
 int Spins;
 bool turnActive;
 bool gameMode;
 bool loopStopper;
 bool buttonPressed;
 int score;
 int PrizeValue;
 bool manualPrize;
 bool manualPrizeAndSpin;
 int instantCash;
 
 void setup(){
  pinMode(buzzer, OUTPUT);
  pinMode(LED, OUTPUT);   // Signaling LED
  pinMode(button, INPUT); // Big red button
  digitalWrite(LED, LOW); // Turn off LED
  Serial.begin(9600);
  Spins = 0;
  active = false;
  turnActive = false;
  gameMode = false;
  loopStopper = false;
  manualPrizeAndSpin = false;
  manualPrize = false;
 }

void loop(){
  if (!active && !gameMode && digitalRead(button) == HIGH){
    active = true;
    digitalWrite(LED, HIGH);    // Turn on signaling LED
    delay(250);
    for(int i=0; i<4; i++){         // Simulate the 4 beeps made whenever a contestant buzzes in
      tone(buzzer, 1200);
      delay(50);
      noTone(buzzer);
      delay(50);
    }
    Serial.println("type y for correct or anything else for incorrect\n");
    
  }
  responseChecker();  // Check whether player answers correctly or not
  boardMode();
}

void responseChecker(){
  if(Serial.available()){

    answer = Serial.readStringUntil('/n');
    answer.trim();
    if(active){
    if(answer.equals("y")){
      Serial.println("Correct! You win 3 spins!\n");
      for(int i=0; i<8; i++){
        digitalWrite(LED, LOW);
        delay(250);
        digitalWrite(LED, HIGH);
        delay(250);
      }
      Spins++;
      Spins++;
      Spins++;
      tone(buzzer, 1050);
      delay(200);
      noTone(buzzer);
      Serial.println("Spins:");
      Serial.println(Spins);
      digitalWrite(LED, LOW);
      active = false;
    } else{
      Serial.println("Incorrect answer. No spins awarded\n");
      Serial.println("Spins:");
      Serial.println(Spins);
      digitalWrite(LED, LOW);
      active = false;
    }
    }
    if(answer.equals("t")){
    Serial.println("Turn activated");
    turnActive = true;
    digitalWrite(LED, HIGH);
    }
    if(turnActive){
    if(answer.equals("1") && turnActive){
         Serial.println("Correct! You win 1 spin!\n");
      for(int i=0; i<8; i++){
        digitalWrite(LED, LOW);
        delay(250);
        digitalWrite(LED, HIGH);
        delay(250);
      }
      Spins++;
      tone(buzzer, 1050);
      delay(200);
      noTone(buzzer);
      Serial.println("Spins:");
      Serial.println(Spins);
      digitalWrite(LED, LOW);
      turnActive = false;
    } else if(answer.equals("0") && turnActive){
      Serial.println("Incorrect answer. No spins awarded\n");
      Serial.println("Spins:");
      Serial.println(Spins);
      digitalWrite(LED, LOW);
      turnActive = false;
    }
  }
  }
}

void boardMode(){
  if(answer.equals("b") && !loopStopper){
    Serial.println("big board mode");
    gameMode = true;
    loopStopper = true;
  } else if(answer.equals("q") && loopStopper){
    Serial.println("trivia mode");
    gameMode = false;
    loopStopper = false;
} if(gameMode){
  if(!buttonPressed && digitalRead(button) == HIGH){
    buttonPressed = true;
    Serial.println("Enter prize <No pun intended!>\n");
  }
  if(buttonPressed){
    if(answer.equals("whammy")){
      Serial.println("Oh no! A whammy!");
      delay(500);
      score = 0;
      Spins--;
      Serial.println("Spins:");
      Serial.println(Spins);
      Serial.println();
      Serial.println("Score:");
      Serial.println(score);
      Serial.println();
      buttonPressed = false;
      answer = "";
    } else if(answer.equals("prize")){
      PrizeValue = random(200, 1500);
      score += PrizeValue; 
      Serial.println("You win a prize! a value of: ");
      Serial.println(PrizeValue);
      Serial.println();
      Spins--;
      Serial.println("Spins:");
      Serial.println(Spins);
      Serial.println();
      Serial.println("Score:");
      Serial.println(score);
      Serial.println();
      buttonPressed = false;
      answer = "";
    } else if(answer.equals("bigprize")){
      PrizeValue = random(500, 5000);
      score += PrizeValue; 
      Serial.println("You win a prize! a value of: ");
      Serial.println(PrizeValue);
      Serial.println();
      Spins--;
      Serial.println("Spins:");
      Serial.println(Spins);
      Serial.println();
      Serial.println("Score:");
      Serial.println(score);
      Serial.println();
      buttonPressed = false;
      answer = "";
    } else if(answer.equals("spin") && !manualPrizeAndSpin){
      manualPrizeAndSpin = true;
      Serial.println("You selected cash and spin");
    }  else if(answer.equals("cash") && !manualPrize){
      manualPrize = true;
      Serial.println("You selected cash");
    }
    if(manualPrize){
      if(Serial.available() > 0){
      instantCash = Serial.parseInt();
      score += instantCash;
      Spins--;
      Serial.println("Spins:");
      Serial.println(Spins);
      Serial.println();
      Serial.println("Score:");
      Serial.println(score);
      Serial.println();
      manualPrize = false;
      buttonPressed = false;
      answer = "";
    }
    }if(manualPrizeAndSpin){
      if(Serial.available() > 0){
      instantCash = Serial.parseInt();
      score += instantCash;
      Serial.println("Spins:");
      Serial.println(Spins);
      Serial.println();
      Serial.println("Score:");
      Serial.println(score);
      Serial.println();
      manualPrizeAndSpin = false;
      buttonPressed = false;
      answer = "";
    }
    }
  }
}
}

Where in your code do you look for this string?

Did you mean '\n' perhaps?

a7

oh, lol I meant "spin"

You are using a variable of type String

const int LED = 4;
String answer; // <<<=== here
int Spins;

re-assigning a global variable of type String eats up all memory over time
and this can lead to very hard to find strange behaviour of the code
if the new re-assigning a value to a String overwrites other variable in RAM.

The SafeString-library does not have this flaw. But of course needs addtional memory
SafeStrings are almost a drop-in replacement for variables of type String
But offers mure functions.
Here is a code-version where I have added Serial-debug-output to make visible what is really happening in your code
You can disable this serial output by changing

#define debugActive true

to

#define debugActive false

you can install the SafeString-library with the library-manager of the Arduino-IDE

#define debugActive true

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *

// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName)  \
  if (debugActive) { \
    Serial.print( F(#myFixedText " "  #variableName"=") ); \
    Serial.println(variableName); \
  }
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if (debugActive) { \
      if ( millis() - intervalStartTime >= timeInterval ){ \
        intervalStartTime = millis(); \
        Serial.print( F(#myFixedText " "  #variableName"=") ); \
        Serial.println(variableName); \
      } \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// print only once when value has changed

#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if (debugActive) { \
      if ( lastState != variableName ){ \
        Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
        Serial.print(lastState); \
        Serial.print( F(" to ") ); \
        Serial.println(variableName); \
        lastState = variableName; \
      } \
    } \
  } while (false);
// end of macros dbg and dbgi and dbgc
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <SafeString.h>
createSafeString(multiPurpose_SS, 128);
createSafeString(answer_SS, 128);

bool active;
const int buzzer = 2;
const int button = 8;
const int LED = 4;
String answer;
int Spins;
bool turnActive;
bool gameMode;
bool loopStopper;
bool buttonPressed;
int score;
int PrizeValue;
bool manualPrize;
bool manualPrizeAndSpin;
int instantCash;

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(LED, OUTPUT);   // Signaling LED
  pinMode(button, INPUT); // Big red button
  digitalWrite(LED, LOW); // Turn off LED
  Serial.begin(9600);
  Spins = 0;
  active = false;
  turnActive = false;
  gameMode = false;
  loopStopper = false;
  manualPrizeAndSpin = false;
  manualPrize = false;
}

void loop() {
  dbgc("top of loop", active);

  if (!active && !gameMode && digitalRead(button) == HIGH) {
    active = true;
    digitalWrite(LED, HIGH);    // Turn on signaling LED
    delay(250);
    for (int i = 0; i < 4; i++) {   // Simulate the 4 beeps made whenever a contestant buzzes in
      tone(buzzer, 1200);
      delay(50);
      noTone(buzzer);
      delay(50);
    }
    Serial.println("type y for correct or anything else for incorrect\n");

  }
  responseChecker();  // Check whether player answers correctly or not
  boardMode();
}

void responseChecker() {
  if (Serial.available()) {
    dbgi("4:", Serial.available(), 1000);

    //answer = Serial.readStringUntil('/n');
    answer_SS = Serial.readStringUntil('/n').c_str(); // SafeString expects .c_str()
    answer_SS.trim();
    if (answer_SS.length() > 0) {
      dbg("1:", answer_SS);
    }
    dbgc("2:", active);
    if (active) {
      if (answer_SS.equals("y")) {
        Serial.println("Correct! You win 3 spins!\n");
        for (int i = 0; i < 8; i++) {
          digitalWrite(LED, LOW);
          delay(250);
          digitalWrite(LED, HIGH);
          delay(250);
        }
        Spins++;
        Spins++;
        Spins++;
        dbgc("3:", Spins);
        tone(buzzer, 1050);
        delay(200);
        noTone(buzzer);
        Serial.println("Spins:");
        Serial.println(Spins);
        digitalWrite(LED, LOW);
        active = false;
      } else {
        Serial.println("Incorrect answer. No spins awarded\n");
        Serial.println("Spins:");
        Serial.println(Spins);
        digitalWrite(LED, LOW);
        active = false;
      }
    }
    if (answer_SS.equals("t")) {
      Serial.println("Turn activated");
      turnActive = true;
      digitalWrite(LED, HIGH);
    }
    if (turnActive) {
      if (answer_SS.equals("1") && turnActive) {
        Serial.println("Correct! You win 1 spin!\n");
        for (int i = 0; i < 8; i++) {
          digitalWrite(LED, LOW);
          delay(250);
          digitalWrite(LED, HIGH);
          delay(250);
        }
        Spins++;
        dbgc("5:", Spins);
        tone(buzzer, 1050);
        delay(200);
        noTone(buzzer);
        Serial.println("Spins:");
        Serial.println(Spins);
        digitalWrite(LED, LOW);
        turnActive = false;
      } 
      else if (answer_SS.equals("0") && turnActive) {
        Serial.println("Incorrect answer. No spins awarded\n");
        Serial.println("Spins:");
        Serial.println(Spins);
        digitalWrite(LED, LOW);
        turnActive = false;
      } // end of else if (answer_SS.equals("0") && turnActive) {
    } // end of if (turnActive) {
  } // end of if (Serial.available()) {
}

void boardMode() {
  dbgc("G",gameMode);
  if (answer_SS.equals("b") && !loopStopper) {
    Serial.println("big board mode");
    gameMode = true;
    loopStopper = true;
  } 
  else if (answer_SS.equals("q") && loopStopper) {
    Serial.println("trivia mode");
    gameMode = false;
    loopStopper = false;
  }
  
  if (gameMode) {
    if (!buttonPressed && digitalRead(button) == HIGH) {
      buttonPressed = true;
      Serial.println("Enter prize <No pun intended!>\n");
    }
    if (buttonPressed) {
      if (answer_SS.equals("whammy")) {
        Serial.println("Oh no! A whammy!");
        delay(500);
        score = 0;
        Spins--;
        Serial.println("Spins:");
        Serial.println(Spins);
        Serial.println();
        Serial.println("Score:");
        Serial.println(score);
        Serial.println();
        buttonPressed = false;
        answer_SS = "";
      } 
      else if (answer_SS.equals("prize")) {
        PrizeValue = random(200, 1500);
        score += PrizeValue;
        Serial.println("You win a prize! a value of: ");
        Serial.println(PrizeValue);
        Serial.println();
        Spins--;
        Serial.println("Spins:");
        Serial.println(Spins);
        Serial.println();
        Serial.println("Score:");
        Serial.println(score);
        Serial.println();
        buttonPressed = false;
        answer_SS = "";
      } 
      else if (answer_SS.equals("bigprize")) {
        PrizeValue = random(500, 5000);
        score += PrizeValue;
        Serial.println("You win a prize! a value of: ");
        Serial.println(PrizeValue);
        Serial.println();
        Spins--;
        Serial.println("Spins:");
        Serial.println(Spins);
        Serial.println();
        Serial.println(score);
        Serial.println("Score:");
        Serial.println();
        buttonPressed = false;
        answer_SS = "";
      } 
      else if (answer_SS.equals("spin") && !manualPrizeAndSpin) {
        manualPrizeAndSpin = true;
        Serial.println("You selected cash and spin");
      }  else if (answer_SS.equals("cash") && !manualPrize) {
        manualPrize = true;
        Serial.println("You selected cash");
      }
      if (manualPrize) {
        if (Serial.available() > 0) {
          instantCash = Serial.parseInt();
          dbgc("6:",instantCash);
          score += instantCash;
          Spins--;
          Serial.println("Spins:");
          Serial.println(Spins);
          Serial.println();
          Serial.println("Score:");
          Serial.println(score);
          Serial.println();
          manualPrize = false;
          buttonPressed = false;
          answer_SS = "";
        }
      } 
      if (manualPrizeAndSpin) {
        if (Serial.available() > 0) {
          dbgi("7:",Serial.available(),1000);
          instantCash = Serial.parseInt();
          dbgc("8:",instantCash);
          score += instantCash;
          Serial.println("Spins:");
          Serial.println(Spins);
          Serial.println();
          Serial.println("Score:");
          Serial.println(score);
          Serial.println();
          manualPrizeAndSpin = false;
          buttonPressed = false;
          answer_SS = "";
        } // end of if (Serial.available() > 0)
      } // end of if (manualPrizeAndSpin) {
    } // end of if (buttonPressed) {
  } // end of if (gameMode) {
}

best regards Stefan

Did you mean '\n' perhaps?

a7

I've got it now! I just flipped the slash before the "n" wherever the "readStringUntil" command is.

1 Like

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