miercuri, 27 februarie 2013

Arduino si o tastatura cu 12 butoane

   Un sistem de control al accesului la intrarea in bloc sau dezactivarea unui sistem de alarma prin introducerea unui cod se poate face (si) cu ajutorul unui Arduino si a unei tastaturi cu 12 butoane.
   In articolul The Matrix Keypad how-to de pe site-ul oficial ARDUINO se gaseste o librarie si cateva exemple simple.
   In cartea "30 projects for Evil Genius" scrisa de Simon Monk sunt prezentate 2 proiecte (nr. 10 si 27) cu acest tip de tastatura pentru a realiza un sistem de acces cu cod din 4 cifre.
   Pe net am gasit cateva articole, care pot ajuta:
Advanced Arduino project: Security terminal
Arduino keyboard password

Arduino and Relays and a Keypad
   Pentru primele teste vreau sa folosesc placuta mea cu LED-ul RGB si tastatura conectata ca in carte.

   Am gasit la ADELAIDA o tastatura, care are codul KB304-PAW, iar pretul este de 16,5lei.
   Am gasit urmatoarele date despre ea:
   In mare, la apasarea uneia din cele 12 taste, se realizeaza un contact intre o anumita coloana si un rand, o prezentare am gasit-o in articolul PIC16F877a : Interfacing a Keypad and an LCD
   Un prim sketch este cel din cartea mentionata mai sus, cu modificarea pinilor alocati LED-urilor rosu si verde, respectiv luarea in considerarea si a unui LED albastru; cand sistemul este "armat" este aprins LED-ul rosu, la fiecare apasare a unei tase se aprinde scurt LED-ul albastru, iar daca codul este tastat corect sistemul este "dezarmat" si se aprinde LED-ul verde, pentru a trece din starea "armat" se apasa tastele * sau #:

// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
#include <Keypad.h>
char* secretCode = "1234";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key)
{
digitalWrite(bluePin, HIGH);
delay(100);
digitalWrite(bluePin, LOW);
}
if (key == '*' || key == '#')
{
position = 0;
setLocked(true);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4)
{
setLocked(false);
}
delay(100);
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}

joi, 28.02.2013
    Am facut cateva poze si un filmulet:
    Un prim filmulet se gaseste la adresa Arduino keypad password (1)

     Dupa ce m-am tot jucat cu tastatura si sketch-ul incarcat in Arduino, am constatat ca pot pacali "acces"-ul, deoarece se numara doar tastarile corecte daca nu se apasa tastele de resetare contor (* sau #), asa ca am modificat sketch-ul:


// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch version 1.1 (28.02.2013)
#include <Keypad.h>
char* secretCode = "1234";
int position = 0;
int position2 = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
}
if (key == '*' || key == '#')
{
position = 0;
position2 = 0;
setLocked(true);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4 )
{
setLocked(false);
}
delay(100);
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}

    Deoarece una din aplicatii ar putea fi un modul de acces intr-o scara de bloc sau incinta cu ajutorul unei yale electromagnetice, dupa introducerea codului corect bobina yalei trebuie alimentata o perioada scurta de timp (de exemplu 5 secunde), asa ca am modificat sketch-ul:


// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch version 1.2 (28.02.2013)
#include <Keypad.h>
char* secretCode = "1234";
int position = 0;
int position2 = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
}
if (key == '*' || key == '#')
{
position = 0;
position2 = 0;
setLocked(true);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4 )
{
setLocked(false);
}
delay(100);
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay (5000);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
}

    Am realizat un filmulet cu noua varianta, numit Arduino - door acces just 5 second

    Pentru ca modulul sa fie pregatit de introducerea codului fara a mai apasa * sau #, am mai completat ultimele linii din sketch:


if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay (5000);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
position = 0;
position2 = 0;
setLocked(true);
}
}

1 martie 2013
    Am facut un nou filmulet, numit Arduino - door acces just 5 second (2):

