Jump to content

Ardunio Switch control

Hey guys, I'm wondering how to program a switch into my code. My idea is adding it to the loop as if (inswitch= high), open lock.  Would this work?

code:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h> 

#define RST_PIN         9           
#define SS_PIN          10         

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String ok_rfid_1="3d265ac2";
String ok_rfid_2="35a5715";
//String ok_rfid_2="ffffffff"; //add as many as you need.
int lock=3; //Which pin the lock will be on if using a relay or solenoid or similar 
int LED = 6;
int inswitch = 1;

//Use the lines below if you plan on using a servo as a locking mechanism.

Servo myservo;  // create servo object to control a servo 
int posClosed = 360;    // variable to store the servo position for locked
int posOpen = 0;    //same for open...
 
/*
 * Initialize.
 */
void setup() {
    Serial.begin(9600);         // Initialize serial communications with the PC
    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card
    pinMode(LED,OUTPUT);
    //Choose which lock below:
    pinMode(lock, OUTPUT);
    myservo.attach(3);  // attaches the servo on pin 2 to the servo object 
    digitalWrite(LED,HIGH);
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

void open_lock() {
  
  //Use this routine when working with Servos.
  myservo.write(0); 
  digitalWrite(LED,LOW);
  delay(4000);
  myservo.write(160);
  digitalWrite(LED,HIGH);
}

void loop() {

      // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_1) {
      Serial.println("Welcome");
      open_lock();
    }

    if (read_rfid==ok_rfid_2) {
         Serial.println("Welcome");
         open_lock();
   }

   if (
    // else not needed. Anything else is not ok, and will not open the door...
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Are you talking about a switch in programming terms or in physical terms like a light switch?

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, though if you might want to add a pullup or pulldown resistor on that line and you may have problems with debouncing so adding a capacitor to the line might help.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×