Change potentiometer by matrix keyboard

Good morning, I tell you my project, I hope you can help me is life or death haha, the truth has never been very good in this arduino world but it is definitely something that I would like to learn well, well I am doing a rpm counter project, Which must have a pid driver, the master is going to enter a set-point of the revolutions to which the project must be established using a matrix keyboard, I found on the internet a program in arduino but modifying that parameter with a potentiometer, That I want to change is that analog input of the potentiometer by a digital matrix keyboard, here I leave the diagram and the program I hope they can help me :o

//Read senser ee-sx47 Photomicrosensor

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
float v=0.0;
float vf=0.0;
float u=0.0;
float ui=0.0;
float e1=0.0;
float e1_dotf=0.0;
float e1_old=0.0;

float kp=0.105;//0.101-0.515
float ki=0.152;//0.102-0.852
float kd=0.021;//0.011-0.081

// reading a PWM signal using interrupts
int pin1 = 2; // arduino pin number numbers 0 (on digital pin 2)
int intrnum1 = 0; // interrupt number 0-1 for others//and 1 (on digital pin 3)
volatile unsigned long width1 = 4294967295; // width of most recent signal
volatile unsigned long start1 = 0; // start time of rising signal
//Used for timing
int i = 0;
unsigned long timer=0;
unsigned long dtime=1;

// myisr -- interrupt handler
void myisr1()
unsigned long now1 = micros();
width1= now1 - start1;
start1 = now1;

void setup(){
pinMode(9, OUTPUT);
attachInterrupt(intrnum1, myisr1, RISING);
//LOW to trigger the interrupt whenever the pin is low,
//CHANGE to trigger the interrupt whenever the pin changes value
//RISING to trigger when the pin goes from low to high,
//FALLING for when the pin goes from high to low.
timer = millis();//start timing

void loop(){

dtime = millis()-timer;
if(dtime >= 20)
timer = millis();//start timing
sensorValue = analogRead(analogInPin);
sensorValue = map(sensorValue, 0, 1023, 0, 290); //0-290 rpm

v = 60000000.0/(width12.0); //rpm
vf = vf + ((v-vf)0.251);//filter
//PID control
e1 = sensorValue - vf;
float e1_dot = (e1 - e1_old)/0.02;
e1_old = e1;
e1_dotf = e1_dotf + ((e1_dot - e1_dotf)0.251);//filter
ui = ui + (e1
ui = constrain(ui, -60, 60);
u = (kp
e1) + ui + (e1_dotf*kd);

if(sensorValue == 0)
width1 = 4294967295;
u = 0;
u = constrain(u, 0, 255);
analogWrite(9, u);

if(i > 10)
Serial.print(e1); Serial.print("\t");

Find a sketch that reads a number from a keypad. Put the part that checks for input at the top of loop(). When input is complete, save the value in 'sensorValue' (which should have been called 'Setpoint'). Remove these lines:

   sensorValue = analogRead(analogInPin); 
   sensorValue = map(sensorValue, 0, 1023, 0, 290);  //0-290 rpm