Hola, os comento mi problema.
Estoy intentando manejar un motor mediante un teclado de membrana, y que a la vez me muestre en una pantallita LCD un mensaje. Para manejar el motor utilizo 4 teclas, 2 para que vaya en una y otra dirección mientras los pulse, y otros dos hará que se mueva el motor hasta que detecte un final de carrera, esto lo haría una placa, la otra placa sólo mostraría un mensaje dependiendo de la tecla pulsada. Pues bien, por separado todo me funciona perfectamente, ahora, es conectar el teclado a las dos placas y empiezan a pasar cosas rarísimas. Os dejo los dos códigos, uno para cada placa.
LCD y Teclado: (no os he puesto los mensajes porque son un poco largos)
#include <LiquidCrystal.h>
#include <Key.h>
#include <Keypad.h>
const byte ROWS = 2; //three rows
const byte COLS = 2; //two columns
//define the symbols on the buttons of the keypads
char keys[ROWS][COLS] = {
{'1','2'},
{'4','5'},
};
byte rowPins[ROWS] = {7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10}; //connect to the column pinouts of the keypad
char key; // will store the button we press
int i;
int relayV = 6;
int relayL = 13;
boolean luz = false;
boolean fan = false;
//initialize an instance of class NewKeypad
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// initialize LCD with the pins we'll use
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2); // Configuramos el numero de columnas y filas del LCD.
// Set the amount of milliseconds the user will have to hold a button until the HOLD state is triggered.
myKeypad.setHoldTime(500); // Default is 1000mS
}
void loop() {
key = ' ';
key = myKeypad.getKey();
if(myKeypad.getState()!=IDLE){
switch(key) {
case '1':
msg1();
break;
case '2':
msg1();
break;
case '4':
msg1();
break;
case '5':
msg1();
break;
case '7':
msg2();
break;
case '8':
msg3();
break;
} //switch
}
else {
lcd.setCursor(5,0);
lcd.print("PRUEBA");
}
}
Motor y Teclado:
#include <Key.h>
#include <Keypad.h>
const byte ROWS = 2; //three rows
const byte COLS = 2; //two columns
//define the symbols on the buttons of the keypads
char keys[ROWS][COLS] = {
{'1','2'}, //1, 2
{'4','5'}, //5, 6
};
byte rowPins[ROWS] = {10, 11}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12, 13}; //connect to the column pinouts of the keypad
char key; // will store the button we press
int motor1Pin1 = 3; // pin 2 on L293D
int motor1Pin2 = 4; // pin 7 on L293D
int enablePin = 2; // pin 1 on L293D
int finCarrera1 = 5; // fin de Carrera
int finCarrera2 = 6; // fin de Carrera
//initialize an instance of class NewKeypad
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// set all the other pins you're using as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(finCarrera1, INPUT);
pinMode(finCarrera2, INPUT);
// set enablePin low so the motor is off:
digitalWrite(enablePin, LOW);
// Set the amount of milliseconds the user will have to hold a button until the HOLD state is triggered.
myKeypad.setHoldTime(500); // Default is 1000mS
}
void loop() {
key = ' ';
key = myKeypad.getKey();
if(myKeypad.getState()!=IDLE){
switch(key) {
case '1': // motor turning in one direction while pressed
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(enablePin, HIGH);
break;
case '2': // motor turning in the other direction while pressed
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(enablePin, HIGH);
break;
case '4':
while (digitalRead(finCarrera1) == HIGH) { // motor turning in one direction to the end
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(enablePin, HIGH);
}
break;
case '5':
while (digitalRead(finCarrera2) == HIGH) { // motor turning in the other direction to the end
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(enablePin, HIGH);
}
break;
} //switch
}
else {
digitalWrite(enablePin, LOW);
}
}
Además, si pongo los dos else's (el que apaga el motor, y el que me muestra el mensaje en la pantalla), no hace las dos cosas, o me pone el mensaje, o me apaga el motor. Es la primera vez que uso dos arduinos y la verdad estoy un poco perdido, alguien me pueda iluminar un poco?
Gracias de antemano,