/******************************** name: SG90 MICRO SERVO motor code function: This program drives a SG90 MICRO SERVO motor and can be used for security system. LCD1602 module and 3 LEDS show the status of operation the piezo buzzer plays melody when the card accepted and rejected. The angle of turn and melodies can be adjusted easily to suit your needs. ********************************/ //Email:info@acoptex.com //Website:www.acoptex.com /********************************/ // include libraries #include #include #include #include #include #include #include "pitches.h" /********************************/ #define ledPin1 6 #define ledPin2 7 #define ledPin3 4 #define SS_PIN 10 #define RST_PIN 5 /********************************/ // initialize of the RFID: RFID rfid(SS_PIN, RST_PIN); // initialise the LCD1602 I2C: LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // melodies definition: access and rejection - you can change melodies. see in file pitches.h int access_melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4}; int access_noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4}; int noaccess_melody[] = {NOTE_G2, NOTE_G2}; int noaccess_noteDurations[] = {8, 8}; // pins definition - Buzzer and Servo motor int speaker_pin = 8; int servoPin = 9; // note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10 // servo motor definition Servo Lock; // definition of cards: int cardRead; // card read 1 = good 0 = bad for playTune function int serNum[5]; int cards[][5] = { {0, 64, 134, 25, 223}, //user card 1 }; bool access = false; /********************************/ void setup() { // Servo motor attached to the pin: Lock.attach(servoPin); // initialize the Serial port: Serial.begin(9600); // initialize the SPI and RFID: SPI.begin(); rfid.init(); // set up speaker pin: pinMode(speaker_pin, OUTPUT); // set up Green and Red LED pins: pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); // set up the system status blue LED pin: pinMode(ledPin3, OUTPUT); // define the LCD as 16 column by 2 rows: lcd.begin (16, 2); // set up the Servo pin: pinMode(servoPin, OUTPUT); } /********************************/ void loop() { // set up initial Servo position in case of loosing power: Lock.write(90); lcd.setCursor(4, 0); lcd.print("WELCOME"); Serial.println("WELCOME"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); delay(250); digitalWrite(ledPin3, HIGH); if (rfid.isCard()) { if (rfid.readCardSerial()) { Serial.print(rfid.serNum[0]); Serial.print(" "); Serial.print(rfid.serNum[1]); Serial.print(" "); Serial.print(rfid.serNum[2]); Serial.print(" "); Serial.print(rfid.serNum[3]); Serial.print(" "); Serial.print(rfid.serNum[4]); Serial.println(""); for (int x = 0; x < sizeof(cards); x++) { for (int i = 0; i < sizeof(rfid.serNum); i++ ) { if (rfid.serNum[i] != cards[x][i]) { access = false; break; } else { access = true; } } if (access) break; } } if (access) { // blink the LED 2 times: blink(2); digitalWrite(ledPin3, HIGH); // switch on the backlight lcd.setBacklight(HIGH); lcd.setCursor(4, 0); lcd.print("OPENING"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); Serial.println("OPENING"); // access LED - green - on: digitalWrite(ledPin1, HIGH); // plays access melody cardRead = 1; playTune(cardRead); // releases the door, you need to adjust this to positioning the servo according your door locker: Lock.write(0); lcd.clear(); lcd.setCursor(2, 0); lcd.print("PASS THE DOOR"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); Serial.println("PASS THE DOOR"); // delay set to pass the door = 5000 miliseconds = 5 seconds: delay(5000); // closing the door: lcd.clear(); lcd.setCursor(4, 0); lcd.print("CLOSING"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); Serial.println("CLOSING"); // blink the LED 2 times: blink(2); digitalWrite(ledPin3, HIGH); // locks the door, brings the serve to the original position should be adjusted too: Lock.write(90); // access LED - green - off: digitalWrite(ledPin1, LOW); rfid.halt(); lcd.clear(); lcd.noBacklight(); // turn the LCD backlight off } else { // switch on the backlight lcd.setBacklight(HIGH); lcd.setCursor(3, 0); lcd.print("NO ACCESS"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); Serial.println("NO ACCESS"); // No access LED - red - on: digitalWrite(ledPin2, HIGH); // plays no access melody cardRead = 0; playTune(cardRead); // No access LED - red - off: digitalWrite(ledPin2, LOW); rfid.halt(); lcd.clear(); lcd.noBacklight(); // turn the LCD backlight off } } } // function to play the melody during Access and No access void playTune(int Scan) { if (Scan == 1) // card with Access scanned { for (int i = 0; i < 8; i++) //loop through the notes { // Good card read int access_noteDuration = 1000 / access_noteDurations[i]; tone(speaker_pin, access_melody[i], access_noteDuration); int access_pauseBetweenNotes = access_noteDuration * 1.30; delay(access_pauseBetweenNotes); noTone(speaker_pin); } } else // card with No Access scanned for (int i = 0; i < 2; i++) //loop through the notes { int noaccess_noteDuration = 1000 / noaccess_noteDurations[i]; tone(speaker_pin, noaccess_melody[i], noaccess_noteDuration); int noaccess_pauseBetweenNotes = noaccess_noteDuration * 1.30; delay(noaccess_pauseBetweenNotes); noTone(speaker_pin); } } /********************************/ // blink the blue status LED: void blink(int howManyTimes) { int i; for (i = 0; i < howManyTimes; i++) { digitalWrite(ledPin3, HIGH); delay(200); digitalWrite(ledPin3, LOW); delay(200); } }