Unit 30: Unlocked and ready

 

 

 

Code for Sketch One: 


#include 
#include 
#include   // Include TinyDisplay library
#include   // Include the Servo library

#define SS_PIN 10        // Pin for RFID SDA
#define RST_PIN 9        // Pin for RFID RST
#define LED_PIN 6        // Pin for LED (updated to pin 6)
#define CLK_PIN 2        // Pin for display clock
#define DIO_PIN 4        // Pin for display data
#define SERVO_PIN 7      // Pin for servo

MFRC522 rfid(SS_PIN, RST_PIN);  // Create MFRC522 instance
TM1637TinyDisplay display(CLK_PIN, DIO_PIN);  // Create display instance
Servo myServo;  // Create Servo object

void setup() {
  SPI.begin();                // Initialize SPI bus
  rfid.PCD_Init();            // Initialize RFID reader
  pinMode(LED_PIN, OUTPUT);   // Set LED as output
  digitalWrite(LED_PIN, LOW); // Start with LED off

  display.begin();            // Initialize the display
  display.setBrightness(7);   // Set brightness level
  display.showString("----"); // Display dashes at startup
  
  myServo.attach(SERVO_PIN);  // Attach the servo to pin 7
  myServo.write(0);           // Start with servo at 0 degrees (closed position)
}

void loop() {
  // Look for new RFID cards
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    
    // RFID detected - show "oPEn" and light the LED
    display.showString("oPEn");  // Show "oPEn" on display
    digitalWrite(LED_PIN, HIGH); // Turn on LED (Pin 6)
    myServo.write(90);           // Open the servo (rotate to 90 degrees)
    delay(3000);                 // Wait for 3 seconds
    
    // Reset the display and turn off LED, and close the servo
    display.showString("----");  // Reset display
    digitalWrite(LED_PIN, LOW);  // Turn off LED (Pin 6)
    myServo.write(0);            // Close the servo (rotate back to 0 degrees)
    
    // Halt the PICC to prevent multiple readings of the same card
    rfid.PICC_HaltA();
    delay(1000);  // Add delay to avoid multiple detections
  }
}


  

 

Notes: