Lab_interaccio/2011/QAN_v2/Keypad.ino

335 lines
6.4 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
char money[5] = "";
char PIN[5]="";
//take care of some special events
void keypadEvent(byte key){
byte barra, barra_old;
boolean flag_break = 0;
unsigned long currentMillis = millis();
unsigned long previousMillis = currentMillis;
//Serial.flush(); ya no funciona en Arduino UNO
while(Serial.available()>0) Serial.read();
switch (customKeypad.getState()){
case PRESSED:
switch (key){
case 'A':
lcd.clear();
lcd.print("INIT RFID");
lcd.setCursor(0, 1);
while(currentMillis - previousMillis < 5000)
{
currentMillis = millis();
barra = ((currentMillis - previousMillis)*17)/5000;
if(barra != barra_old)
{
barra_progreso(barra);
barra_old = barra;
}
if( rfid_read() )
{
if(keypadPIN())
{
httpCall( 1, 0 );
//httpRequest( 1, 0); // operacio = INIT, money = 0
//delay(4000); // espero para salir del while
}
}
}
lcdInit();
break;
case 'B':
lcd.clear();
lcd.print("BAJA RFID");
while(currentMillis - previousMillis < 5000)
{
currentMillis = millis();
barra = ((currentMillis - previousMillis)*17)/5000;
if(barra != barra_old)
{
barra_progreso(barra);
barra_old = barra;
}
if( rfid_read() )
{
if(keypadPIN())
{
httpCall( 2, 0 );
//httpRequest( 2, 0); // operacio = INIT, money = 0
//delay(4000); // espero para salir del while
}
}
}
lcdInit();
break;
case 'C':
lcd.clear();
lcd.print("COBRO RFID");
while(currentMillis - previousMillis < 5000)
{
currentMillis = millis();
barra = ((currentMillis - previousMillis)*17)/5000;
if(barra != barra_old)
{
barra_progreso(barra);
barra_old = barra;
}
if( rfid_read() )
{
if(keypadPIN())
{
if(keypadMoney())
{
httpCall( 4, money );
//httpRequest( 4, money); // operacio = INIT, money = 0
//delay(4000); // espero para salir del while
}
}
}
}
lcdInit();
break;
case 'D':
lcd.clear();
lcd.print("DESCUENTO RFID");
lcd.setCursor(0, 1);
while(currentMillis - previousMillis < 5000)
{
currentMillis = millis();
barra = ((currentMillis - previousMillis)*17)/5000;
if(barra != barra_old)
{
barra_progreso(barra);
barra_old = barra;
}
if( rfid_read() )
{
if(keypadPIN()) // si captura de pin es correcta!
{
if(keypadMoney())
{
httpCall( 3, money );
//httpRequest( 3, money); // operacio = INIT, money = 0
//delay(4000); // espero para salir del while
}
}
}
}
lcdInit();
break;
case '*':
lcd.clear();
lcd.print("SALDO RFID");
lcd.setCursor(0, 1);
while(currentMillis - previousMillis < 5000)
{
currentMillis = millis();
barra = ((currentMillis - previousMillis)*17)/5000;
if(barra != barra_old)
{
barra_progreso(barra);
barra_old = barra;
}
if( rfid_read() )
{
if(keypadPIN())
{
httpCall( 5, 0 );
}
}
}
lcdInit();
break;
}
//break;
}
}
void barra_progreso(int progreso)
{
Serial.print(progreso);
Serial.print(" ");
if(progreso <= 16)
{
lcd.setCursor(progreso-1,1);
lcd.write(255);
}
}
boolean keypadMoney(){
byte i = 0;
boolean scape = true;
unsigned long currentMillis = millis();
unsigned long previousMillis = currentMillis;
Serial.println("------------------------------");
Serial.println("Enter MONEY");
lcd.clear();
lcd.print("Enter Quantity:");
lcd.setCursor(0,1);
while ( (currentMillis - previousMillis < 30000) && scape ) // or '#' or pulsadas 4 teclas
{
currentMillis = millis();
char customKey = customKeypad.getKey();
if (customKey != NO_KEY) {
//Serial.println(key);
switch (customKeypad.getState())
{
case PRESSED:
// añadimos numeros al array
if(customKey == '#')
{
money[i] = '\0';
scape = false;
}
else
{
money[i] = customKey;
Serial.print(money[i]);
lcd.write(money[i]);
if(i>=3)
scape = false;
else
i++;
}
break;
case RELEASED:
break;
case HOLD:
break;
}
}
}
if(!scape)
{
Serial.println();
Serial.println("------------------------------");
Serial.print("Money captured: ");
Serial.println(money);
return(true);
}
else
{
Serial.println();
Serial.println("------------------------------");
Serial.println("TIME OUT");
return(false);
}
}
boolean keypadPIN(){ // FUNCION PIN
byte i = 0;
boolean scape = true;
unsigned long currentMillis = millis();
unsigned long previousMillis = currentMillis;
Serial.println("------------------------------");
Serial.println("Enter PIN");
lcd.clear();
lcd.print("Enter PIN:");
lcd.setCursor(0,1);
while ( (currentMillis - previousMillis < 30000) && scape )
{
currentMillis = millis();
char customKey = customKeypad.getKey();
if (customKey != NO_KEY)
{
//Serial.println(customKey);
switch (customKeypad.getState())
{
case PRESSED:
// añadimos numeros al array
PIN[i] = customKey;
Serial.print(PIN[i]);
lcd.write(PIN[i]);
if(i>=3)
scape = false;
else
i++;
break;
case RELEASED:
break;
case HOLD:
break;
}
}
}
if(!scape)
{
Serial.println();
Serial.println("------------------------------");
Serial.print("PIN captured: ");
Serial.println(PIN);
return(true);
}
else
{
Serial.println();
Serial.println("------------------------------");
Serial.println("TIME OUT");
return(false);
}
}