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();
}