Integrating a OLED Display, 3x4 Keypad & Arduino Micro (Help Please)

Good morning all,

I am hoping for a bit of help.

The project I am working on has several different purposes but to give you an overview. An "Observer" is on a "Section", as a competitor goes through that "Section" he or she types the competitor number on a keypad, hits enter, then on the next page that appears they type the score the competitor gets, hits enter and the loop repeats. As I progress I am going to be looking for those scores to then be saved to a CSV file on a memory card reader but that is further down the line.

Before it gets to the part I have described above there are a few other questions, "How many sections?" being one of them. Again I would like the "Observer" to be able to type in a number on the keypad and hit enter when the next screen will appear.

I have wired up the keypad through resistors so that it goes to one analogue pin. However, I am struggling with the code to actually get a number on number on the screen.

So at the moment I am asking for help with the following bit of code. I know this isn't correct but I haven't been able to find a good tutorial online with an OLEM screen.

void setupLoop ()
{
while (digitalRead(buttonEnter) == LOW)
{
lcd.setCursor(0, 0);
lcd.print("How Many");
lcd.setCursor(0, 1);
lcd.print("Sections?");
Getkey();
{
    lcd.clear();
    lcd.home();
    lcd.print(Getkey);
    lcd.setCursor(10, 1);
    delay (2000);
  }
  delay(3000);
  lcd.clear();
}

The goal is it comes onto a screen which says "How Many Sections?" the user can type on the keyad, "12", for example, then hit enter and the program will move to the next part.

This is the entirety of the code as there are probably some mistakes in the set-up.

#include <Adafruit_CharacterOLED.h>
Adafruit_CharacterOLED lcd(OLED_V2, 6, 7, 8, 9, 10, 11, 12);
//---------------------------------------
#include <OnewireKeypad.h>
#include <Wire.h>

#define Rows 4
#define Cols 3
#define Pin A0
#define Row_Res 4700
#define Col_Res 1000

char KEYS[] = {
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9',
  '*', '0', '#'
};

OnewireKeypad <Adafruit_CharacterOLED, 12> KeyPad(lcd, KEYS, Rows, Cols, Pin, Row_Res, Col_Res );
OnewireKeypad <Print, 12> KeyPad(Serial, KEYS, Rows, Cols, Pin, Row_Res, Col_Res );
//----------------------------------------

const int buttonEnter = 4;

void setup()
{
  pinMode (buttonEnter, INPUT);
  //--------------------------------------
  lcd.begin(16, 2);
  lcd.setCursor(3, 0);
  lcd.print("Welcome to");
  lcd.setCursor(4, 1);
  lcd.print("MiScore!");
  delay(3000);
  lcd.clear();
  //--------------------------------------
  while (digitalRead(buttonEnter) == LOW)
  {
    lcd.setCursor(1, 0);
    lcd.print("Press Enter to");
    lcd.setCursor(5, 1);
    lcd.print("Begin!");
  }
  lcd.clear();
}

void loop()
{
  setupLoop ();
  mainLoop();
  subLoop ();
}

void setupLoop ()
{
while (digitalRead(buttonEnter) == LOW)
{
lcd.setCursor(0, 0);
lcd.print("How Many");
lcd.setCursor(0, 1);
lcd.print("Sections?");
Getkey();
{
    lcd.clear();
    lcd.home();
    lcd.print(Getkey);
    lcd.setCursor(10, 1);
    delay (2000);
  }
  delay(3000);
  lcd.clear();
}
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("How Many");
  lcd.setCursor(0, 1);
  lcd.print("Laps?");
  delay(3000);
  lcd.clear();
  //---------------------------------------
}

void mainLoop ()
{
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Enter Section");
  lcd.setCursor(0, 1);
  lcd.print("Number:");
  delay(3000);
  lcd.clear();
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Observers Name?");
  delay(3000);
  lcd.clear();
  //---------------------------------------
}

void subLoop ()
{
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Rider Number?");
  delay(3000);
  lcd.clear();
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Rider Score?");
  delay(3000);
  lcd.clear();
  //---------------------------------------
}

Any help or advice would be appreciated.

Many thanks.

I'd need to know exactly what it's doing right now to better help you. However I can tell you that this section of code:

Getkey();
{
    lcd.clear();
    lcd.home();
    lcd.print(Getkey);
    lcd.setCursor(10, 1);
    delay (2000);
  }
  delay(3000);
  lcd.clear();
}

Should be more like this:

char keyPress = Getkey();
if (keyPress != NO_KEY)
{
    lcd.clear();
    lcd.home();
    lcd.print(keyPress);
    lcd.setCursor(10, 1);
    delay (2000);
  }
  delay(3000);
  lcd.clear();
}

SRegan:
I'd need to know exactly what it's doing right now to better help you. However I can tell you that this section of code:

Getkey();

