element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog New to arduino and programming, 4x3 analog keypad question about making a passcode
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 10 Jan 2013 11:57 AM Date Created
  • Views 3054 views
  • Likes 0 likes
  • Comments 21 comments
Related
Recommended

New to arduino and programming, 4x3 analog keypad question about making a passcode

Former Member
Former Member
10 Jan 2013

Hi to you all.I am a 47 year old man who due to an accident am disabled and bought myself an Arduino to keep myself busy. I am  new to arduino and have written some basic sketches. and have made some nice projects. I have an UNO R3 board and I have made an analog keypad using 12 tactile buttons and 7 resistors with a 1nf capacitor to help with bouncing. i did this so that all my digital pins are free to use. My idea is to make an alarm system using 2 PIR sensors, 4 LED's, a buzzer and a siren.(I may want to add more components later) I have no problem in writing the code except for the 4x3 keypad. I want to make a passcode eg 1234 but this is the part I am struggling with. The push buttons return a value when they are pressed so how would I go about that part I have written code to serial print the code to my pc and give them a number. please see the code examples below but I have looked all over the internet and asked on the Arduino forum for some help with the keypad. If anyone can help I would appreciate it.

Thank you for taking the time to read this.

void readkeyboard(){
   keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
   if (keyboardValue <25){keypressed = 0;}
   if ((keyboardValue >25) && (keyboardValue < 67)){keypressed = 1;}
   if ((keyboardValue >67) && (keyboardValue < 108)){keypressed = 2;}
   if ((keyboardValue >108) && (keyboardValue < 162)){keypressed = 3;}
   if ((keyboardValue >162) && (keyboardValue < 253)){keypressed = 4;}
   if ((keyboardValue >253) && (keyboardValue < 361)){keypressed = 5;}
   if ((keyboardValue >361) && (keyboardValue < 479)){keypressed = 6;}
   if ((keyboardValue >479) && (keyboardValue < 619)){keypressed = 7;}
   if ((keyboardValue >619) && (keyboardValue < 765)){keypressed = 8;}
   if ((keyboardValue >765) && (keyboardValue < 819)){keypressed = 9;}
   if ((keyboardValue >819) && (keyboardValue < 899)){keypressed = 0;}// this is the * key
   if ((keyboardValue >889) && (keyboardValue < 955)){keypressed = 0;}
   if (keyboardValue >965){keypressed = 0;}  // this is the # key

}

when the keys are pressed they return these values

 

1=58, 2=96, 3=145,   4=228,  5=335,  6=447, 7=598,  8=720,  9=809,  *=890,  0=941, #=970

 

I have Written to the buttons like this just to test the keypad works as it should

   if (keyboardValue >25 && keyboardValue < 67) digitalWrite (12,HIGH);
   if (keyboardValue >67 && keyboardValue < 130) digitalWrite (11,HIGH);
   if (keyboardValue >162 && keyboardValue < 253) digitalWrite (9,HIGH);
   if (keyboardValue >108 && keyboardValue < 162) digitalWrite (10,HIGH);
   if (keyboardValue >253 && keyboardValue < 361) digitalWrite (8,HIGH);

  • Sign in to reply

