Improved KEY PAD


Keypad Shield Example

This example shows an application on 1Sheeld’s keypad and text
to speech shield.

By using this example, you can use the app’s keypad to enter
the password (ex: 1234#) to do an action, in this case blinking
a LED on pin 13. Moreover inserting after default code the  admin’s code
 (blinking led on pin 12 )is possible to change code itself.
All the actions are voice guided with the text to speech facility.
All further code changes require the entering of user code followed
by admin one. Previous user code or restoring of the default code are
not admitted.

we don’t hold responsibility in case something went wrong. Use
it at your own risk.


/* Include 1Sheeld library. */
int iterations = 1,error_attemp=0;
int ledPin = 13; //code recognized
int ledPin2 = 12; // new code programming mode
char code_default[6]=”1234#”,code_admin[6]=”ABCD#”;
char code_r[6]=”00000″,new_code[6]=”00000″;
bool default_code_ok=false, code_ok=false,new_code_flg=false,admin_code=false;
void setup()
  /* Start communication. */
  /* Set the LED as output. */
  pinMode(ledPin,OUTPUT); // led corresponding to code accepted
  pinMode(ledPin2,OUTPUT);// led corresponding to code updating

void loop()
{   if(Keypad.isRowPressed(3) && Keypad.isColumnPressed(0))  // key: * ,will reset the code pattern inserted
      {   code_ok=false; TextToSpeech.say(“,reset,”); iterations = 1;delay(200);}
    else if(iterations == 1 && isAnyButtonPressed()  )
      {  code_r[0]=decode_pad(); TextToSpeech.say(“first digit”);delay(200);iterations++;}
    else if(iterations == 2 && isAnyButtonPressed()  )
      {  code_r[1]=decode_pad();  TextToSpeech.say(“second digit”);delay(200);iterations++;}
    else if(iterations == 3 && isAnyButtonPressed()  )
      {  code_r[2]=decode_pad();  TextToSpeech.say(“third digit”);delay(200);iterations++;}
    else if(iterations == 4 && isAnyButtonPressed()  )
      {  code_r[3]=decode_pad();  TextToSpeech.say(“fourth digit”);delay(200);iterations++;}
    else if(iterations == 5 && isAnyButtonPressed()  )
      {  code_r[4]=decode_pad();  TextToSpeech.say(“fifth digit,”);delay(1000);iterations++;}

    if( iterations == 6 )    
     {//Terminal.print(“code inserted: “);Terminal.println(code_r);
      if ( !admin_code &&((! new_code_flg && (strcmp(code_default,code_r) == 0)) || ( new_code_flg && (strcmp(new_code,code_r) == 0))  ) )
         {if (! new_code_flg)
              {//Terminal.println(” Success, recognized Default code.”);
                TextToSpeech.say(“Default code, accepted”);}
              {//Terminal.println(” Success, recognized New code.”);
                TextToSpeech.say(“New code, accepted”);}
          iterations = 1; default_code_ok=!new_code_flg; code_ok=true;error_attemp=0;
          digitalWrite(ledPin,HIGH);delay(3000); digitalWrite(ledPin,LOW);    
      else if ( code_ok && (strcmp(code_admin,code_r) == 0) )
         { //Terminal.println(” Success, recognized Admin code.”); Terminal.println(” Insert new code.”);
           digitalWrite(ledPin2,HIGH);TextToSpeech.say(“Ready to change code”);
           iterations = 1;   admin_code=true;     code_ok=false; }      
      else if ( admin_code  )
         { if (  ((strcmp(new_code,code_r)==0) || (strcmp(code_default,code_r)==0)) )
             {TextToSpeech.say(“entered code is equal to previous”);delay(3000);strcpy (code_r,”00000″);
              admin_code=false;code_ok=false;digitalWrite(ledPin2,LOW);digitalWrite(ledPin,LOW);  }
             {strcpy (new_code,code_r);
              digitalWrite(ledPin2,LOW); //Terminal.println(” Success, New code accepted.”);
              TextToSpeech.say(“New code, enabled”);
              iterations = 1;  new_code_flg=true;admin_code=false; code_ok=false; }
         { iterations = 1;code_ok=false;error_attemp++;
           strcpy (code_r,”00000″);
           //Terminal.println(” FAIL, code not accepted.”);Terminal.print(“Error attemp counter “);Terminal.println(error_attemp);
           digitalWrite(ledPin,LOW);digitalWrite(ledPin2,LOW);TextToSpeech.say(“warning, Code failed”);}
      if  (error_attemp>2)
          {delay(2000);TextToSpeech.say(“To many errors, key pad has been locked “);delay(30000);
           TextToSpeech.say(“,,key pad is unlocked,,,”);delay(2000);

 char decode_pad()
{ char a[17]=”123A456B789C*0#D”;
   if(Keypad.isRowPressed(0) && Keypad.isColumnPressed(0)) return a[0];
   if(Keypad.isRowPressed(0) && Keypad.isColumnPressed(1)) return a[1];  
   if(Keypad.isRowPressed(0) && Keypad.isColumnPressed(2)) return a[2];
   if(Keypad.isRowPressed(0) && Keypad.isColumnPressed(3)) return a[3];    
   if(Keypad.isRowPressed(1) && Keypad.isColumnPressed(0)) return a[4];
   if(Keypad.isRowPressed(1) && Keypad.isColumnPressed(1)) return a[5];  
   if(Keypad.isRowPressed(1) && Keypad.isColumnPressed(2)) return a[6];
   if(Keypad.isRowPressed(1) && Keypad.isColumnPressed(3)) return a[7];        
   if(Keypad.isRowPressed(2) && Keypad.isColumnPressed(0)) return a[8];
   if(Keypad.isRowPressed(2) && Keypad.isColumnPressed(1)) return a[9];  
   if(Keypad.isRowPressed(2) && Keypad.isColumnPressed(2)) return a[10];
   if(Keypad.isRowPressed(2) && Keypad.isColumnPressed(3)) return a[11];    
  // if(Keypad.isRowPressed(3) && Keypad.isColumnPressed(0)) return a[12];
   if(Keypad.isRowPressed(3) && Keypad.isColumnPressed(1)) return a[13];  
   if(Keypad.isRowPressed(3) && Keypad.isColumnPressed(2)) return a[14];
   if(Keypad.isRowPressed(3) && Keypad.isColumnPressed(3)) return a[15];        

boolean isAnyButtonPressed()
{  return ((Keypad.isAnyRowPressed() && Keypad.isAnyColumnPressed()));}

Related Blogs

Arduino IoT

Arduino IoT Shield Functions Explainer

Arduino makers …. you can connect your Arduino to MQTT brokers(servers) and launch your IoT with Arduino IoT Shield! It’s the 1Sheeld IoT shield that will do the job for you. It’s one of the most important but recent Arduino shields in 1Sheeld. Simply, it turns your Arduino into MQTT client where you can publish and …

Arduino Temperature Humidity System

IoT Arduino Temperature and Humidity System

Monitor my home environmental parameters remotely is one of the most desirable things I have ever wanted to make. It’s not about purchasing such a system that can get the job done for me. But, passion to create it myself. So, today, I am going to connect the popular DHT sensor to an online MQTT …

Arduino Door Lock

IoT Arduino Door Lock using 1Sheeld and Smartphone

  “Oh God! … It seems like I have forgotten the door lock open, again” … the normal saying whenever I am in hurry to go out! Have you encountered this problem before? It always confuses me since I may even think that I have left the door lock opened, and once I go back, …

IoT Fan Control

Arduino IoT Fan Control using 1Sheeld and Smartphone

Here I am with another IoT-based home automation project; Arduino IoT Fan Control. Every day, I get back from work tired and feeling hot. No air conditioner here and the fan takes much time to start cooling the room, sadly! So, I was thinking why not controlling the fan from outside my home with my …

Arduino Light Control

IoT Arduino Light Control using 1Sheeld and Smartphone

Have you ever forgotten to turn your home lights off when you got outside? Sadly, it happens to me all the time. And this causes confusion once I get my bills! Furthermore, here is one secret about me; I am a lazy man. I always want to control my room lamp from my sweet bed. …


MQTT Protocol – How it Works

MQTT is one of the most commonly used protocols in IoT projects. It stands for Message Queuing Telemetry Transport. In addition, it is designed as a lightweight messaging protocol that uses publish/subscribe operations to exchange data between clients and the server. Furthermore, its small size, low power usage, minimized data packets and ease of implementation …