{
    lcd.clear();
    lcd.home();
    lcd.print(Getkey);
    lcd.setCursor(10, 1);
    delay (2000);
  }
  delay(3000);
  lcd.clear();
}

Thanks for the comment SRegan. I have tried to put this section of code in and am getting the following errors: -

ObserverSketchV1.3.ino:21:34: error: conflicting declaration 'OnewireKeypad<Print, 12u> KeyPad'
ObserverSketchV1.3.ino:20:44: error: 'KeyPad' has a previous declaration as 'OnewireKeypad<Adafruit_CharacterOLED, 12u> KeyPad'
ObserverSketchV1.3.ino: In function 'void setupLoop()':
ObserverSketchV1.3.ino:62:26: error: 'Getkey' was not declared in this scope
Error compiling.

Just to clarify the revised entire code is now: -

#include <Adafruit_CharacterOLED.h>
Adafruit_CharacterOLED lcd(OLED_V2, 6, 7, 8, 9, 10, 11, 12);
//---------------------------------------
#include <OnewireKeypad.h>
#include <Wire.h>

#define Rows 4
#define Cols 3
#define Pin A0
#define Row_Res 4700
#define Col_Res 1000

char KEYS[] = {
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9',
  '*', '0', '#'
};

OnewireKeypad <Adafruit_CharacterOLED, 12> KeyPad(lcd, KEYS, Rows, Cols, Pin, Row_Res, Col_Res );
OnewireKeypad <Print, 12> KeyPad(Serial, KEYS, Rows, Cols, Pin, Row_Res, Col_Res );
//----------------------------------------

const int buttonEnter = 4;

void setup()
{
  pinMode (buttonEnter, INPUT);
  //--------------------------------------
  lcd.begin(16, 2);
  lcd.setCursor(3, 0);
  lcd.print("Welcome to");
  lcd.setCursor(4, 1);
  lcd.print("MiScore!");
  delay(3000);
  lcd.clear();
  //--------------------------------------
  while (digitalRead(buttonEnter) == LOW)
  {
    lcd.setCursor(1, 0);
    lcd.print("Press Enter to");
    lcd.setCursor(5, 1);
    lcd.print("Begin!");
  }
  lcd.clear();
}

void loop()
{
  setupLoop ();
  mainLoop();
  subLoop ();
}

void setupLoop ()
{
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("How Many");
  lcd.setCursor(0, 1);
  lcd.print("Sections?");
  char keyPress = Getkey();
  if (keyPress != NO_KEY)
  {
    lcd.clear();
    lcd.home();
    lcd.print(keyPress);
    lcd.setCursor(10, 1);
    delay (2000);
  }
  delay(3000);
  lcd.clear();
}

void mainLoop ()
{
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Enter Section");
  lcd.setCursor(0, 1);
  lcd.print("Number:");
  delay(3000);
  lcd.clear();
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Observers Name?");
  delay(3000);
  lcd.clear();
  //---------------------------------------
}

void subLoop ()
{
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Rider Number?");
  delay(3000);
  lcd.clear();
  //---------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Rider Score?");
  delay(3000);
  lcd.clear();
  //---------------------------------------
}

Change this line:

OnewireKeypad <Print, 12> KeyPad(Serial, KEYS, Rows, Cols, Pin, Row_Res, Col_Res );

To this (commenting it out):

//OnewireKeypad <Print, 12> KeyPad(Serial, KEYS, Rows, Cols, Pin, Row_Res, Col_Res );

And change this line:

char keyPress = Getkey();

To:

char keyPress = KeyPad.Getkey();

Thanks for this!

It is now almost there. I have taken out the "bulk" of the code just to try and get this working. This is what I have changed to the area we are discussing to.....

void setupLoop ()
{
  //---------------------------------------
  while (digitalRead(buttonEnter) == LOW)
  {
    lcd.setCursor(0, 0);
    lcd.print("How Many");
    lcd.setCursor(0, 1);
    lcd.print("Sections?");
    char keyPress = KeyPad.Getkey();
    if (keyPress != NO_KEY)
    {
      lcd.home();
      lcd.setCursor(14, 1);
      lcd.print(keyPress);
    }
  }
  lcd.clear();
  delay (200);
}

And it works 90%, the only problem is while it is at the stage where it is pulling the information from the keypad the number is only visible while you are holding the keypad button down. I need to be able to type say 2 numbers and they stay there until enter is pushed.

I presume I am going to have to ready the switchstate of the keypad. I can do this with a digital pin but not sure how it would work with this setup. Are you able to help with this or point m in the right direction?

Thank you for your help so far.

Remove the "lcd.clear();" this is clearing the LCD every loop which is why the input isn't staying.

SRegan:
Remove the "lcd.clear();" this is clearing the LCD every loop which is why the input isn't staying.

I tried that but it still isn't keeping the number. It appears when you hold the keypad down but then goes once the keypad is released.