Passing keystrokes and text to a function

Newbie here.
I’m trying to code a function that will write anything to an i2C led screen (64x128). This could be text constants, strings, and individual key presses from a keypad.
As a test I would like to display both the word ‘test’ and the key pressed but I cannot get both to work together.
I list the code at the end.

If the last parameter is written:
TextToScreen(int x,int y,int TextSize,int TextCol, char message[])
Here is the output:

If the last parameter is written:
TextToScreen(int x,int y,int TextSize,int TextCol, char message)
Here is the output:

Neither of these is what I want, which is 'test 5' on the LED screen, i.e. a faithful rendition of the arguments that I am sending to the function. I seem to have either the keystroke or the text but not both!
Can anybody help please? Maybe I need to have two different (but almost identical) functions .. I guess that would solve my issue but seems clumsy and simply exposes my poor understanding of what’s actually happening.

```cpp
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>

// SETTING UP THE OLED DISPLAY ....
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// SETTING UP THE KEYPAD ....
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;

 
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
 
// Connections to Arduino
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
char inputCode[10];


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


void setup() {
Serial.begin(9600);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
} // end of void setup


void loop() {
  char customKey = customKeypad.getKey();
display.clearDisplay();
if (customKey){
TextToScreen(30,20,2,SSD1306_WHITE,"test");
TextToScreen(30,40,2,SSD1306_WHITE,customKey);
}
} // end of void loop


void TextToScreen(int x,int y,int TextSize,int TextCol,char message[])
{
display.setTextColor(TextCol); 
display.setTextSize(TextSize);
display.setCursor(x,y);
display.write(message);
display.display();
}


thank you for this. I am avoiding objects (memory etc.) ... happy to work with 2 functions .. just thought I should check that I wasn't being a total idiot. Great answer and very quick! thanks again.

The compiler will happily create the two functions for you using a template:

template <typename T>
void TextToScreen(int x, int y, int TextSize, int TextCol, const T message) {
  display.setTextColor(TextCol);
  display.setTextSize(TextSize);
  display.setCursor(x, y);
  display.print(message);
  display.display();
}
1 Like

Adafruit_GFX already does:

class Adafruit_GFX : public Print {

How are you doing that given that your code uses so many objects: Adafruit_SSD1306, Keypad, Serial, Wire, etc?

Thanks for the reply.
I can't avoid those library calls? ... I have read that string objects are best avoided with small boards (e.g. nano) and it's "better" for understanding !? I think I'm probably already out of my depth here!

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