¾ÆµÎÀÌ³ë ¼Ò½ºÄÚµå º´ÇÕ Áú¹®µå¸³´Ï´Ù.

cojubu72   
   Á¶È¸ 1564   Ãßõ 0    

 

#include <SoftwareSerial.h>


#define BT_RXD 8

#define BT_TXD 7


SoftwareSerial bluetooth(BT_RXD, BT_TXD);



int buzzerPin = 4;

bool isDetection = false;


void setup()

{

  Serial.begin(9600);

  bluetooth.begin(9600);

  pinMode(buzzerPin, OUTPUT);

}


void loop()

{

  byte data;

  while (bluetooth.available() > 0)

  {

    data = bluetooth.read();

    Serial.write(data);


    if (isDetection && data == 75)

    {

      Buzzer();

    }

    isDetection = false;

    if (data == 79)

    {

      isDetection = true;

    }

  }


}

void Buzzer()

{

  Serial.write("disconnected");

  tone(buzzerPin, 131);

  delay(5000);

  noTone(buzzerPin);

  delay(1000);


  tone(buzzerPin, 262);

  delay(5000);

  noTone(buzzerPin);

  delay(1000);

}

==========================================================

#include <Adafruit_Fingerprint.h>



#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)

// For UNO and others without hardware serial, we must use software serial...

// pin #2 is IN from sensor (GREEN wire)

// pin #3 is OUT from arduino  (WHITE wire)

// Set up the serial port to use softwareserial..

SoftwareSerial mySerial(2, 3);


#else

// On Leonardo/M0/etc, others with hardware serial, use hardware serial!

// #0 is green wire, #1 is white

#define mySerial Serial1


#endif



Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);


int Relay = 13;


void setup()

{

  pinMode(Relay, OUTPUT);

  //digitalWrite(Relay,HIGH);


  Serial.begin(9600);

  while (!Serial);  // For Yun/Leo/Micro/Zero/...

  delay(100);

  Serial.println("\n\nAdafruit finger detect test");


  // set the data rate for the sensor serial port

  finger.begin(57600);

  delay(5);

  if (finger.verifyPassword()) {

    Serial.println("Found fingerprint sensor!");

  } else {

    Serial.println("Did not find fingerprint sensor :(");

    while (1) {

      delay(1);

    }

  }


  Serial.println(F("Reading sensor parameters"));

  finger.getParameters();

  Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);

  Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);

  Serial.print(F("Capacity: ")); Serial.println(finger.capacity);

  Serial.print(F("Security level: ")); Serial.println(finger.security_level);

  Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);

  Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);

  Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);


  finger.getTemplateCount();


  if (finger.templateCount == 0) {

    Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");

  }

  else {

    Serial.println("Waiting for valid finger...");

    Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");

  }

   //digitalWrite(Relay,LOW);

}


void loop()                     // run over and over again

{

  //digitalWrite(Relay,LOW);

  getFingerprintID();

  delay(50);            //don't ned to run this at full speed.

}


