Skip to content

Instantly share code, notes, and snippets.

@AungWinnHtut
Created July 31, 2018 14:40
Show Gist options
  • Save AungWinnHtut/c125f49b9b6a13b4f9d7179cdf06b3b6 to your computer and use it in GitHub Desktop.
Save AungWinnHtut/c125f49b9b6a13b4f9d7179cdf06b3b6 to your computer and use it in GitHub Desktop.
// http://geek.adachsoft.com
//card int hex uid
//card1 - 136-4-118-179-73 88-4-76-B3-49 4-76-B3-1A-4B-3F-80
//card2 - 204-153-242-137-46 CC-99-F2-89-2E CC-99-F2-89
//card3 - 245-74-201-73-63 F5-4A-C9-49-3F F5-4A-C9-49
//card4 - 23-103-238-43-181 17-67-EE-2B-B5 17-67-EE-2B
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include "pitch.h"
#define SERVO_PIN 3
#define RFID_SS_PIN 10
#define RST_PIN 9
#define BUZZER_PIN 2
SoftwareSerial BlueTooth(5, 4); //Txd - 10, Rxd - 11
LiquidCrystal_I2C lcd(0x27, 20, 4); //tochange with 16,2
Servo myservo; // create servo object to control a servo
MFRC522 mfrc522(RFID_SS_PIN, RST_PIN); // Create MFRC522 instance.
void funLCDprint(String data, int col = 0, int line = 0, int iDelayTime = 0, int clearstatus = 0)
{
if (clearstatus == 1)
{
lcd.clear();
}
lcd.setCursor(col, line); //Start at character location (col,line)
lcd.print(data);
Serial.println(data);
delay(iDelayTime);
}
void setup() {
lcd.begin();
lcd.backlight();
Serial.begin(9600);
BlueTooth.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
funLCDprint("Smart Gate Control");
Serial.println("");
myservo.attach(SERVO_PIN);
myservo.write( 80 );
delay(1000);
myservo.detach();
}
void loop() {
int j = 0;
char ca[6];
for (int i = 0; i < 6; i++)
{
ca[i] = '\0';
}
if (BlueTooth.available()) {
Serial.println("bt");
while (BlueTooth.available() > 0)
{
char ch = BlueTooth.read();
ca[j] = ch;
j++;
Serial.println(ch);
}
String cmd(ca);
Serial.println(cmd);
if (cmd == "open")
{
funLCDprint("opening gate by bluetooth");
gateopen();
}
else if (cmd == "close")
{
funLCDprint("closing gate by bluetooth");
gateclose();
}
}
else
{
}
//Look for new cards
//digitalWrite(RFID_SS_PIN, LOW);
if ( !mfrc522.PICC_IsNewCardPresent() ) {
return;
}
//Select one of the cards
if ( !mfrc522.PICC_ReadCardSerial() ) {
return;
}
String content = "";
byte letter;
for ( byte i = 0; i < mfrc522.uid.size; i++ ) {
content.concat(String(mfrc522.uid.uidByte[i], HEX));
if ( i < mfrc522.uid.size - 1 ) content += "-";
}
content.toUpperCase();
Serial.println();
funLCDprint("UID tag :'", 0, 1, 0, 1);
funLCDprint(content, 0, 2, 0, 1);
if ( content == "4-76-B3-1A-4B-3F-80") {
gateopen();
}
else if ( content == "17-67-EE-2B") {
gateclose();
}
else {
funLCDprint("Access denied");
beep2(1);
}
delay(100);
}
void gateopen()
{
beep1();
funLCDprint("Authorized access");
myservo.attach(SERVO_PIN);
myservo.write( 150 );
delay(500);
myservo.detach();
}
void gateclose()
{
beep1();
funLCDprint("Door Close");
myservo.attach(SERVO_PIN);
myservo.write( 80 );
delay(500);
myservo.detach();
}
void beep1()
{
tone(BUZZER_PIN, 1500);
delay(500);
noTone(BUZZER_PIN);
delay(500);
}
void beep2(int lapTime)
{
if (lapTime == 1)
{
for (uint8_t nLoop = 0; nLoop < 4; nLoop ++)
{
tone(BUZZER_PIN, NOTE_A5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_B5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_C5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_B5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_C5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_D5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_C5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_D5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_E5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_D5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_E5);
delay(NOTE_SUSTAIN);
tone(BUZZER_PIN, NOTE_E5);
delay(NOTE_SUSTAIN);
}
noTone(BUZZER_PIN);
}
else
{
tone(BUZZER_PIN, NOTE_G4);
delay(250);
tone(BUZZER_PIN, NOTE_C4);
delay(500);
noTone(BUZZER_PIN);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment