如何结合两个基于序列的代码

时间:2019-04-01 19:06:49

标签: arduino serial-port text-to-speech

我有两段代码,我知道如何分别工作,但无法在同一程序中一起工作

我尝试用一​​些基本的回显来调试控制台内容,尝试创建类并在主循环内调用这些类,这是我组合的代码。

*/

// include the SoftwareSerial library so we can use it to talk to the Emic 2 module
#include <SoftwareSerial.h>
#include <SPI.h>
#include <MFRC522.h>

#define rxPin   10  // Serial input (connects to Emic 2's SOUT pin)
#define txPin   11 // Serial output (connects to Emic 2's SIN pin)
#define ledPin  13  // Most Arduino boards have an on-board LED on this pin
#define SS_PIN 53
#define RST_PIN 49

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
// set up a new serial port
SoftwareSerial emicSerial =  SoftwareSerial(rxPin, txPin);

void setup()  // Set up code called once on start-up
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();
  // define pin modes
  pinMode(ledPin, OUTPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  // set the data rate for the SoftwareSerial port
  emicSerial.begin(9600);

  digitalWrite(ledPin, LOW);  // turn LED off

  /*
    When the Emic 2 powers on, it takes about 3 seconds for it to successfully
    initialize. It then sends a ":" character to indicate it's ready to accept
    commands. If the Emic 2 is already initialized, a CR will also cause it
    to send a ":"
  */
  emicSerial.print('\n');             // Send a CR in case the system is already up
  while (emicSerial.read() != ':');   // When the Emic 2 has initialized and is ready, it will send a single ':' character, so wait here until we receive it
  delay(10);                          // Short delay
  emicSerial.flush();                 // Flush the receive buffer
}

void loop()  // Main code, to run repeatedly
{

  // Speak some text
  emicSerial.print('S');
  emicSerial.print("Welcome to the Command center Mk1. Please tap your badge to continue");  // Send the desired string to convert to speech
  emicSerial.print('\n');
  digitalWrite(ledPin, HIGH);         // Turn on LED while Emic is outputting audio
  while (emicSerial.read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
  digitalWrite(ledPin, LOW);

  delay(500);    // 1/2 second delay
 if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "94 03 F5 17") 
//change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    emicSerial.print("Access granted");
    Serial.println();
    delay(3000);
  }

 else   {
    Serial.println(" Access denied");
    emicSerial.print("Warning, you are not authorised. This device will selfdestruct in 5 seconds. 5,4,3,2,1");
    delay(3000);
  }
}

我确实创建了一个TTS类和一个RFID类,可以调用一个或另一个,但不能两者都调用。 emic序列不起作用,RFID“启动”控制台文本也起作用,但是我不能两者都起作用。这个想法是RFID卡将被接受或拒绝,并将通过文本到语音模块传递适当的响应

0 个答案:

没有答案