2 martie 2013
    Am facut mici modificari la ultimul sketch, pentru schimbare parola si/sau numar de caractere (5 in loc de 4).
    Pentru a schimba parola de 4 cifre, se modifica doar o linie
#include <Keypad.h>
char* secretCode = "2255";  //parola va fi 2255
int position = 0;
   Un filmulet, care arata modul de functionare se numeste Arduino - door acces just 5 second (3)

    Pentru a avea o parola de 5 cifre, se modifica linia dinainte
#include <Keypad.h>
char* secretCode = "20131";  //parola va fi 20131
int position = 0;

si se inlocuieste cifra 4 din linia ingrosata cu 5
if (key == secretCode[position])
{
position ++;
}
if (position == 5 & position2 == 5 ) // aici se verifica daca s-au tastat la rand cele 5 cifre, in ordinea corecta
{
setLocked(false);
}
delay(100);
}

   Am facut un filmulet, care se numeste Arduino - door acces just 5 second (4)

   Am adaptat si cel dea-l doilea sketch din carte, modificandu-l sa nu mai fie pacalit de numarul de tastari, punand si partea cu LED-ul albastru la fiecare tastare, rezultandu-mi:


// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch door lock ver.2.0 (02.03.2013)
#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "1234";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int solenoidPin = 13;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
loadCode();
flash();
updateOutputs();
}
void loop()
{

char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
}
if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
if (key == '#')
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
updateOutputs();
}
delay(100);
}

void updateOutputs()
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, HIGH);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, LOW);
}
}
void getNewCode()
{
flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
}
saveCode();
flash();flash();
}

void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}

void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}

void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(100);
}


    Filmuletul, care prezinta modul de functionare, se numeste Arduino - door lock ver.2.0 (code stored in EEPROM)

4 martie 2013
    Am modificat sketch-ul pentru a tine un electromagnet cuplat (alimentat) doar 5 secunde, cum sunt sistemele de acces in scarile de bloc... am pastrat partea de schimbare si memorare a codului in EEPROM (pentru a se pastra si la deconectarea alimentarii):


// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch door lock ver.2.1 (04.03.2013)
#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "1234";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int solenoidPin = 13;
int i = 0;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
loadCode();
flash();
updateOutputs();
}
void loop()
{

char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
}
if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
if (key == '#')
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
updateOutputs();
}
delay(100);
}

void updateOutputs()
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, HIGH);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, LOW);

delay (50);
for (int i=0; i <= 100; i++)
{
char key;
key = keypad.getKey();
if (key == '*')
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
delay (50);
}
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, HIGH);
locked = true;
position = 0;
position2= 0;
}
}

void getNewCode()
{
flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
}
saveCode();
flash();flash();
}

void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}

void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}

void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(100);
}

    Cand este LED-ul rosu aprins, electromagnetul nu este alimentat si usa este incuiata. Daca se introduce codul corect  din 4 cifre, se aprinde LED-ul verde si se alimenteaza timp de 5 secunde electromagnetul putandu-se deschide usa (daca nu se confirma codul, se apasa inainte tasta # care reseteaza numaratorul, dupa care se reintroduce codul).
    Daca in timpul in care LED-ul verde este aprins, se poate schimba codul de acces prin apasarea tastei * urmata de noul cod din 4 cifre... LED-urile se vor clipi intr-o anumita ordine pentru a confirma memorarea.

    Un filmulet cu functionarea, se numeste Arduino - door acces just 5 second (code stored in EEPROM)

3 comentarii:

  1. Exact ce aveam nevoie! Multumiri, Nicu FLORICA!

    RăspundețiȘtergere
  2. "Thank's for your tutorial.very useful for beginners like me..by the way, what if the government electricity goes out...):

    RăspundețiȘtergere
  3. When I disconnect it, the password is not saved. How can I get it saved?

    RăspundețiȘtergere