Arduino Keypad

Hi, I'm building a project and it uses the Keypad for inputs, I'm having a problem with clearing the previous entry before going to the next process

I need to know how to clear the entry,

For example,

If I enter 15 in the first process i reads 15 but after that if I enter 15 again for the second process it reads as 1515.

Guys please help me to solve this. thank you in advance.

Please post your full sketch. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it’s ok to add it as an attachment.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

When your code requires a library that’s not included with the Arduino IDE please always post a link(using the chain links icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

Please, post your keyboard layout and its connection with the Arduino.

int solenoidPin1 = 23;
int solenoidPin2 = 25;
int solenoidPin3 = 27;
int a = 0;
int b = 0;
int c = 0;
char customKey;
int x = 0;

String weight = "";
int integerweight = 0;
int weightkey;
//char KeypadX;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'W'},
{'4', '5', '6', 'F2'},
{'7', '8', '9', 'F3'},
{'START', '0', 'STOP', 'F4'}
};
byte rowPins[ROWS] = {36, 34, 32, 30};
byte colPins[COLS] = {28, 26, 24, 22};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
pinMode(solenoidPin1, OUTPUT);
pinMode(solenoidPin2, OUTPUT);
pinMode(solenoidPin3, OUTPUT);

analogWrite(6, 80);

lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);

}
// put your setup code here, to run once:

void loop() {
char key = keypad.getKey();

if (key != NO_KEY) {
Serial.println(key);
}
char customKey = keypad.getKey();

if (key == '1') {
lcd.clear();
lcd.print("Process 1");
delay(3000);
lcd.clear();
lcd.print("Enter Weight:");
while (1) {
char weightkey = keypad.getKey();
if (weightkey == 'W') { //press this to confirm weight
integerweight = weight.toInt();

break;
} else if (weightkey == '1' || weightkey == '2' || weightkey == '3' || weightkey == '4' || weightkey == '5' || weightkey == '6' || weightkey == '7' || weightkey == '8' || weightkey == '9' || weightkey == '0') {
weight += (char)weightkey; //else if(weightkey != other non numeric keys. less comparisons
//
lcd.print(weightkey);

}

delay (500); //leave some time to debounce
}

delay(1000);
//b = 1;
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Press START");
delay(1000);
lcd.clear();
a = 1;
delay(100);

Serial.println(integerweight);

}

if (key == '2') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Process 2");
delay(3000);
lcd.clear();
lcd.print("Enter Weight:");
while (1) {
char weightkey = keypad.getKey();
if (weightkey == 'W') { //press this to confirm weight
integerweight = weight.toInt();
break;
} else if (weightkey == '1' || weightkey == '2' || weightkey == '3' || weightkey == '4' || weightkey == '5' || weightkey == '6' || weightkey == '7' || weightkey == '8' || weightkey == '9' || weightkey == '0') {
weight += (char)weightkey; //else if(weightkey != other non numeric keys. less comparisons
lcd.print(weightkey);
}

delay (500); //leave some time to debounce
}

int weightint = weight.toInt();
if(weightint == 15){
Serial.println("15");
}

//lcd.print(key);

delay(5000);

lcd.clear();
lcd.print("Press START");
delay(1000);
lcd.clear();

a = 2;
}

This is the code I used

I need to know how to clear the entry,

You need to set weight back to zero just before you start to read the new entry, perhaps where you prompt the user to enter the weight

Also, on the first pass through loop( ) weight has not been set to anything meaningful.

int solenoidPin1 = 23;
int solenoidPin2 = 25;
int solenoidPin3 = 27;
int a = 0;
int b = 0;
int c = 0;
char customKey;
int x = 0;

String weight = "";
int integerweight = 0;
int weightkey;

//char KeypadX;
LiquidCrystal lcd( 12, 11, 5, 4, 3, 2 );
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'W'},
  {'4', '5', '6', 'F2'},
  {'7', '8', '9', 'F3'},
  {'START', '0', 'STOP', 'F4'}
};

