Sari la conținut
ELFORUM - Forumul electronistilor

Buton arduino


loleksibolek

Postări Recomandate

Acum ceva timp am modificat un sketch (adresa de unde e luat e la începutul lui) pentru a-mi acționa un servomotor in funcție de un cititor rfid.De mers merge bine,memoreaza si șterge carduri ok,acționeaza servomotorul, dar m-am lovit de o problema: dacă panoul cu cititorul rfid și afișajul  e în afara ușii(asta face,acționează un zăvor cu servomotorul)din interior nu pot să deschid ușa .Așa că m-am gândit sa-i pun un buton care la apăsare să acționeze servomotorul câteva secunde apoi să-l aducă in poziția de închis.Eh,nu știu ce și cum dar nu-mi iese deloc,maxim ce am reușit e să citească la început butonul si să acționeze , apoi pauză...Deci dacă cineva are timp și se pricepe...

Acesta este 

 RFID CARD READER 
  By https://nonstopengineering.blogspot.com/
  Using Arduino,RFID-RC522 and LCD 16x2
  ------------------------------------------*/
#include "Servo.h"
#include <EEPROM.h>  //Library To read and write PICC's UIDs from/to EEPROM
#include <SPI.h>      //Library  RC522 Module uses SPI protocol
#include <MFRC522.h> //Library  RC522 Module
#include <LiquidCrystal.h> //Library  for LCD Display
int button = 2;
int buttonState = 0;
int servoPin = A5; 
Servo Servo1; 
boolean match = false; // initialize card match to false
boolean programMode = false; // initialize programming mode to false
int successRead; // Variable integer to keep if we have Successful Read from Reader
byte storedCard[4];   // Stores an ID read from EEPROM
byte readCard[4];           // Stores scanned ID read from RFID Module
byte masterCard[4]; // Stores master card's ID read from EEPROM
#define PinLed A0
#define PinAlarma A1
#define PinAcces A3
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.
LiquidCrystal lcd(3,4,5,6,7,8); //Initializing LCD PINS as (RS,EN,D4,D5,D6,D7)




