Struct Gains Gibberish Values

I’m trying to make my poll_buttons function fill in a struct which indicates that the LCD screen should have a new phrase written to it. To me this makes sense since poll_buttons functions should be responsible for poling buttons, and sending messages to other parts of the system where more specific things are done.

The problem is that the struct’s lineOne array seems to fill up with gibberish at some point. Here’s my code.

#define BUTTON_UP 0x08
#define BUTTON_DOWN 0x04
#define BUTTON_LEFT 0x10
#define BUTTON_RIGHT 0x02
#define BUTTON_SELECT 0x01

struct TextUpdateCommand {
  boolean updateText;
  char * lineOne;
};

struct MovementRequest {
  boolean requestMovement;
  uint8_t location[2];
};


MovementRequest movementRequest;  // Holds messages set by pollKeys and read by Hero#draw
TextUpdateCommand textUpdateCommand; // holds msgs set by pollKeys and read by drawText()



void setup() {
  Serial.begin(115200);
  
  textUpdateCommand.updateText = true;
  textUpdateCommand.lineOne = "Fun Game";
  
  Serial.print("Fun Game");
}

void loop() {
  pollKeys(movementRequest, textUpdateCommand);
  if (textUpdateCommand.updateText)
    Serial.print(textUpdateCommand.lineOne);
    
  drawText(textUpdateCommand);
  //graphicsPhase();
  //endPhase();
}



int drawText(struct TextUpdateCommand &t) {  
  if (t.updateText){
    //lcd.clear();
    //lcd.setCursor(0,0);
    //lcd.print(t.lineOne);
    
    Serial.print(t.lineOne);
    
    t.lineOne = "";
    t.updateText = false;
  }
}




// This function needs to 'request movement' rather than set a position
// so that another function outside of "pollKeys" can be soly responsible
// for confining movements
//
// Messages:
//   - MovementRequest to hero object
//   - MessageDisplay to drawText function
void pollKeys(struct MovementRequest &movementRequest, struct TextUpdateCommand &textUpdateCommand) {
  uint8_t buttons;                       // button read value

  // buttons = lcd.readButtons();  // read the buttons on the shield
  
  char inChar = (char)Serial.read(); 
  
  if (inChar == 'a'){
    Serial.print("A button was pressed");
    buttons = BUTTON_SELECT;
  }
  else
    buttons = 0x00;
  
  if(buttons!=0) {                     // if a button was pressed
    //boolean wasSelectButtonPressedTooRecently = (frame - lastButtonPress) <= 8;
    boolean wasSelectButtonPressedTooRecently = false;
    //doesNeedRedraw = true;
    if (buttons & BUTTON_RIGHT) {       // if up pressed, increment hours
      //hero->x +=1;
    }
    if (buttons & BUTTON_LEFT) {     // if down pressed, decrement hours
      //hero->x -=1;
    }
    if (buttons & BUTTON_UP) {       // if up pressed, increment hours
      //hero->y -=1;
    }
    if (buttons & BUTTON_DOWN) {     // if down pressed, decrement hours
      //hero->y +=1;
    }

    if ( buttons & BUTTON_SELECT && !wasSelectButtonPressedTooRecently ) { 
      // gameMap->setLevel(1);
      uint8_t x = 1; //hero->x;
      uint8_t y = 1; //hero->y;
      
      uint8_t a[2] = {x,y};
      
      // struct Trigger t = gameMap->checkTrigger(a);
      
      if (true){  //(t.disabled == true){
        
        char buffer[20];
        sprintf(buffer, "No Trigger %d, %d", x, y);
        
        textUpdateCommand.updateText = true;
        textUpdateCommand.lineOne = buffer;
        
        //Serial.print(textUpdateCommand.lineOne);
      }
      else {
        //lcd.clear();
        //lcd.setCursor(0,0);
        char buffer[20];
        sprintf(buffer, "Trigger! %d, %d", x, y);
        //lcd.print(buffer);
        Serial.print(buffer);
      }
      
      //lastButtonPress = frame;
      
    }

    //confineDisplayText();
  }
}