uint8_t getFingerprintID() {

  uint8_t p = finger.getImage();

  switch (p) {

    case FINGERPRINT_OK:

      Serial.println("Image taken");

      break;

    case FINGERPRINT_NOFINGER:

      Serial.println("No finger detected");

      return p;

    case FINGERPRINT_PACKETRECIEVEERR:

      Serial.println("Communication error");

      return p;

    case FINGERPRINT_IMAGEFAIL:

      Serial.println("Imaging error");

      return p;

    default:

      Serial.println("Unknown error");

      return p;

  }


  // OK success!


  p = finger.image2Tz();

  switch (p) {

    case FINGERPRINT_OK:

      Serial.println("Image converted");

      break;

    case FINGERPRINT_IMAGEMESS:

      Serial.println("Image too messy");

      return p;

    case FINGERPRINT_PACKETRECIEVEERR:

      Serial.println("Communication error");

      return p;

    case FINGERPRINT_FEATUREFAIL:

      Serial.println("Could not find fingerprint features");

      return p;

    case FINGERPRINT_INVALIDIMAGE:

      Serial.println("Could not find fingerprint features");

      return p;

    default:

      Serial.println("Unknown error");

      return p;

  }


  // OK converted!

  p = finger.fingerSearch();

  if (p == FINGERPRINT_OK) {

    Serial.println("Found a print match!");


    digitalWrite(Relay, HIGH);



  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {

    Serial.println("Communication error");

    return p;

  } else if (p == FINGERPRINT_NOTFOUND) {

    Serial.println("Did not find a match");

    digitalWrite(Relay, LOW);

    return p;

  } else {

    Serial.println("Unknown error");

    return p;

  }


  // found a match!

  Serial.print("Found ID #"); Serial.print(finger.fingerID);

  Serial.print(" with confidence of "); Serial.println(finger.confidence);


  return finger.fingerID;

}


// returns -1 if failed, otherwise returns ID #

int getFingerprintIDez() {

  uint8_t p = finger.getImage();

  if (p != FINGERPRINT_OK)  return -1;


  p = finger.image2Tz();

  if (p != FINGERPRINT_OK)  return -1;


  p = finger.fingerFastSearch();

  if (p != FINGERPRINT_OK)  return -1;


  // found a match!

  Serial.print("Found ID #"); Serial.print(finger.fingerID);

  Serial.print(" with confidence of "); Serial.println(finger.confidence);

  return finger.fingerID;

}

========================================================

#include <Adafruit_Fingerprint.h>

#include <SoftwareSerial.h>


#define BT_RXD 8

#define BT_TXD 7


SoftwareSerial bluetooth(BT_RXD, BT_TXD);


int buzzerPin = 4;

int Relay = 13;

bool isDetection = false;


#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)

// For UNO and others without hardware serial, we must use software serial...

// pin #2 is IN from sensor (GREEN wire)

// pin #3 is OUT from arduino  (WHITE wire)

// Set up the serial port to use softwareserial..

SoftwareSerial mySerial(2, 3);


#else

// On Leonardo/M0/etc, others with hardware serial, use hardware serial!

// #0 is green wire, #1 is white

#define mySerial Serial1


#endif



Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);


void setup()

{

  pinMode(Relay, OUTPUT);

  pinMode(buzzerPin, OUTPUT);


  Serial.begin(57600);

  bluetooth.begin(57600);

  while (!Serial);  // For Yun/Leo/Micro/Zero/...

  delay(100);

  Serial.println("\n\nAdafruit finger detect test");


  finger.begin(57600);

  delay(5);

  if (finger.verifyPassword()) {

    Serial.println("Found fingerprint sensor!");

  } else {

    Serial.println("Did not find fingerprint sensor :(");

    while (1) {

      delay(1);

    }

  }


  Serial.println(F("Reading sensor parameters"));

  finger.getParameters();

  Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);

  Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);

  Serial.print(F("Capacity: ")); Serial.println(finger.capacity);

  Serial.print(F("Security level: ")); Serial.println(finger.security_level);

  Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);

  Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);

  Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);


  finger.getTemplateCount();


  if (finger.templateCount == 0) {

    Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");

  }

  else {

    Serial.println("Waiting for valid finger...");

    Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");

  }

}


void loop()                     // run over and over again

{

    byte data;

  while (bluetooth.available() > 0)

  {

    data = bluetooth.read();

    Serial.write(data);


    if (isDetection && data == 75)

    {

      Buzzer();

    }

    isDetection = false;

    if (data == 79)

    {

      isDetection = true;

    }

  }


  getFingerprintID();

  delay(50);            //don't ned to run this at full speed.

}


