Issue getting two numbers to display on LCD to then recall on

Hi fellow arduino lovers.

Im using an Arduino Mega for a project where I need to display a welcome message on an LCD, then when a number (1-32) gets entered it is displayed upon the lcd with a new message (you have selected machine number '0' press s to continue).

my initial code was the following:

//libraries//
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//ints that will remain constant//
const int delayTime= 4000; //sets delay time for 4 seconds
const int delayTime2 = 30000; //sets delay time for 30 seconds
const int delayTime3 = 80; //sets delay time for 80 milli seconds
const int buzzer = 50;

int red = 52;
int yellow = 53;
int green = 51;

//Pull up code to check on previous transactions//
char lasttrans = "60"; //code to initiate last transactions
int currentPosition = 0;
char previous = "S";  //goes to previous transaction
char next = "C";     // goes to next transaction

byte value=0;

//Timer to check on inactivity and perform reset//
unsigned long previousMillis = 0;        // will store last time lcd
const long interval = 60000;           // interval at which to blink (milliseconds)
const long timer = 4000;


//Strings for all lcd text//
String welcome="Welcome to";
String shop="Shop Name";
String please="Please Select";
String machine="Machine Number";
String select="You have selected";
String option="Press S to continue";


//keypad set up for a 4x3 keypad
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'S','0','C'}
};
byte rowPins[ROWS] = {8,7,2,3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,5,4}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );




//start up of running code//


void setup()
 {
  lcd.init(); // initialize the LCD
  lcd.backlight(); // turns on LCD backlight
  Serial.begin(9600); //initialize the serial monitor
  

  pinMode (green, OUTPUT);
  pinMode (yellow, OUTPUT);
  pinMode (red, OUTPUT);
  pinMode(buzzer, OUTPUT);
  displayWelcomeScreen();                 
                  
 }


void loop()
{   
   char key = keypad.getKey(); //gets key press from keypad
   int value = 0;
   
   
    if (key != NO_KEY && key != 'S') //if key pressed then
    {   
     
     if (key >= '0' && key <= '9') //if key pressed is between 0 and 9 then maths equation to determin value, 
      {
       
       value = value *10;
       value = value + key - '0';
      }
   
   
     Serial.println(value); 
     lcd.clear();
     digitalWrite (red, LOW);
     lcd.setCursor(1,0);   //prints out the machine selected for confirmation (allows for changes)
     lcd.print(select);
     lcd.setCursor(3,1);
     lcd.print(machine);
     lcd.setCursor(9,2);
     lcd.print (value);
     lcd.setCursor(0,3);
     lcd.print (option);
     digitalWrite (yellow, HIGH);
    }
 
   
  if(key == 'S')// if S (yes) it will confirm selection and ask for money at vend price
    { 
     lcd.clear();
     digitalWrite (yellow, LOW);
     digitalWrite (green, HIGH);
     lcd.setCursor(0,0);
     lcd.print ("********************");
     lcd.setCursor(0,1);
     lcd.print ("***  Please pay  ***");
     lcd.setCursor(0,2);
     lcd.print ("***  Vend price  ***");
     lcd.setCursor(0,3);
     lcd.print ("********************");

    }
    

    if (key == 'C')//if C (no) it will cancel and go back to beginning. 
    {  
      lcd.clear();
      displayWelcomeScreen();
      tone(buzzer, 1000); // Send 1KHz sound signal...
      delay(1000);        // ...for 1 sec
      noTone(buzzer);     // Stop sound...
      digitalWrite (green, LOW);
      digitalWrite (yellow, LOW);
      digitalWrite (red, HIGH);
      
      
    }

    
      unsigned long currentMillis = millis();  // if no action after 60 seconds it will reset to beginning
   if (currentMillis - previousMillis >= interval) 
   {    
    previousMillis = currentMillis;

    if (int(key)==NO_KEY)
      {
      lcd.clear();
      displayWelcomeScreen();
      }
    }
  }

       
                                                    



  void displayWelcomeScreen ()
  {
    lcd.clear();
    digitalWrite (green, LOW);
    digitalWrite (yellow, LOW);
    digitalWrite (red, HIGH);
    lcd.setCursor(4,0);
    lcd.print(welcome);
    lcd.setCursor(4,1);         
    lcd.print(shop);               
    lcd.setCursor(3,2);
    lcd.print(please);         
    lcd.setCursor(3,3);         
    lcd.print(machine);
                                     
  }