byte rowPins[ROWS] = {36, 34, 32, 30};
byte colPins[COLS] = {28, 26, 24, 22};
Keypad keypad = Keypad( makeKeymap( keys ), rowPins, colPins, ROWS, COLS  );

void setup( ) {
    pinMode( solenoidPin1, OUTPUT );
    pinMode( solenoidPin2, OUTPUT );
    pinMode( solenoidPin3, OUTPUT );

    analogWrite( 6, 80 );

    lcd.begin( 16, 2 );
    lcd.clear( );
    Serial.begin( 9600 );
}


void loop( ) {
    char key = keypad.getKey( );

    if( key != NO_KEY ) {
        Serial.println( key );
    }
    char customKey = keypad.getKey( );
   
    if( key == '1' ) {
        lcd.clear( );
        lcd.print( "Process 1" );
        delay( 3000 );
        lcd.clear( );
        lcd.print( "Enter Weight:" );

        while( 1 ) {
            char weightkey = keypad.getKey( );
            if( weightkey == 'W' ) {                //press this to confirm weight
                integerweight = weight.toInt( );
                break;
            } 
            else if( weightkey == '1' || weightkey == '2' || weightkey == '3' || weightkey == '4' || 
                     weightkey == '5' || weightkey == '6' || weightkey == '7' || weightkey == '8' || 
                     weightkey == '9' || weightkey == '0' ) {
                        //else if( weightkey != other non numeric keys. less comparisons
                        weight +=(char)weightkey; 
                        lcd.print( weightkey );
            }
            delay( 500 );   //leave some time to debounce
        }
 
   
        delay( 1000 );
        //b = 1;
        lcd.setCursor( 0, 0 );
        lcd.clear( );
        lcd.print( "Press START" );
        delay( 1000 );
        lcd.clear( );
        a = 1;
        delay( 100 );
        Serial.println( integerweight );
    }
 
    if( key == '2' ) {
        lcd.clear( );
        lcd.setCursor( 0, 0 );
        lcd.print( "Process 2" );
        delay( 3000 );
        lcd.clear( );
        lcd.print( "Enter Weight:" );
        while( 1 ) {
            char weightkey = keypad.getKey( );
            if( weightkey == 'W' ) { //press this to confirm weight
                integerweight = weight.toInt( );
                break;
            } else if( weightkey == '1' || weightkey == '2' || weightkey == '3' || weightkey == '4' || 
                       weightkey == '5' || weightkey == '6' || weightkey == '7' || weightkey == '8' || 
                       weightkey == '9' || weightkey == '0' ) {
                            //else if( weightkey != other non numeric keys. less comparisons
                            weight +=(char)weightkey; 
                            lcd.print( weightkey );
            }
            delay( 500 ); //leave some time to debounce
        }
     
        int weightint = weight.toInt( );
        if( weightint == 15 ){
            Serial.println( "15" );
        }
   
        //lcd.print( key );
        delay( 5000 );
        lcd.clear( );
        lcd.print( "Press START" );
        delay( 1000 );
        lcd.clear( );
        a = 2;
    }
}

boolrules:
Also, on the first pass through loop( ) weight has not been set to anything meaningful.

What about the global declaration

String weight = "";

Personally I would not use a String as I prefer to build the actual number as the digits are received by multiplying the current value by 10 and adding the value of new digit, but each to his own.

Also

      else if (weightkey == '1' || weightkey == '2' || weightkey == '3' || weightkey == '4' || weightkey == '5' || weightkey == '6' || weightkey == '7' || weightkey == '8' || weightkey == '9' || weightkey == '0')

could be tidied up to

else if (weightkey >= '0' && weightkey <= '9')

The biggest change that I would make would be to eliminate the while(1) and let loop() do the looping, but these are things that can wait until the OP gets the current style of code working

char weightkey = keypad.getKey();
<snip>
        weight += (char)weightkey;

Why, exactly, is it necessary to cast a char to a char?