0

For the project, we want to import the code for the RFID code in the sketch of the project with cpp and h files, but it gives an error that the mfrc522 has multiple definitions. The first definition can be found in the cpp. Can you help us what to change? Here is the code of the cpp:

#include "Userinterface.h"

Userinterface::Userinterface() {
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
}

void Userinterface::checkCard() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) return;
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) return;

  // Show UID on serial monitor

  for (byte i = 0; i < mfrc522.uid.size; i++) {
     uid.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     uid.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  uid.toUpperCase();
  uid = uid.substring(1);
  Serial.println(uid);
  delay(3000);
}
Share a link to this question (includes your user id)
| edit | | close | delete |
0

In a setup with multiple files and classes you might need to use objects with pointer as done in assignment 4a.

The general way to use it is (example for display):

In .h file declare a pointer to the object:

U8X8_SSD1306_128X64_NONAME_HW_I2C * display;

In .cpp file initialize it in the constructor:

Userinterface::Userinterface() {
  display = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
}

Use an init method which is called in setup():

void Userinterface::init(Controller * c) {
  // initialize display:
  display->begin();
  display->setPowerSave(0);
  display->setFont(u8x8_font_pxplusibmcgathin_f);
}

An alternative can be to simply use a single sketch without classes. That might be best to begin with as it is a lot simpler and can be used for small scale test faster, without a lot of additional coding.

Share a link to this answer (includes your user id)
| edit | delete |

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.