/******************************** name: BYJ48 Stepper motor code function: This program drives a unipolar or bipolar stepper motor and can be used for security system. LCD1602 module and 2 LEDs show the status of operation the piezo buzzer plays melody when the card accepted and rejected. The steps and motor speed can be adjusted easily to suit your needs. The molodies can be changed too. ********************************/ //Email:info@acoptex.com //Website:www.acoptex.com /********************************/ // include the library code #include #include #include #include #include #include #include "pitches.h" /********************************/ #define motorSteps 64 // change this depending on the number of steps // per revolution of your motor #define motorPin1 4 #define motorPin2 2 #define motorPin3 3 #define motorPin4 9 #define ledPin1 6 #define ledPin2 7 #define SS_PIN 10 #define RST_PIN 5 /********************************/ // initialize of the Stepper library: Stepper myStepper(motorSteps, motorPin1, motorPin2, motorPin3, motorPin4); // initialize of the RFID library: 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}; int speaker_pin = 8; // definition of cards: int cardRead; // card read 1 = good 0 = bad for playTune function int power = 8; int serNum[5]; int cards[][5] = { {0, 64, 134, 25, 223}, //Card 1 }; bool access = false; /********************************/ void setup() { // set the motor speed at 200 RPMS: myStepper.setSpeed(200); // 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); // define the LCD as 16 column by 2 rows lcd.begin (16, 2); // switch on the backlight lcd.setBacklight(HIGH); } /********************************/ void loop() { //goto column 4 and first line lcd.setCursor(4, 0); // Print at cursor Location: lcd.print("WELCOME"); Serial.println("WELCOME"); //goto column 2 and second line lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); delay(500); 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) { // Opening the door: cardRead = 1; playTune(cardRead); lcd.clear(); 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); myStepper.step(1000); // Delay set to pass the door = 5000 miliseconds = 5 seconds: lcd.clear(); lcd.setBacklight(HIGH); lcd.setCursor(2, 0); lcd.print("PASS THE DOOR"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); Serial.println("PASS THE DOOR"); delay(5000); // Closing the door: lcd.clear(); lcd.setBacklight(HIGH); lcd.setCursor(4, 0); lcd.print("CLOSING"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); Serial.println("CLOSING"); myStepper.step(-1000); // Access LED - green - off: digitalWrite(ledPin1, LOW); delay(250); digitalWrite(power, LOW); delay(250); lcd.clear(); } else { // No access - wrong card: cardRead = 0; playTune(cardRead); Serial.println("NO ACCESS"); lcd.clear(); lcd.setBacklight(HIGH); lcd.setCursor(3, 0); lcd.print("NO ACCESS"); lcd.setCursor(2, 1); lcd.print("ACOPTEX.COM"); delay(500); // No access LED - red - on: digitalWrite(ledPin2, HIGH); delay(500); // No access LED - red - off: digitalWrite(ledPin2, LOW); delay(250); digitalWrite(power, LOW); delay(250); lcd.clear(); } } rfid.halt(); } //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); } }