Difficulty finding the right code

Hi, this is my first post and I hope somebody here would be able to help me.

For this project I am using an arduino uno, a 4x4 keyboard, and an LCD screen.

So I am trying to make a project where the user can enter a table number on the keyboard, and it apears on the LCD screen. (see the attachment)
But I'm still struggling.
I want it so that the number for the table above can only be entered or deleted after the user presses 'A'.
And only after they press 'B', they can enter or delete the number below. But I can't figure it out...

I would also like to add 2 LED lights. When the user is entering the first number, the first LCD light burns.
When they are entering the second number, the second LCD light burns. They should be able to constantly chance the number they want. But I think I don't have enough pins.. unless if I use another LCD screen maby, but this is the only one that I can use in Tinckercad.com.

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

byte ArrowDown[8] = {
  B00100,
  B00100,
  B00100,
  B00100,
  B11111,
  B01110,
  B00100,
};

byte ArrowUp[8] = {
  B00100,
  B01110,
  B11111,
  B00100,
  B00100,
  B00100,
  B00100,
};

long input = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

void setup()
{
lcd.createChar(0, ArrowUp);
lcd.createChar(1, ArrowDown);
  
lcd.begin(16, 2);
for(int i=0;i<=3;i++);
lcd.setCursor(0,0);
  lcd.print("TABLE : ");
  lcd.setCursor(5,0);
  lcd.write(byte(0));
  lcd.setCursor(0,1);
  lcd.print("TABLE : ");
  lcd.setCursor(5,1);
  lcd.write(byte(1));
lcd.setCursor(0, 0);
}

void loop()
{
  customKey = customKeypad.getKey();

  switch(customKey) 
  {
   case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
   lcd.setCursor(8,0);
   input = input * 10 + (customKey - '0');
   lcd.print(input);
   break;
    
    case '*':
     input = 0;
     lcd.clear();
     lcd.setCursor(0,0);
   lcd.print("TABLE : ");
   lcd.setCursor(5,0);
   lcd.write(byte(0));
   lcd.setCursor(0,1);
   lcd.print("TABLE : ");
   lcd.setCursor(5,1);
 lcd.write(byte(1));
     break;
  }
}

circuit.PNG
circuit2.PNG

circuit.PNG

circuit2.PNG

More members will see your code if you post it according to the forum guidelines.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Posting images so we do not have to download them:
How to post an image.
Another page on posting images.

Okay thank you!
I have modified my post now.

Could someone please help me with my code?

pabloheugebaert:
Okay thank you!
I have modified my post now.

Could someone please help me with my code?

Patience. This is an international forum, many of the members are sleeping now. :slight_smile:

aarg:
Patience. This is an international forum, many of the members are sleeping now. :slight_smile:

Oh yes, sometimes I get carried away when I'm working on a project. :smiley:
I'll be patient.

Hi,
Have you built the circuit?

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom... :slight_smile:

But I'm still struggling.

With what?

You need to explain what you expect the program to do, and what it does instead.

Work through the mistakes one by one.

Do not use D0 or D1 as these are used with Serial communications.


You can use A0-5 as digital inputs too.

A0 = D14 . . . A5 = D19

If you want key 'A' to do one thing and key 'B' to do another you could start by adding some more cases to your 'switch/case' statement:

    case 'A':  // Do one thing
      break;

    case 'B':  // Do another thing
      break;

TomGeorge:
Hi,
Have you built the circuit?

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom... :slight_smile:

I have not built the circuit yet, only in the simultator "Tinkercad". And I'm just a beginner when it comes to programming arduino.

jremington:
With what?

You need to explain what you expect the program to do, and what it does instead.

Work through the mistakes one by one.

So I want the program to set the lcd cursor on (8,0) after 'A' is pressed, and on (8,1) after 'B' is pressed. My program didn't change after I pressed 'A' or 'B', the lcd cursor stayed on (8,0).
I already changed my program but now it doesn't do anything anymore...

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

byte ArrowDown[8] = {
  B00100,
  B00100,
  B00100,
  B00100,
  B11111,
  B01110,
  B00100,
};