Currently Whenever I press a number, it will display on the screen correctly, however it will only display 0-9.

I can't seem to get it to display double digits such as 10-32 etc.

if i type 1 (1 appears) followed by a 0 (the 1 gets replaced by 0)

my purpose for this is I want to assign a monetary value in GBP to each number and am unable to do so unless I can get two digits working.

The other issue i have is that I want to store the selected number(s) when i press 'S' on the keypad to confirm the selection. This would then allow me to jump to the code where I can assign the monetary value for that machine, however I am having difficulty coding that to work.

I have tried an alternate code using while/switch statements which also runs into the exact same issues as the previous code :

void loop()
  { 
    v1 = GetNumber();
    v1.toInt();
    Serial.println(v1);
    v2 = GetNumber();
    v2.toInt();
    Serial.println(v2);
  }
    int  GetNumber()
   {
      int num = 0;
      char key = keypad.getKey();
   
      while(key != 'S')
      {
         switch (key)
          {
            case NO_KEY:
            break;

            case '0': case '1': case '2': case '3': case '4':
            case '5': case '6': case '7': case '8': case '9':
            lcd.clear();
            digitalWrite (red, LOW);
            lcd.setCursor(1,0);   //prints out the machine selected for confirmation (allows for changes)
            lcd.print(select);
            lcd.setCursor(3,1);
            lcd.print(machine);
            lcd.setCursor(9,2);
            lcd.print (key);
            num = num * 10 + (key - '0');
            lcd.setCursor(0,3);
            lcd.print (option);
            digitalWrite (yellow, HIGH);
          
            break;

            case 'C':
            num = "";
            lcd.clear();
            displayWelcomeScreen ();
            break;
          }
        key = keypad.getKey();     
      }
   return num;
   
   }    

void displayWelcomeScreen ()
  {
    lcd.clear();
    digitalWrite (green, LOW);
    digitalWrite (yellow, LOW);
    digitalWrite (red, HIGH);
    lcd.setCursor(4,0);
    lcd.print(welcome);
    lcd.setCursor(4,1);         
    lcd.print(shop);               
    lcd.setCursor(3,2);
    lcd.print(please);         
    lcd.setCursor(3,3);         
    lcd.print(machine);
                                     
  }

i have searched multiple forum posts, and tried everything i can think of, but I am rather new with this type of stuff. Anyone who could point me in the right direction or knows what i am missing would be helping me out loads.

I appreciate you taking the time to read this.
Thankyou

char lasttrans = "60"; You can't put a string into a single char.
Didn't the compiler complain?

It didn't complain, but then again I haven't tried to use that in any part of the code, it would have been for future use, but I appreciate the advice on that.

squaidskun:
I have tried an alternate code using while/switch statements which also runs into the exact same issues as the previous code :

void loop()

{
   v1 = GetNumber();
   v1.toInt();
   Serial.println(v1);
   v2 = GetNumber();
   v2.toInt();
   Serial.println(v2);
 }

That code is converting your Strings to integers and then discarding the result.

String s = "111";
int v = s.toInt();
...

Correct me if im wrong, im just trying to understand this code you posted and what it would be used for?

String s = "111";
int v = s.toInt();

if im understanding correctly i need a separate string, this will read multiple keypad inputs? then it will take that number, covert it into an int, which then i can use to print to lcd? So i would do something like.

String s = String ('0');
int v = s.toInt();
serial.println (int v);

Your GetNumber()

   int  GetNumber()
   {

is returning an int so no need to convert it to an int as it is already one.

squaidskun:
Correct me if im wrong, im just trying to understand this code you posted and what it would be used for?

String s = "111";

int v = s.toInt();

I was just showing you how to properly use the toInt() method in the String class. Your code didn't assign the return value is it was pointless.

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