Char variable not printing value to serial monitor

Hello,

I am trying to write a quick test program for a future project of mine. I have a keypad connected and working. Any time a button is pressed, the previous button press and the new button press should be written to the serial monitor, after which the latest press assumes the value of the new press. Please see code below. I am unsure why the Serial.println(lastPressed); is writing blank to the serial monitor. The rest outputs fine.

//keypad library
#include <Keypad.h>

//set size of keypad
const byte COLS = 4;
const byte ROWS = 4;

//define global variables
char lastPressed ='0';
char nowPressed;

//define content of numpad
char hexaKeys [ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};

//define pin connections for keypad
byte rowPins [ROWS]= {9,8,7,6};
byte colPins [COLS]= {5,4,3,2};

//define keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

//start up serial connection for output monitoring.
void setup() {
  Serial.begin(9600);
}

void loop() {
 char nowPressed = customKeypad.getKey();
  
 if (nowPressed){
  Serial.println(lastPressed);  //print previous and latest button press values, separated by "to"
  Serial.println("to");
  Serial.println(nowPressed);
 };
 lastPressed = nowPressed;      //update lastPressed to become value from nowPressed
}

Some example output:
"
to
6

to
B

to
2

to
8
"

Any advice?

Move the update into the if statement.

  if (nowPressed){
    Serial.println(lastPressed);  //print previous and latest button press values, separated by "to"
    Serial.println("    to    ");
    Serial.println(nowPressed);
    lastPressed = nowPressed;
  }

Also, you don't need the harmless semicolon after the closing brace of the if statement.

a7

1 Like

Thank you @alto777 this works perfectly! I do not understand why it is necessary to include the update in the if statement. Should it not, after the if is completed, execute the rest of the loop anyway?

I hope you mean that's the only place you have it now.

An if statement executes its body conditionally. So if you want the old value to move along and make room, so to speak, for the new value, you wanna do that only if there is, in fact, a new value.

You can see why by playing computer. Put your finger on the original code and trace it step by step. With the update outside the if statement, watch what gets returned when there is no new keypad input clobber the old value as you unconditionally execute the update…

a7

Unless a button is pressed, lastPressed will become zero (in your original code) which is a non-printable character.

Your code updated lastPressed whether a key was being pressed or not.

When no key is being pressed, getKey returns a value of NO_KEY.

NO_KEY has the following definition in the library.

const char NO_KEY = '\0';

So any time through the loop when you're weren't pressing a key, lastPressed was set to '\0', a non printable character.

When you finally pressed a key, lastPressed was set to `\0', which is a non printable character as has already been pointed out by @sterretje.

That's why you should only update lastPressed when a key had been pressed as pointed out by @alto777. Otherwise you lose the last key that was pressed the next time through the loop when no key was being pressed.

2 Likes

Make sure you're updating lastPressed with the previous button press before assigning the new value.