void setup()
{
 for(int x=0; x<2; x++)
  {
    pinMode(button, INPUT); 
  }  
     
 

  
  // put your setup code here, to run once:
  Serial.begin(9600);  // Initialize serial communications with PC
  lcd.begin(16, 2);    //Initializing LCD 16x2
  pinMode(PinLed, OUTPUT);  //LED and Buzzer PIN OUT
  pinMode(PinAlarma,OUTPUT);
  pinMode(PinAcces,OUTPUT);
  digitalWrite(PinAcces,HIGH);

    Servo1.attach(servoPin);    
      Servo1.write(0); 
  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
  if (EEPROM.read(1) != 1) {  // Look EEPROM if Master Card defined, EEPROM address 1 holds if defined
    Serial.println("No Master Card Defined"); //When no Master Card in Your EEROM (Serial Display)
    Serial.println("Scan A PICC to Define as Master Card");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.println("Setare Mastercard  "); //When no Master Card in Your EEROM (LCD Display)
    lcd.setCursor(0, 1);
    lcd.println("Scanare card....."); //Scan any RFID CARD to set Your Master Card in Your EEROM (LCD Display)
    delay(1500);
    do {
      successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
    }
    while (!successRead); //the program will not go further while you not get a successful read
    for ( int j = 0; j < 4; j++ ) { // Loop 4 times
      EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
    }
    EEPROM.write(1, 1); //Write to EEPROM we defined Master Card.
    Serial.println("Master Card setat");
    
  }
  Serial.println("Master Card's UID");
  for ( int i = 0; i < 4; i++ ) {     // Read Master Card's UID from EEPROM
    masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
    Serial.print(masterCard[i], HEX); //Master Card only view in serial
     Serial.println("Waiting PICCs to bo scanned :)"); 
  }
  //WAITING TO SCAN THE RFID CARDS:
  Serial.println("");
  Serial.println("Scanare card");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.println("   Asteptati       ");
  lcd.setCursor(0, 1);
  lcd.println("Verificare ...     ");
  delay(1500);
}
void loop() {{
 
    
  
   



   
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("    SCANATI ");
  lcd.setCursor(0, 1);
  lcd.print("      CARDUL");

  
 
   
  
 
     
  do {
    successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
    if (programMode) {
      // Program Mode cycles through RGB waiting to read a new card
    }
    else {
   }}
  while (!successRead); //the program will not go further while you not get a successful read
  if (programMode) {
    if ( isMaster(readCard) ) {  //If master card scanned again exit program mode
      Serial.println("This is Master Card");
      Serial.println("Exiting Program Mode");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("   IESIRE");
      lcd.setCursor(0, 1);
      lcd.print("MOD MASTERCARD");
      delay(2000);
      programMode = false;
      return;
    }
    else {
      if ( findID(readCard) ) { //If scanned card is known delete it
        Serial.println("I know this PICC, so removing");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Card memorat!");
        lcd.setCursor(0, 1);
        lcd.print("Se sterge.....");
        delay(5000);
        deleteID(readCard);
        Serial.println("-----------------------------");
      }
      else {                    // If scanned card is not known add it
        Serial.println("I do not know this PICC, adding...");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Card nr:");
        lcd.setCursor(0, 1);
        lcd.print(readCard[0], HEX);
        lcd.print(readCard[1], HEX);
        lcd.print(readCard[2], HEX);
        lcd.print(readCard[3], HEX);
        lcd.print(readCard[4], HEX);
        delay(4000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Card nou");
        lcd.setCursor(0, 1);
        lcd.print("Se adauga.....");
        delay(5000);
        writeID(readCard);
        Serial.println("-----------------------------");
      }} }
  else {
    if ( isMaster(readCard) ) {  // If scanned card's ID matches Master Card's ID enter program mode
      programMode = true;
      Serial.println("Welcome to Mastercard Mode");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("MASTERCARD MOD");
      lcd.setCursor(0, 1);
      lcd.print("  Asteptati...");
      delay(3000);
      int count = EEPROM.read(0); // Read the first Byte of EEPROM that
      Serial.print("I have ");    // stores the number of ID's in EEPROM
      Serial.print(count);
      Serial.print(" record(s) on EEPROM");
      Serial.println("");
      Serial.println("Scan a PICC to ADD or REMOVE");
      Serial.println("-----------------------------");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("      Mod   ");
      lcd.setCursor(0, 1);
      lcd.print("  Add&remove");
      delay(2500);
    }
    else {
        
  
      
      if ( findID(readCard) ) {        // If not, see if the card is in the EEPROM
        Serial.println("Acces Granted");
       
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("    ACCES     ");
        lcd.setCursor(0, 1);
        lcd.print("     PERMIS   ");
        digitalWrite(PinAcces, LOW);
        digitalWrite(PinLed,HIGH);
        Servo1.write(55 );
        delay(10000);
        digitalWrite(PinAcces, HIGH);
        digitalWrite(PinLed,LOW);
        lcd.clear();
        Servo1.write (0);
      }
      else {        // If not, show that the ID was not valid
        Serial.println("Access Denied");
        for (int abcd = 0; abcd < 6; abcd++)
        {
        
        lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("   ACCES   ");
          lcd.setCursor(0, 1);
          lcd.print("     INTERZIS  ");
          digitalWrite(PinAlarma, HIGH);
          delay(1500);
          digitalWrite(PinAlarma, LOW);
          lcd.clear();
          lcd.print("  CARD  ");
          lcd.setCursor(0, 1);
          lcd.print("   NEAUTORIZAT  ");
          delay(1500);
        }       
   
    

  
        lcd.clear();
      }

      
      }}}}
int getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
    return 0;
  }
  // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  // I think we should assume every PICC as they have 4 byte UID
  // Until we support 7 byte PICCs

  Serial.println("Scanning PICC's UID.........");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   SCANARE");
  lcd.setCursor(0, 1);
  lcd.print("     CARD.....");
  delay(2000);
  
  
  for (int i = 0; i < 4; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println("");
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}
boolean isMaster( byte test[] ) {
  if ( checkTwo( test, masterCard ) )
    return true;
  else
    return false;
}

boolean checkTwo ( byte a[], byte b[] ) {
  if ( a[0] != NULL ) // Make sure there is something in the array first
    match = true; // Assume they match at first
  for ( int k = 0; k < 4; k++ ) { // Loop 4 times
    if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail
      match = false;
  }
  if ( match ) { // Check to see if if match is still true
    return true; // Return true
  }
  else  {
    return false; // Return false
  }}
