Ok excuse me, i dont have too much experience in this. Here is my code:
#include <Keypad.h>
//VARIABLES ENVIO DATOS
int led = 13;
int onModulePin = 2; // Definimos el Pin 2 para activar o desactivar el modulo 3G (sin pulsar el botón)
int timesToSend = 1; // Numero de sms a enviar
int count = 0;
char phone_number[]="*********"; // ********* numero que recibirá el sms
//VARIABLES TECLADO
const byte ROWS = 4; //4 filas
const byte COLS = 3; //3 columnas
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
byte rowPins[ROWS] = {7, 6, 5, 4}; // reserva de memoria en bytes, conectar a los pines de filas del teclado
byte colPins[COLS] = {10, 9, 8}; // reserva de memoria en bytes, conectar a los pines de columnas del teclado
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char serialnumber[10]= {'6','1','0','3','7','2','2'};
char marcamaquina[10]= {'B','T'};
char modelomaquina[10]= {'L','W','E','2','0','0'};
char usuario1[4]={'1','2','3','#'};
char usuario2[4]={'4','5','6','#'};
char usuario3[4]={'7','8','9','#'};
char usuario4[4]={'0','9','8','#'};
char usuario5[4]={'7','6','5','#'};
char attempt[4]={0,0,0,0};; // usado para comparar
int z=0;
int sal1 = 11; // salida led verde pin 11
int sal2 = 12; // salida led rojo pin 12
char attempt2[4]={0,0,0,0};
//PROGRAMA
void setup()
{
pinMode(sal1, OUTPUT);
pinMode(sal2, OUTPUT);
Serial.begin(115200); //Configura la velocidad del puerto serie
keypad.setHoldTime(150); // Tiempo de pulsado de boton. Default is 1000mS
keypad.setDebounceTime(30); // Tiempo de rebote de bootn. Default is 50mS
Serial.println(" Introduzca PIN: ");
}
void leds()
{
if (sal1 == LOW)
sal2 = HIGH;
else
{
sal2 = LOW;
}
}
void checkPIN()
{
int correct=0;
for (int q=0; q<=3; q++)
{
if (attempt[q]==usuario1[q]||attempt[q]==usuario2[q]||attempt[q]==usuario3[q]||attempt[q]==usuario4[q]||attempt[q]==usuario5[q])
{
attempt2[q]=attempt[q];
correct++;
}
}
if ((correct==3) && ((attempt2[4]==usuario1[4]) || (attempt2[4]==usuario2[4]) || (attempt2[4]==usuario3[4]) || (attempt2[4]==usuario4[4]) || (attempt2[4]==usuario5[4])))
{
Serial.println("PIN correcto");
digitalWrite(11, HIGH); // activa un contacto
for (int zz=0; zz<=4; zz++) // borrar el último código introducido
{
attempt[zz]=0;
}
switchModule();
setup2();
loop2();
}
else
{
Serial.println("PIN erroneo");
digitalWrite(12, HIGH); // activa un contacto
delay(1000);
digitalWrite(12, LOW);
for (int zz=0; zz<=4; zz++) // borrar el último código introducido
{
attempt[zz]=0;
}
setup();
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
switch(key)
{
case '*':
z=0;
digitalWrite(12, LOW);
digitalWrite(11, LOW);
setup();
break;
case '#':
delay(50);
Serial.println(" ");
checkPIN();
break;
default:
attempt[z]=key;
z++;
}
Serial.print("*");
leds();
}
}
void loop()
{
readKeypad();
}
void switchModule(){
digitalWrite(onModulePin,HIGH); //Para activar el módulo 3G, debemos enviarle un pulso de 2sg por el pin número 2 (lo mismo para apagar)
Serial.println("Activando modulo 3G...");
delay(2000);
digitalWrite(onModulePin,LOW);
Serial.println("Modulo 3G activado...");
}
void setup2(){
Serial.begin(115200); //Configuramos puerto UART
delay(2000);
pinMode(led, OUTPUT);
pinMode(onModulePin, OUTPUT);
for (int i=0;i< 5;i++){
delay(5000);
}
Serial.println("AT+CMGF=1"); //Seleccionamos el formato del sms (0=PDU 1=texto)
delay(100);
}
void loop2(){
while (count < timesToSend){
delay(1500);
Serial.print("AT+CMGS=\""); // send the SMS number
Serial.print(phone_number);
Serial.println("\"");
delay(1500);
Serial.print("Maquina conectada: ");
Serial.println (marcamaquina);
delay(1500);
Serial.print("Modelo: ");
Serial.println (modelomaquina);
delay(1500);
Serial.print("Numero de serie: ");
Serial.println (serialnumber);
delay(1500);
Serial.print("Usuario: ");
Serial.println (attempt2);
delay(500);
Serial.write(0x1A); //sends ++
Serial.write(0x0D);
Serial.write(0x0A);
delay(5000);
count++;
}
}