Specifically, when it gets into the drawText function, in prints gibberish to the LCD display. But it works fine the first time through when it prints the message created in the setup() routine.

You should not be using a pointer. Use a static array, and strcpy() the new message to the array.

Ah, thanks for the info.

In case others need example:

#define BUTTON_UP 0x08
#define BUTTON_DOWN 0x04
#define BUTTON_LEFT 0x10
#define BUTTON_RIGHT 0x02
#define BUTTON_SELECT 0x01

struct TextUpdateCommand {
  boolean updateText;
  char lineOne[20];
};

struct MovementRequest {
  boolean requestMovement;
  uint8_t location[2];
};


MovementRequest movementRequest;  // Holds messages set by pollKeys and read by Hero#draw
TextUpdateCommand textUpdateCommand; // holds msgs set by pollKeys and read by drawText()



void setup() {
  Serial.begin(115200);
  
  textUpdateCommand.updateText = true;
  strcpy(textUpdateCommand.lineOne, "Fun Game");
  
  Serial.print("Fun Game");
}

void loop() {
  pollKeys(movementRequest, textUpdateCommand);
  if (textUpdateCommand.updateText)
    Serial.print(textUpdateCommand.lineOne);
    
  drawText(textUpdateCommand);
  //graphicsPhase();
  //endPhase();
}



void drawText(struct TextUpdateCommand &t) {  
  if (t.updateText){
    //lcd.clear();
    //lcd.setCursor(0,0);
    //lcd.print(t.lineOne);
    
    Serial.print(t.lineOne);
    
    strcpy(t.lineOne, " ");
    t.updateText = false;
  }
}




// This function needs to 'request movement' rather than set a position
// so that another function outside of "pollKeys" can be soly responsible
// for confining movements
//
// Messages:
//   - MovementRequest to hero object
//   - MessageDisplay to drawText function
void pollKeys(struct MovementRequest &movementRequest, struct TextUpdateCommand &textUpdateCommand) {
  uint8_t buttons;                       // button read value

  // buttons = lcd.readButtons();  // read the buttons on the shield
  
  char inChar = (char)Serial.read(); 
  
  if (inChar == 'a'){
    Serial.print("A button was pressed");
    buttons = BUTTON_SELECT;
  }
  else
    buttons = 0x00;
  
  if(buttons!=0) {                     // if a button was pressed
    //boolean wasSelectButtonPressedTooRecently = (frame - lastButtonPress) <= 8;
    boolean wasSelectButtonPressedTooRecently = false;
    //doesNeedRedraw = true;
    if (buttons & BUTTON_RIGHT) {       // if up pressed, increment hours
      //hero->x +=1;
    }
    if (buttons & BUTTON_LEFT) {     // if down pressed, decrement hours
      //hero->x -=1;
    }
    if (buttons & BUTTON_UP) {       // if up pressed, increment hours
      //hero->y -=1;
    }
    if (buttons & BUTTON_DOWN) {     // if down pressed, decrement hours
      //hero->y +=1;
    }

    if ( buttons & BUTTON_SELECT && !wasSelectButtonPressedTooRecently ) { 
      // gameMap->setLevel(1);
      uint8_t x = 1; //hero->x;
      uint8_t y = 1; //hero->y;
      
      uint8_t a[2] = {x,y};
      
      // struct Trigger t = gameMap->checkTrigger(a);
      
      if (true){  //(t.disabled == true){
        
        char buffer[20];
        sprintf(buffer, "No Trigger %d, %d", x, y);
        
        textUpdateCommand.updateText = true;
        strcpy(textUpdateCommand.lineOne,  buffer);
        
        //Serial.print(textUpdateCommand.lineOne);
      }
      else {
        //lcd.clear();
        //lcd.setCursor(0,0);
        char buffer[20];
        sprintf(buffer, "Trigger! %d, %d", x, y);
        //lcd.print(buffer);
        Serial.print(buffer);
      }
      
      //lastButtonPress = frame;
      
    }

    //confineDisplayText();
  }
}