Top Comments

  • billabott
    billabott over 12 years ago in reply to Former Member +1
    Get some sleep and check out http://www.oomlout.com/a/products/ardx/ tomorrow.
  • Former Member
    Former Member over 12 years ago

    Hi again. I have rewritten the code to try and sort out my problems. I have stopped the keypad returning conatant zeros but i still cannot get the password to work. Hopefully one of you will be able to help me with that. I can manage the rest of it.

    Thank you.

    Jeremy

     

     


    int i, k, pwcount=0;
    char password[]="1234",entry[10];// password that is stored
    int buttonPin=0;// keypad pin
    int val;// value of key pressed
    int PIR=12;//PIR sensor power pin
    int senseValue;// value of PIR sensor pin
    int nos;// variable for number of the keypad
    int sensePin=1;//Pin PIR is connected to
    int buzzerPin=2;// buzzer connected here

     

    int led3=3;// leds for testing
    int led4=4;
    int led5=5;
    int led=13;

     

     

     


    void setup() {
    pinMode (buttonPin,INPUT);
    pinMode (sensePin,INPUT);
    pinMode(PIR,OUTPUT);
    pinMode (led,OUTPUT);
    pinMode (led3,OUTPUT);
    pinMode (led4,OUTPUT);
    pinMode (led5,OUTPUT);
    pinMode (buzzerPin,OUTPUT);
    Serial.begin(9600);

     

    }

     

    void loop() {
       for (i=0; i<=1; i++){
      reset();


      val=analogRead(buttonPin);
      val = map(val, 0, 1023, 0, 255);
    // Serial.print("Button Value   ");//used for debugging
    //Serial.println(val);


    senseValue=analogRead(sensePin);
    senseValue = map(senseValue, 0, 1023, 0, 255);
    //Serial.print("     Sensor Value   ");
    //Serial.println(senseValue);

     

    //delay(200);

     


    if((val>10&&val<17)){(nos=1);//read the keypad
    blinker();


      if( nos==1){
       entry[pwcount]='1' ;//returns the button press
       pwcount++;
       delay(250);
    Serial.print('1');//prints the key value for debugging

     
    }}

     

    if((val>20&&val<29)){(nos=2);
      blinker();
      if( nos==2){
       entry[pwcount]='2' ;
       pwcount++;
       delay(250);
    Serial.print('2');

      
    }}

     

    if((val>31&&val<38)){(nos=3);
      blinker();


      if( nos==3){
       entry[pwcount]='3' ;
       pwcount++;
       delay(250);
    Serial.print('3');


     
     
    }}

     

    if((val>50&&val<58)){(nos=4);
      blinker();


      if( nos==4){
       entry[pwcount]='4' ;
       pwcount++;
       delay(250);
    Serial.print('4');


     
     
    }}

     

    if((val>75&&val<88)){(nos=5);
       blinker();


      if( nos==5){
       entry[pwcount]='5' ;
       pwcount++;
       delay(250);
    Serial.print('5');

     
     
     
    }}
    if((val>101&&val<115)){(nos=6);
      blinker();


      if( nos==6){
       entry[pwcount]='6' ;
       pwcount++;
       delay(250);
    Serial.print('6');


     
     
    }}
    if((val>138&&val<150)){(nos=7);
      blinker();


      if( nos==7){
       entry[pwcount]='7' ;
       pwcount++;
       delay(250);
    Serial.print('7');

     
     
     
    }}
    if((val>170&&val<180)){(nos=8);
      blinker();


      if( nos==8){
       entry[pwcount]='8' ;
       pwcount++;
       delay(250);
    Serial.print('8');

     

     
     
    }}
    if((val>195&&val<202)){(nos=9);
      blinker();


      if( nos==9){
       entry[pwcount]='9' ;
       pwcount++;
       delay(250);
    Serial.print('9');


     
     
    }}
    if((val>219&&val<226)){(nos=10);
       blinker();


      if( nos==10){
       entry[pwcount]='*' ;
       pwcount++;
       delay(250);
    Serial.print('*');

     

     
     
    }}
    if((val>230&&val<238)){(nos=11);
      blinker();


      if( nos==11){
       entry[pwcount]='0' ;
       pwcount++;
       delay(250);
    Serial.print('0');

     

     
     
    }}
    if((val>238&&val<245)){(nos=12);
       blinker();


      if( nos==12){
       entry[pwcount]='#' ;
       pwcount++;
       delay(250);
    Serial.print('#');
    pwcount=0;
      }
      for(k=0; k<=5; k++){//if the passwords match this part does not work
       if(k==5){
      Serial.println("Alarm Disabled");
       digitalWrite(3, HIGH);
       digitalWrite (4, LOW);
       digitalWrite (13,LOW);
       digitalWrite(PIR,LOW);
      
      
     
       break;
      
      } 

     

      }
      

       if(password[k] != entry[k]){// if the passwords do not match
         digitalWrite (4, HIGH);
        // Serial.println("Alarm ON 2 Seconds Until Armed");
         digitalWrite(3,LOW);
          break;
         armed();
         //delay(2000);
         //digitalWrite (13,HIGH);
      
         break;
      
      }
      // k<10
      for(k=0; k<10; k++)
      entry[k]=0;
      } 
    }
    }

     

    void reset(){
       int j;
       for(j=0; j<=1; j++)
       analogWrite(j,LOW);
    }

     

    void blinker(){// blinks the led for confirmation of button press
      digitalWrite(13, HIGH);
      delay(15);
      digitalWrite(13, LOW);
     
     
    }
    void printing(){
    delay(200);

     

     

     

    }
    void armed (){
      delay(100);
      digitalWrite(13,HIGH);
      digitalWrite(PIR,HIGH);
        Serial.println("Alarm Enabled");
         printing();
         Serial.println("Callibrating System Please wait..");
         printing();
         Serial.println("System Activating...");
         printing();
         Serial.println("All Systems Functioning Correctly");
          printing();
           delay(500);
         Serial.println("Alarm On");
    }

     

    void triggered(){
      digitalWrite(buzzerPin,HIGH);
      digitalWrite(13,LOW);
      delay(30);
      digitalWrite(buzzerPin,LOW);
      digitalWrite(13,HIGH);
      delay(30);
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 12 years ago

    Hi Billabott. I have made the changes as you have shown. The ldr should have been PIR for my motion sensor..( I hve changed that now) I will comment my sketch more to make it easier to read.

    The code keeps on looping I think because the keypad returns a constant 0 value from the analog pin. I want to make some code to ignore the value 0 but I cannot make it do that.

    Thanks for looking at it for me though

    Jeremy

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • billabott
    billabott over 12 years ago in reply to Former Member

    After looking at an official arduino.cc example of using analog input ports, I can safely recommend several changes to your code.

    As far as I know, A0 and A1 are just undefined variables and as such are of no value in configuring the analog pins.

     

    char password[]="1234",entry[10];

    int buttonPin=0;                  //  here

    int val;                              // here            

    int ldr=12;        // what the heck is this?  You Need a Whole Lot more comments documenting your intent

    int senseValue;                 // here

    int led=13;

    int sensePin=1;                   //here

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 12 years ago

    Thank you billabott I would appreciate that.

    Jeremy

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • billabott
    billabott over 12 years ago in reply to Former Member

    The usual way to handle the sequencing of actions in the code is via a "state machine".  Will have time to look at your code tomorrow unless someone else pipes in.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube