Keypad + Motor DC

Hello

How can I move a Motor DC with a code in the Keypad?
This is my Code,

#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//---[Motores DC]---
int MX1 = 24;
int MX2 = 25;
//PWM 0

int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5


int read_LCD_buttons()  
  { adc_key_in = analogRead(0);      // Leemos A0
    if (adc_key_in > 900) return btnNONE;     
    if (adc_key_in < 50)   return btnRIGHT; 
    if (adc_key_in < 250)  return btnUP;
    if (adc_key_in < 450)  return btnDOWN;
    if (adc_key_in < 650)  return btnLEFT;
    if (adc_key_in < 850)  return btnSELECT; 

    return btnNONE;  
  }
  
char codigo[3];  
int espacio = 0;
int cuadro = 0;
int fila = 0;
const byte Filas = 4;          //KeyPad de 4 filas
const byte Cols = 4;           //y 4 columnas
byte Pins_Filas[] = {53, 51, 49, 47};     //Pines Arduino para las filas.
byte Pins_Cols[] = {45, 43, 41, 39};     // Pines Arduino para las columnas.

char Teclas [ Filas ][ Cols ] =
    {
        {'1','2','3','A'},
        {'4','5','6','B'},
        {'7','8','9','C'},
        {'*','0','#','D'}
     };

Keypad Teclado = Keypad(makeKeymap(Teclas), Pins_Filas, Pins_Cols, Filas, Cols);
int numero = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Codigo libro:");
  pinMode(MX1,OUTPUT);
  pinMode(MX2,OUTPUT);
}


void loop() {
  lcd_key = read_LCD_buttons();
     
  char tecla = Teclado.getKey();

   if (tecla != 0) 
   {             
     lcd.setCursor(espacio,1);
     lcd.print(tecla);
     espacio++;
   }

   codigo[numero]=tecla;  
    
   if( lcd_key == btnUP)
  {
    lcd.setCursor(4,1);
    lcd.print("Aceptado");
  }
  if (espacio == 3)
  {
    espacio = 0; 
  }
  if( lcd_key == btnSELECT)
  {
    lcd.setCursor(0,1);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Codigo libro:");
    espacio=0;
  }

 if(codigo[numero]==120){ // Coordenada X=1, Y=1 
   analogWrite(0,255);
   digitalWrite(MX1,LOW);
   digitalWrite(MX2,HIGH);
   delay(4000);
   analogWrite(0,0);
   digitalWrite(MX1,LOW);
   digitalWrite(MX2,LOW);
 }  
}

I try to save the information of keypad in a array but nothing happens.

Thanks!

Well you seem to be overwriting codigo[numero] over and over again because you are never incrementing numero to go to the next index. Where is numero++?

The only way codigo[numero]==120 will work is if codigo[numero] is equal to 'x'.

Once you add in numero++ to your code correctly, you can convert the contents of codigo to an actual integer using atoi(). Look at this link here: http://www.gdsw.at/languages/c/programming-bbrown/c_068.htm

Keep in mind this line, char codigo[3]; This will need to be changed to 4 instead of 3, in order to use atoi.

Another option is to do this.

  int myVal = 0; // add this at the top of your code.

  char tecla = Teclado.getKey();
  if (tecla) // same as tecla != NO_KEY
  {
    lcd.setCursor(espacio, 1);
    lcd.print(tecla);
    espacio++;

    myVal = (myVal * 10) + tecla - '0'; // this is easier to use, but will need to be cleared later.
  }

I try to do as you said but when increasing number ++ , the LCD doesnt recognize a number, just one digit and changed for other digits when I put it.
Besides not recognize the code to move the motor

Tell me what this does.

#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//---[Motores DC]---
int MX1 = 24;
int MX2 = 25;
//PWM 0

int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5


int read_LCD_buttons()
{ adc_key_in = analogRead(0);      // Leemos A0
  if (adc_key_in > 900) return btnNONE;
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 250)  return btnUP;
  if (adc_key_in < 450)  return btnDOWN;
  if (adc_key_in < 650)  return btnLEFT;
  if (adc_key_in < 850)  return btnSELECT;

  return btnNONE;
}

char codigo[3];
int espacio = 0;
int cuadro = 0;
int fila = 0;
const byte Filas = 4;          //KeyPad de 4 filas
const byte Cols = 4;           //y 4 columnas
byte Pins_Filas[] = {53, 51, 49, 47};     //Pines Arduino para las filas.
byte Pins_Cols[] = {45, 43, 41, 39};     // Pines Arduino para las columnas.

char Teclas [ Filas ][ Cols ] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

Keypad Teclado = Keypad(makeKeymap(Teclas), Pins_Filas, Pins_Cols, Filas, Cols);
int numero = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Codigo libro:");
  pinMode(MX1, OUTPUT);
  pinMode(MX2, OUTPUT);
}


void loop() 
{
  static int myVal = 0;
  lcd_key = read_LCD_buttons();

  char tecla = Teclado.getKey();

  if (tecla != 0)
  {
    lcd.setCursor(espacio, 1);
    myVal = (myVal * 10) + tecla - '0';
    lcd.print(myVal);
  }

  codigo[numero] = tecla;

  if ( lcd_key == btnUP)
  {
    lcd.setCursor(4, 1);
    lcd.print("Aceptado");
  }
  
  if ( lcd_key == btnSELECT)
  {
    lcd.setCursor(0, 1);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Codigo libro:");
    espacio = 0;
  }

  if (myVal == 120) // Coordenada X=1, Y=1
  {
    //analogWrite(0, 255); // what is this for?
    digitalWrite(MX1, LOW);
    digitalWrite(MX2, HIGH);
    delay(4000);
    //analogWrite(0, 0);
    digitalWrite(MX1, LOW);
    digitalWrite(MX2, LOW);
    myVal = 0;   //Reset this back to 0 when finished.
  }
}

It works !!!

analogWrite(0, 255); // what is this for?
You can move a motor DC with PWM and set when it moves or stops with a H Bridge.

Thanks a lot !

So why is it not labeled like the rest of your motor pins?

//---[Motores DC]---
int MX1 = 24;
int MX2 = 25;
//PWM 0

I put a comment because it is a reference to know where is connected but when one uses PWM motor DC is not necessary to declare it.
In my case I use L298N H Bridge and it has "Enable" which you can use it like pwm.

I'm saying, how is anyone else supposed to know what analogWrite(0,0); does. You gave your other motor pins labels that say they go to the motors, so my question is, why isn't this and the other one labeled as MXPwm?