BREAKING NEWS

19 April 2016

UNLOCK VIA SECRET CODE

As this is the first article I write treating argument Arduino, it seems a must premise: do not consider myself an expert, I'm learning to take their first steps in the field of embedded del electronic devouring the many guides and video courses that They found in the network. So I hope that many of you will not hesitate to give me some advice!

Now we move on to describe this little application.
Below the chart produced by Fritzing:


It is a system that, following the insertion of a combination, it performs the following tasks:

sends a location set to the Servant
sets the power of the signaling LED
sends a signal to the buzzer
The necessary components, in addition to the Arduino UNO Rev3, are the following:
1Basic Servo
1Piezo Speaker
1Membrane Matrix Keypad
1Red LED – 5mmpackage 5 mm [THT]; colour Red
1Green LED – 5mmpackage 5 mm [THT]; colour Green
1100 Ω Resistorpackage 0805 [SMD]; tolerance ±5%; resistance 100Ω
2220 Ω Resistorpackage 0805 [SMD]; tolerance ±5%; resistance 220Ω

Now we turn to the analysis of the code (I hope that the comments in the code are enough to understand how it works):

/* Control Keyboard 3x4 by Georg T.*/




#include <Arduino.h>
#include <Servo.h> //lbrary for the servo functions
#include <Keypad.h> //library for the keypad functions

char code[] = "3688"; //declare the secret code
int position = 0;
int match_code = 1;
int greenPin = 13;
int redPin = 12;
int buzzer = 9;
Servo lock_servo; //initialize and name the servo
//---------keyboard declaration--------------------------
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys [ROWS][COLS] = {
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'*','0','#'},
};
byte rowPins[ROWS] = {5, 6, 7, 8,}; //connected to the row pinouts
byte colPins[COLS] = {2, 3, 4,}; //connected to the col pinouts
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//---------------------------------------------------------
void setup()
{
Serial.begin(9600);
keypad.setHoldTime(200); //set the minimum time to get the data of keys
keypad.setDebounceTime(50); //set the debounce time for keys
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(buzzer, OUTPUT);
lock_servo.attach(10);
}
//define a function to control the buzzer
void alert_sound(int pin, unsigned int freq, unsigned long time, int num)
 {
 for(int i=0; i<num; i++)
 {
 tone(pin, freq);
 delay(time);
 noTone(pin);
 }
 }

void loop()
{
 char key = keypad.getKey();
// jolly characters to lock, and restart the variable "position"
 if (key == '*' || key == '#' ) 
 {
 position = 0;
 match_code=1;
 }
//check if the key pressed match wit the number and its position in the array "code"
//if match increase the value of position by 1
 if (key == code[position])
 {
 position++; 
 }
//when all characters match with "code" set value of match_code
 if (position == 4) 
 {
 Serial.println("unlock"); //serial print for debug
 match_code=2;
 }
switch(match_code){
case 1: //lock: red LED ON; servo set to 90°
 digitalWrite(redPin, HIGH);
 digitalWrite(greenPin, LOW);
 alert_sound(9, 1000, 300, 1);//buzzer alert
 lock_servo.write(90); //set degrees position servo
 match_code = 0; //reset matc_code value
 break;
case 2: //unlock: green LED ON; servo set to 10°
 digitalWrite(redPin, LOW);
 digitalWrite(greenPin, HIGH);
 alert_sound(9, 4000, 100, 1); //buzzer alert
 lock_servo.write(10); //set degrees position servo
 match_code = 0; //reset match_code value
 break;
 }
}

Share this:

Post a Comment

 
Back To Top
Distributed By Blogger Templates | Designed By OddThemes