//este
#include <Keypad.h>
const int motorPin1 = 12; // Pin de control 1 del motor
const int motorPin2 = 14; // Pin de control 2 del motor
const int motorInput2Pin = 35;
// Definiciones del teclado matricial
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 RowPin[ROWS] = {19, 18, 5, 17}; // Pines de fila
byte ColPin[COLS] = {16, 4, 2, 15}; // Pines de columna
Keypad keypad = Keypad(makeKeymap(keys), RowPin, ColPin, ROWS, COLS);
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key >= '0' && key <= '9') { // Si se presiona un número
int targetCount = key - '0'; // Convertir el carácter a entero
// Rotar el motor hacia adelante o hacia atrás según la dirección actual
if (targetCount > 0) {
rotateMotor(true); // Rotar hacia adelante
} else {
rotateMotor(false); // Rotar hacia atrás
}
delay(targetCount * 1000); // Esperar según el valor ingresado
stopMotor(); // Detener el motor después de esperar
} else if (key == 'A') { // Si se presiona 'A', cambiar la dirección hacia adelante
rotateMotor(true);
} else if (key == 'B') { // Si se presiona 'B', cambiar la dirección hacia atrás
rotateMotor(false);
}
}
}
void rotateMotor(bool forward) {
if (forward) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
} else {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
}
}
void stopMotor() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}