byte ArrowUp[8] = {
  B00100,
  B01110,
  B11111,
  B00100,
  B00100,
  B00100,
  B00100,
};

long input = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

void setup()
{
lcd.createChar(0, ArrowUp);
lcd.createChar(1, ArrowDown);
  
lcd.begin(16, 2);
for(int i=0;i<=3;i++);
lcd.setCursor(0,0);
  lcd.print("TABLE : ");
  lcd.setCursor(5,0);
  lcd.write(byte(0));
  lcd.setCursor(0,1);
  lcd.print("TABLE : ");
  lcd.setCursor(5,1);
  lcd.write(byte(1));
lcd.setCursor(0, 0);
}

void loop()
{
  customKey = customKeypad.getKey();

  switch(customKey) 
  {
    case 'A':
     lcd.setCursor(8,0);
     switch(customKey)
            {
             case '0' ... '9':
   lcd.setCursor(8,0);
   input = input * 10 + (customKey - '0');
   lcd.print(input);
   break;
    
   case '*':
     input = 0;
     lcd.clear();
     lcd.setCursor(0,0);
   lcd.print("TABLE : ");
   lcd.setCursor(5,0);
   lcd.write(byte(0));
   lcd.setCursor(0,1);
   lcd.print("TABLE : ");
   lcd.setCursor(5,1);
 lcd.write(byte(1));
     break; 
            }
          
   case 'B':
     lcd.setCursor(8,1);
       switch(customKey)
            {
             case '0' ... '9':
   lcd.setCursor(8,1);
   input = input * 10 + (customKey - '0');
   lcd.print(input);
   break;
    
   case '*':
     input = 0;
     lcd.clear();
     lcd.setCursor(0,0);
   lcd.print("TABLE : ");
   lcd.setCursor(5,0);
   lcd.write(byte(0));
   lcd.setCursor(0,1);
   lcd.print("TABLE : ");
   lcd.setCursor(5,1);
 lcd.write(byte(1));
     break; 
            }
  }
}

I also made a sketch to let you see what I want to achieve.


(the arrow in CASE 'B' should be pointing down.. My mistake)

  customKey = customKeypad.getKey();

  switch(customKey)
  {
    case 'A':
     lcd.setCursor(8,0);
     switch(customKey)
            {
             case '0' ... '9':

DO NOT PUT A NEW SWITCH STATEMENT AROUND YOUR EXISTING SWITCH STATEMENT. When the 'customKey' is found to be 'A' in the outer switch statement it is NOT going to magically change to something else before the inner switch statement. The way you wrote it, puttting the 'A','B' cases in an outer switch, NEITHER of the switch statements where you handle '0'...'9' or '*' will ever be executed unless 'customKey' is 'A' or 'B'.

Just try "adding some more cases to your 'switch/case' statement:" like I said before.

Okay, I'm pretty new to arduino like I said. That's why I didn't fully understand the first time.
But I do understand now. So I changed my code with more cases, and now it works fine now!
Thanks!

void loop()
{
  customKey = customKeypad.getKey();

  switch (customKey)
  {
    case 'A':
      lcd.setCursor(8, 0);
      break;

    case 'B':
      lcd.setCursor(8, 1);
      break;

    case '0' ... '9':
      input = input * 10 + (customKey - '0');
      lcd.print(input);
      break;

    case '*':
      input = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("TABLE : ");
      lcd.setCursor(5, 0);
      lcd.write(byte(0));
      lcd.setCursor(0, 1);
      lcd.print("TABLE : ");
      lcd.setCursor(5, 1);
      lcd.write(byte(1));
      break;
  }
}

Now it's doing something weird with the input numbers. for example when i click '1', '2', '3' the LCD screen will display '112123'. So I'll have to figure that out still.

Always position the LCD cursor to the character location prior to printing to the screen.

    case '0' ... '9':
      input = input * 10 + (customKey - '0');
      lcd.print(input);
      break;

Exactly what you told it to display. You probably meant:

    case '0' ... '9':
      input = input * 10 + (customKey - '0');
      lcd.print(customKey);
      break;