IAN LANG ELECTRONICS

The previous chapter went through the theory of using a matrix keypad, so let's combine it with a few other bits on the Arduino to make something practical. It's an alarm that gets set off when an IR distance sensor detects something passing in front of it. The alarm is armed and disarmed by the keypad.

A Matrix Keypad Activated Alarm

Go Back

Above is a small (52 sec) video showing the practical upshot of this. To make this, you will need in addition to your Arduino board, the following:

 

Sharp GP2Y0A21YK Infrared Distance Sensor

 

Piezzo Buzzer

 

Potentiometer (10k)

 

LED (5mm any colour)

 

8 ohm loudspeaker

 

4 resistor ~ 670 ohm

 

1 x resistor ~ 100 ohm

It's wired together as in the diagram on the left. The observant will have noticed that the basic layout for working the keypad is the same as we had in the last chapter as indeed is much of the code (we're using strings to handle the input).

 

The potentiometer is there to alter the sensitivity of the IR sensor. It's a good deal more sensitive in dark conditions than it is in light. The piezzo buzzer provides the key beeps and the output  is provided by an 8 ohm loudspeaker.

 

This wont give you an output that is particularly loud. If you require a really loud output, use the output from pin 12 to drive a relay, which switches 9V through a mini-siren. It's loud alright. Neighbour-maddeningly so.

 

All this hardware isn't going to bring us much joy if we don't have the right code to work the Arduino.

Here's a rather large chunk of it below:

 

Developed in Fritzing

#include <EEPROM.h>

String inputvalue="";

String PINvalue="";

String storecode="";

boolean pressed=false;

boolean godigit=false;

boolean changeflag=false;

boolean armed=false;

boolean continu=true;

int stopdigit=0;

int countup=0;

void setup(){

for(int t=2;t<5;t++){

pinMode(t,OUTPUT);

digitalWrite(t,1);}

}

void loop(){

poller();

if (inputvalue=="*"){changecode();}

else{sumup();}

if (changeflag==true){

  changeflag=false;PINvalue="";}

  if (armed==true){

    if (analogRead(A0)>175){continu=true;}}

    if (armed==true && continu==true){

    tone(12,440);}

  else {noTone (12);}  

  }

 

void poller(){

    if ( digitalRead(8)==0&&digitalRead(9)==0&&digitalRead(10)==0&&digitalRead(11)==0){

pressed=false;stopdigit=0;}

else{pressed=true;}

if (pressed==true&&stopdigit==0){

tone(5,440,150);

stopdigit=1;godigit=true;

           //**************TOP ROW**************

if (digitalRead(8)==1){

digitalWrite(2,0); //col 1

if (digitalRead(8)==0){

inputvalue="1";}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(8)==0){

inputvalue="2";}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

if (digitalRead(8)==0){

inputvalue="3";}

digitalWrite(4,1);}// col 3

 //**************SECOND ROW**************

if (digitalRead(9)==1){

digitalWrite(2,0); //col 1

if (digitalRead(9)==0){

inputvalue="4";}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(9)==0){

inputvalue="5";}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

if (digitalRead(9)==0){

inputvalue="6";}

digitalWrite(4,1);}// col 3

//**************THIRD ROW**************

if (digitalRead(10)==1){

digitalWrite(2,0); //col 1

if (digitalRead(10)==0){

inputvalue="7";}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(10)==0){

inputvalue="8";}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

if (digitalRead(10)==0){

inputvalue="9";}

digitalWrite(4,1);}// col 3

//**************BOTTOM ROW**************

if (digitalRead(11)==1){

 digitalWrite(2,0); //col 1

if (digitalRead(11)==0){

inputvalue= "*";}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(11)==0){

inputvalue="0";}

digitalWrite(3,1);//col 2

digitalWrite(4,0);//col 2

if (digitalRead(11)==0){

inputvalue="#";}

digitalWrite(4,1);}

}}

 

void sumup(){

  if (godigit==true){

  godigit=false;

PINvalue=PINvalue+inputvalue;

if (PINvalue.length()>3&&changeflag==false){

checker();}

}}

void checker(){

  storecode=""; int z=0;

 for (int t=1;t<5;t++){

  int incoming=EEPROM.read(t);

 storecode=storecode+incoming;}

 Serial.println();

if (storecode==PINvalue){

switch (armed){

case true:

armed=false; continu=false;digitalWrite(7,0);break;

case false:

armed=true;digitalWrite(7,1);break;

}

}

PINvalue="";storecode="";

}

void changecode(){

  storecode="";

  for(int t=0;t<10;t++){tone(5,440,150);delay(155);}

countup=0;changeflag=true;

do{

  inputvalue="-1";

poller();

if (inputvalue!="-1"){godigit=1;

tone(5,1000,150);

sumup();countup++;}

}while (countup<4);

for (int t=1;t<5;t++){

int incoming=EEPROM.read(t);

 storecode=storecode+incoming;}

if (storecode==PINvalue){pinnew();}

else{for (int t=0;t<10;t++){tone(5,220,150);delay(150);}}

PINvalue="";inputvalue="";changeflag=false;

}

void pinnew(){

    for(int t=0;t<10;t++){tone(5,440,150);delay(155);}

  countup=0; do{

  inputvalue="-1";  poller();

  if (inputvalue !="-1"){

  if (inputvalue!="#"&&inputvalue!="*"){

   int writevalue[]={0,1,2,3,4,5,6,7,8,9};

   for (int t=0;t<11;t++){

     if (inputvalue==writevalue[t]){

    countup++;EEPROM.write (countup,writevalue[t]);}}}}

  }while (countup<4);

    for(int t=0;t<10;t++){tone(5,440,150);delay(155);}

}

 

 

Over the page, we go through and see what each chunk is doing.

More>