boolean findID( byte find[] ) {
  int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  for ( int i = 1; i <= count; i++ ) {  // Loop once for each EEPROM entry
    readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
    if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
      return true;
      break; // Stop looking we found it
    }
    else {  // If not, return false
    }}
  return false;
}
void readID( int number ) {
  int start = (number * 4 ) + 2; // Figure out starting position
  for ( int i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes
    storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
  }
}
void deleteID( byte a[] ) {
  if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
    failedWrite(); // If not
  }
  else {
    int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
    int slot; // Figure out the slot number of the card
    int start;// = ( num * 4 ) + 6; // Figure out where the next slot starts
    int looping; // The number of times the loop repeats
    int j;
    int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
    slot = findIDSLOT( a ); //Figure out the slot number of the card to delete
    start = (slot * 4) + 2;
    looping = ((num - slot) * 4);
    num--; // Decrement the counter by one
    EEPROM.write( 0, num ); // Write the new count to the counter
    for ( j = 0; j < looping; j++ ) { // Loop the card shift times
      EEPROM.write( start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
    }
    for ( int k = 0; k < 4; k++ ) { //Shifting loop
      EEPROM.write( start + j + k, 0);
    }
    successDelete();
  }}
  //For Failed to add the card:
void failedWrite() {

  Serial.println("something wrong with Card");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SOMETHING WRONG");
  lcd.setCursor(0, 1);
  lcd.print("WITH CARD");
  delay(2000);
}
//For Sucessfully Deleted:
void successDelete() {
  Serial.println("Stergere reusita");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("STERGERE");
  lcd.setCursor(0, 1);
  lcd.print("EFECTUATA");
  delay(2000);
}
int findIDSLOT( byte find[] ) {
  int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
    readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
    if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
      // is the same as the find[] ID card passed
      return i; // The slot number of the card
      break; // Stop looking we found it
    }
  }
}
//For Sucessfully Added:
void successWrite() {

  Serial.println("Succesfully added");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" ADAUGARE");
  lcd.setCursor(0, 1);
  lcd.print("  EFECTUATA");
  delay(2000);
}
//For Adding card to EEROM:
void writeID( byte a[] ) {
  if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before!
    int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
    int start = ( num * 4 ) + 6; // Figure out where the next slot starts
    num++; // Increment the counter by one
    EEPROM.write( 0, num ); // Write the new count to the counter
    for ( int j = 0; j < 4; j++ ) { // Loop 4 times
      EEPROM.write( start + j, a[j] ); // Write the array values to EEPROM in the right position
    }
    successWrite();
  }
    
  
  else {
    failedWrite();
  }
}

Butonul l-am pus la pinul 2,deci am nevoie ca la apăsarea butonului să  acționeze sevomotorul  câteva secunde(Servo1.write(55)) apoi să revina la 0.

Mulțumesc.

Link spre comentariu
  • Răspunsuri 1
  • Creat
  • Ultimul Răspuns

Top autori în acest subiect

Zile populare

Top autori în acest subiect

Modifici linia asta:

if ( findID(readCard) ) {        // If not, see if the card is in the EEPROM

în

 

 

if ( findID(readCard) || digitalRead(2) ) {        // If not, see if the card is in the EEPROM

Asta dacă pinul 2 e tras spre alimentare ( 1 logic activ)

altfel inversezi condiţia de citire a pinului:

digitalRead(2) == LOW;

Editat de mihaicozac
Link spre comentariu

Creează un cont sau autentifică-te pentru a adăuga comentariu

Trebuie să fi un membru pentru a putea lăsa un comentariu.

Creează un cont

Înregistrează-te pentru un nou cont în comunitatea nostră. Este simplu!

Înregistrează un nou cont

Autentificare

Ai deja un cont? Autentifică-te aici.

Autentifică-te acum



×
×
  • Creează nouă...

Informații Importante

Am plasat cookie-uri pe dispozitivul tău pentru a îmbunătății navigarea pe acest site. Poți modifica setările cookie, altfel considerăm că ești de acord să continui.Termeni de Utilizare si Ghidări