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

cojubu72   
   Á¶È¸ 1587   Ãßõ 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 4747/5684
2014-05   4957514   Á¤ÀºÁØ1
2015-12   1493960   ¹é¸Þ°¡
2006-03   4917   ±èÀå¿ø
2006-03   5587   ±èÀ±Èñ
2006-03   6542   ±è¹Î¼º
2006-03   5371   ±èÁ¤Áß
2006-03   5498   ±è¼®ÇÑ
2006-03   4957   ¼±µ¿¾ð
2006-03   4865   ±è¿µÇÏ
2006-03   5671   ¾ÈâÁØ
2006-03   5585   ÀÌ´ë·Î
2006-03   5217   ±è³²¼º
2006-03   5584   ±è¿ìÁø
2006-03   5507   ÃÖ¸¶·ç
2006-03   5157   ¹Ú°æ¿í
2006-03   5156   俵Áø
2006-03   5557   À±Á¾¿Ï
2006-03   5592   ±èÃá½Ä
2006-03   5585   ÃÖÁ¾Çå
2006-03   5325   ÃÖÀϼ·
2006-03   4616   À±¿µ¹è
2006-03   5406   ¿À°­¼®