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 Robotics

Arduino Robotics Series, from a car to a robot arm

Arduino keeps standing out from the crowd of all-around development boards due too its ease of use and budget price. As a result, this has enabled almost anyone to get his own idea into life no matter how crazy or even useless to the whole world it was, except for its maker! Also, it has …

Arduino Robot Arm

How to build an Arduino Robot Arm placed on RC Car

Arduino robots are always funny and definitely more enjoyable when combined with robotic arm! And today, I will walk you through the making of a simple Arduino Robot Arm that’s made of cardboard and how you can attach it over your Arduino Bluetooth RC Car / Robot that you have made so far through this series. …

Arduino Line Following Robot

How to build an Arduino Line Following Robot using Cardboard

Arduino Line Following Robot is one of the easiest and most well-known projects that anyone can make to learn the basics of programming, electronics, and mechanics all in one project. It’s known that this project is common for most of the makers and tech students and today I am going to give it a revisit …

Arduino Obstacle Avoiding Robot

How to build an Arduino Obstacle Avoiding Robot using Cardboard

Back again with another Arduino Bluetooth RC Car tutorial but with an advanced feature that makes your car autonomous by adding ultrasonic sensors to the RC Car you have made before!. Yeah, I know that you may have seen other tutorials using ultrasonic to make the car/robot avoid obstacles ahead, but I am going to …

Arduino Bluetooth RC car

How to make an Arduino Bluetooth RC Car using Cardboard

  Cardboard crafts are one of the most popular and easy to make DIY stuff. Mixing this with the unmatchable enjoyment of RC Cars and the ease of using Arduino, I am going to show you how to make a Cardboard Arduino Bluetooth RC car that you can control via Bluetooth from your smartphone.  …

Arduino Security Camera

Arduino Security Camera System (Arduino Home Hacks #4)

Arduino Security Camera Have you ever wanted to check your home in real time? Afraid that maybe someone has stormed in, don’t remember if you shut the door or not or maybe wanna check if your Hyper-energetic dog has broken any of your dishes AGAIN!! 🙂 Then this is the perfect place for you cause today …