uint8_t getFingerprintID() {

  uint8_t p = finger.getImage();

  switch (p) {

    case FINGERPRINT_OK:

      Serial.println("Image taken");

      break;

    case FINGERPRINT_NOFINGER:

      Serial.println("No finger detected");

      return p;

    case FINGERPRINT_PACKETRECIEVEERR:

      Serial.println("Communication error");

      return p;

    case FINGERPRINT_IMAGEFAIL:

      Serial.println("Imaging error");

      return p;

    default:

      Serial.println("Unknown error");

      return p;

  }


  // OK success!


  p = finger.image2Tz();

  switch (p) {

    case FINGERPRINT_OK:

      Serial.println("Image converted");

      break;

    case FINGERPRINT_IMAGEMESS:

      Serial.println("Image too messy");

      return p;

    case FINGERPRINT_PACKETRECIEVEERR:

      Serial.println("Communication error");

      return p;

    case FINGERPRINT_FEATUREFAIL:

      Serial.println("Could not find fingerprint features");

      return p;

    case FINGERPRINT_INVALIDIMAGE:

      Serial.println("Could not find fingerprint features");

      return p;

    default:

      Serial.println("Unknown error");

      return p;

  }


  // OK converted!

  p = finger.fingerSearch();

  if (p == FINGERPRINT_OK) {

    Serial.println("Found a print match!");


    digitalWrite(Relay, HIGH);



  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {

    Serial.println("Communication error");

    return p;

  } else if (p == FINGERPRINT_NOTFOUND) {

    Serial.println("Did not find a match");

    digitalWrite(Relay, LOW);

    return p;

  } else {

    Serial.println("Unknown error");

    return p;

  }


  Serial.print("Found ID #"); Serial.print(finger.fingerID);

  Serial.print(" with confidence of "); Serial.println(finger.confidence);


  return finger.fingerID;

}


// returns -1 if failed, otherwise returns ID #

int getFingerprintIDez() {

  uint8_t p = finger.getImage();

  if (p != FINGERPRINT_OK)  return -1;


  p = finger.image2Tz();

  if (p != FINGERPRINT_OK)  return -1;


  p = finger.fingerFastSearch();

  if (p != FINGERPRINT_OK)  return -1;


  // found a match!

  Serial.print("Found ID #"); Serial.print(finger.fingerID);

  Serial.print(" with confidence of "); Serial.println(finger.confidence);

  return finger.fingerID;

}


void Buzzer()

{

  Serial.write("disconnected");

  tone(buzzerPin, 131);

  delay(5000);

  noTone(buzzerPin);

  delay(1000);


  tone(buzzerPin, 262);

  delay(5000);

  noTone(buzzerPin);

  delay(1000);

}

========================================


코드가 길어 죄송합니다.


첫번째 코드는 블루투스 연결 후 블루투스가 끊어지면 부저를 울리려고 만들었습니다.

두번째 코드는 릴레이에 솔레노이드와 지문인식장치로 잠금장치를 만들어 보고자 하였습니다.

세번째 코드는 위의 두 기능을 하나의 아두이노에 집어넣고 싶어 합쳐봤습니다.


다만 두 코드 합치지 않고 각자 썼을땐 문제가 없었는데 세번째 코드에서 부저 부분이 작동이 안됩니다.

블루투스 연결, 지문이 일치하면 잠금 off 모두 작동하는데 블루투스가 끊어질 때 부저를 울리도록 하는 기능이 작동하질 않습니다.


두 코드를 합치면서 제가 실수한 부분이 있는지 가르쳐주시면 정말 도움 많이 될 것 같습니다.

cojubu72
ªÀº±Û Àϼö·Ï ½ÅÁßÇÏ°Ô.
±¸Â÷´Ï 2022-12
void loop()                    // run over and over again
{
    byte data;
  while (bluetooth.available() > 0)
  {
    data = bluetooth.read();
    Serial.write(data);
    if (isDetection && data == 75)
    {
      Buzzer();
    }
    isDetection = false;
    if (data == 79)
    {
      isDetection = true;
    }
  }
  getFingerprintID();
  delay(50);            //don't ned to run this at full speed.
}

BT°¡ available ÇÒ¶§¸¸ loop°¡ À¯ÁöµÇ°í unavailable Çϸé while()¿¡¼­ ƨ°Ü ³ª°¡¸é¼­
get FingerpirntID(); delay(50)¸¸ ½ÇÇàµÇ°í
BT°¡ ¿ì¿¬È÷ available ÇØÁöÁö ¾Ê´Â ÀÌ»ó ´Ù½Ã Àú ¾ÈÀÇ ·çÇÁ·Î µé¾î°¡Áö ¸øÇÏ°Ô µË´Ï´Ù.

¾ÆµÎÀ̳ëÀÇ °æ¿ì loop() ÇÔ¼ö¿¡¼­ ¶Ç loop¸¦ µ¹¸®´Â°Ô ¿ÇÀº ÇÁ·Î±×·¡¹Ö ¹æÇâÀÌ ¾Æ´Ò°Ì´Ï´Ù.
loop() ÇÔ¼ö ÀÚü¸¦ »óÀ§ (º¸ÀÌÁö ¾Ê´Â) Äڵ忡¼­ while()¿¡ ÀÇÇؼ­ µ¹¸®°í Àֱ⠶§¹®ÀÌÁÒ.

¾Æ¹«Æ° ±»ÀÌ ¼öÁ¤À» ÇØ¾ß ÇÑ´Ù¸é

if(bt.available() > 0)
{ // BT Á¤»ó
}
else
{
Buzzer(); // BT not available
Serial.println("BT failed.");
// bluetooth.reinit()..
}

½ÄÀ¸·Î ±¸ÇöÇÏ½Ã¸é µÉ °Í °°½À´Ï´Ù.
±¸Â÷´Ï 2022-12
https://docs.arduino.cc/learn/built-in-libraries/software-serial#available

ÇØ´çÇÔ¼ö´Â BTÀÇ »ýÁ¸À» È®ÀÎÇÏ´Â°Ô ¾Æ´Ï¶ó ¼ÒÇÁÆ®¿þ¾î ½Ã¸®¾ó Æ÷Æ®¸¦ ÅëÇØ µ¥ÀÌÅÍ°¡ ÀÐÈ÷´Â°Ô ÀÖ³Ä? ¶ó´Â ±â´ÉÀ» °¡Áø ÇÔ¼ö °°½À´Ï´Ù.
BT ¿Í Åë½ÅÀ» ½ÃµµÇÏ°í timeout ÀÌÈÄ Á×¾ú´Ù°í ÆǴܵǸé else ºÎºÐÀ» ±¸µ¿Çϵµ·Ï º¯°æÇÒ ÇÊ¿ä°¡ ÀÖÀ» °Í °°½À´Ï´Ù.


QnA
Á¦¸ñPage 387/5680
2015-12   1482955   ¹é¸Þ°¡
2014-05   4946287   Á¤ÀºÁØ1
2006-12   4692   ¹Úµ¿¼·
2007-02   5361   ±èÁ¾±¸
2007-08   5105   ÀÌÀ±
2008-07   5148   À̱¤¼±
2003-09   10229   ±è¹Î¿µ
2005-05   5393   ÇÏ¿µÃ¢
2008-07   5559   Çѵμº
2006-03   4735   ³ëÇϼ®
2008-06   5015   ¿ø³«¿¬
2003-10   8871   ¹ÚÂùÀÏ
2005-08   6048   ÁöÇü±Ù
2008-06   5186   ±èÁöȯ
2007-12   8049   ³ëÇü¼®
2004-11   7102   ÀÌ¿ø±â
2005-02   8082   ÃÖ¿ø½Ä
2006-02   5208   °­´ë¼·
2006-07   5550   ÀÌâÁØ
2006-02   6091   ÀÌ¿µÀÍ
2004-05   7699   äÀ±¼º
2005-09   5825   È«Çöö