как объединить два последовательных фрагмента кода - PullRequest
0 голосов
/ 01 апреля 2019

У меня есть два фрагмента кода, которые, как я знаю, работают отдельно, как я хочу, но не могу заставить их работать вместе в одной программе

Я попытался отладить некоторые базовые эхо-запросы к консоли, я пытался создавать классы и вызывать эти классы внутри основного цикла. Это код, который я объединил.

*/

// 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-карта будет либо принята, либо отклонена, и соответствующий ответ будет доставлен через модуль преобразования текста в речь

...