You are on page 1of 5

#include<SoftwareSerial.

h>
/*4x4 Matrix Keypad connected to Arduino
This code prints the key pressed on the keypad to the serial port*/
#include <Keypad.h>
#include <LiquidCrystal.h>
const byte numRows= 1; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
LiquidCrystal lcd(12,11,5,4,3,2);
//keymap defines the key pressed according to the row and columns just as appear
s on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {10}; //Rows 0 to 3
byte colPins[numCols]= {9,8,7,6}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
SoftwareSerial mySerial(0,1);
int read_count=0,tag_count=0;
int j=0,k=0; // Variables to iterate in for loops
char data_temp, RFID_lock[16], RFID_unlock[16], data_store[10][16];
boolean disp_control;
boolean cardRead = false;
boolean matchingCard = true;
boolean occupied = false;
boolean onepressed = false;
boolean twopressed = false;
int timeCount = 0;
void setup() {
mySerial.begin(9600);
pinMode(13,OUTPUT);
Serial.begin(9600);
lcd.begin(20,4);
lcd.setCursor(5,1);
lcd.print("V E L O C K");
lcd.setCursor(2,2);
lcd.print("Press A to begin.");
}
void loop() {
char keypressed = myKeypad.getKey();
if (keypressed =='A'){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Press 1 to lock");
lcd.setCursor(9,1);
lcd.print("or");
lcd.setCursor (2,2);
lcd.print("Press 2 to unlock");
}

if (keypressed == '1'){
lcd.clear();
lcd.setCursor(6,1);
lcd.print("To lock,");
lcd.setCursor(3,2);
lcd.print("scan your card.");
onepressed = true;
}
if (keypressed == '2'){
//LCD display stuff
lcd.clear();
lcd.setCursor(7,0);
lcd.setCursor(5,1);
lcd.print("To unlock,");
lcd.setCursor(3,2);
lcd.print("scan your card.");
twopressed = true;
}
if (onepressed == true) {
LockCard();
cardRead = false;
onepressed = false;
}
if (twopressed == true) {
UnlockCard();
cardRead = false;
twopressed = false;
}
/*Serial.println("Lock or unlock");
LockCard();
cardRead = false;
Serial.println("Try to unlock");
UnlockCard();
cardRead = false;
digitalWrite(13,HIGH);
delay(4000);
digitalWrite(13,LOW);
delay(4000); */
}
boolean Compare() {
for (int i = 2; i < 15; i++) {
if (RFID_lock[i] != RFID_unlock[i]) {
matchingCard = false;
break;
}
}
if (matchingCard == false) {
return false;
}
else {
return true;
}
}
void LockCard() {
if (occupied == true) {
lcd.clear();

lcd.setCursor(4,0);
lcd.print("Bike space is");
lcd.setCursor(6,1);
lcd.print("occupied.");
lcd.setCursor (1,2);
lcd.print("To return, press A.");
cardRead = true;
}
else {
while (cardRead == false) {
GetNum(RFID_lock);
}
occupied = true;
cardRead = true;
SolenoidLock();
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Bike is locked.");
lcd.setCursor (3,2);
lcd.print("Please press A.");
}
}
void SolenoidLock() {
int count = 15;
lcd.clear();
lcd.setCursor(4,1);
lcd.print("Time to lock:");
digitalWrite(13,HIGH);
while (count != 0) {
if (count == 9) {
lcd.clear();
lcd.setCursor(4,1);
lcd.print("Time to lock:");
}
lcd.setCursor(9,2);
lcd.print(count);
delay(1250);
count --;
}
digitalWrite(13,LOW);
}
void SolenoidUnlock() {
int count = 15;
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Time to unlock:");
digitalWrite(13,HIGH);
while (count != 0) {
if (count == 9) {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Time to unlock:");
}
lcd.setCursor(9,2);
lcd.print(count);
delay(1250);
count --;
}

digitalWrite(13,LOW);
}
void UnlockCard() {
if (occupied == false) {
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Bike space is not");
lcd.setCursor(6,1);
lcd.print("occupied.");
lcd.setCursor (1,2);
lcd.print("To return, press A.");
cardRead = true;
}
else {
while (cardRead == false) {
GetNum(RFID_unlock);
}
boolean match = Compare();
if (match == true) {
SolenoidUnlock();
occupied = false;
cardRead = true;
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Bike is unlocked.");
lcd.setCursor (3,2);
lcd.print("Please press A.");
}
else {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Error - no match");
lcd.setCursor (1,2);
lcd.print("To return, press A.");
}
}
}
void GetNum(char tag[]) {
RecieveData(tag);
StoreData(tag);
PrintData(tag);
}
void RecieveData(char tag[]) {
while(mySerial.available()>0) {
data_temp=mySerial.read();
//Serial.println(data_temp);
tag[read_count]=data_temp;
read_count++;
}
}
void StoreData(char tag[]) {

if(read_count==16) {
disp_control=true;
for(k=tag_count;k<=tag_count;k++) {
for(j=0;j<16;j++) {
data_store[k][j]=tag[j];
}
}
tag_count++;
}
}
void PrintData(char tag[]) {
if(disp_control==true) {
for(j=2;j<15;j++) {
Serial.write(tag[j]);
}
Serial.println();
disp_control=false;
read_count = 0;
tag_count = 0;
cardRead = true;
}
}