Hey, i am working on a project using RFID and IR transmitter/ receiver i have some problems when trying to include IRremote.hpp in a external file, my project was in just one file and it worked good but now i am trying to separate the functionality of of the project in different files so it be more clean and easy to modify but i get a lot of errors when trying to include IRremote library in the external file, any help is appreciated.Sry if there any mistakes regarding this post, first time when i ask a question here. This is my code :
//remote.ino
#include "IRHandler.h"
#include "RFIDHandler.h"
#include "Config.h"
#include <stdint.h>
const unsigned long IR_CODE = 0xFFA25D31;
const int IR_BITS = 32; // Lungimea în biți
unsigned long lastRFIDCheck = 0;
const unsigned long RFIDCheckInterval = 200;
byte data1[14] = {"Televizor N21"}; //The first data that needs to be written to the tag.
byte readbackblock[18]; //Array for reading out a block.
void setup() {
pinMode(BUTTON_PIN, INPUT);
Serial.begin(115200);
initializeRFID();
initializeIR();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastRFIDCheck >= RFIDCheckInterval) {
lastRFIDCheck = currentMillis;
checkRFID();
}
}
//IRHandler.h
#ifndef IRHANDLER_H
#define IRHANDLER_H
#include <IRremote.hpp>
extern IRrecv irrecv;
extern IRsend irsend;
void initializeIR();
void receiver();
void transmitter(const unsigned long IR_CODE, const int IR_BITS);
uint32_t mirror_u32(uint32_t input);
#endif
//IRHandler.cpp
#include "IRHandler.h"
#include "Config.h"
IRrecv irrecv(IR_RECEIVE_PIN);
IRsend irsend(IR_TRANSMITER_PIN);
void initializeIR() {
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR initialized!");
}
void transmitter(const unsigned long IR_CODE, const int IR_BITS)
{
if (digitalRead(BUTTON_PIN) == HIGH) {
// Trimite semnalul IR
Serial.println("Sending IR signal...");
irsend.sendNEC(IR_CODE, IR_BITS);
delay(1000);
}
}
void receiver()
{
if (IrReceiver.decode())
{
digitalWrite(LED, HIGH);
Serial.print("Value received: ");
Serial.println(mirror_u32(IrReceiver.decodedIRData.decodedRawData), HEX);
Serial.print("Protocol: ");
Serial.println(IrReceiver.decodedIRData.protocol);
irrecv.resume();
}
else
{
delay(100);
digitalWrite(LED, LOW);
}
}
// funtion to mirror data
uint32_t mirror_u32(uint32_t input)
{
uint32_t returnval = 0;
for (int i = 0; i < 32; i++)
{
int bit = input & 0x01;
returnval <<= 1;
returnval += bit;
input >>= 1;
}
return returnval;
}
RFIDHandler.h
#ifndef RFIDHANDLER_H
#define RFIDHANDLER_H
#include <MFRC522.h>
extern MFRC522 mfrc522;
extern MFRC522::MIFARE_Key key;
void initializeRFID();
void checkRFID();
int writeBlock(int blockNumber, byte arrayAddress[]);
int readBlock(int blockNumber, byte arrayAddress[]);
void get_uid();
#endif
RFIDHandler.cpp
#include "RFIDHandler.h"
#include "Config.h"
#include <SPI.h>
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void initializeRFID() {
SPI.begin();
mfrc522.PCD_Init();
Serial.println("RFID initialized!");
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
void checkRFID()
{
if (!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if (!mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.println("Card detected!");
get_uid();
mfrc522.PICC_HaltA();
}
int writeBlock(int blockNumber, byte arrayAddress[])
{
// check if the block number corresponds to data block or triler block, rtuen with error if it's trailer block.
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; // determine trailer block for the sector
if (blockNumber > 2 && (blockNumber + 1) % 4 == 0)
{
Serial.print(blockNumber);
Serial.println(" is a trailer block: Error");
return 2;
}
// authentication
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3; // return "3" as error message
}
// writing data to the block
status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
// status = mfrc522.MIFARE_Write(9, value1Block, 16);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Data write failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4; // return "4" as error message
}
Serial.print("Data written to block ");
Serial.println(blockNumber);
}
int readBlock(int blockNumber, byte arrayAddress[])
{
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; // determine trailer block for the sector
// authentication of the desired block for access
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed : ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3; // return "3" as error message
}
// reading data from the block
byte buffersize = 18;
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize); //&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
if (status != MFRC522::STATUS_OK)
{
Serial.print("Data read failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4; // return "4" as error message
}
Serial.println("Data read successfully");
}
void get_uid()
{
Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
mfrc522.PICC_HaltA();
}
//Config.h
#ifndef CONFIG_H
#define CONFIG_H
#define LED 2
#define BUTTON_PIN 7
#define IR_TRANSMITER_PIN 4
#define IR_RECEIVE_PIN 8
#define RST_PIN 9
#define SS_PIN 10
#endif
And this are the some of the errors there a many more like this ones, cant understand what is the problem because i dont have any FeedbackLEDControl:
\sketch\IRHandler.cpp.o (symbol from plugin):(.text+0x0): first defined here
\sketch\remote.ino.cpp.o (symbol from plugin): In function `FeedbackLEDControl':
(.text+0x0): multiple definition of `aggregateArrayCounts(unsigned char*, unsigned char, unsigned char*, unsigned char*)'
\sketch\IRHandler.cpp.o (symbol from plugin):(.text+0x0): first defined here
\sketch\remote.ino.cpp.o (symbol from plugin): In function `FeedbackLEDControl':
(.text+0x0): multiple definition of `IRrecv::decodeDistanceWidth()'
\sketch\IRHandler.cpp.o (symbol from plugin):(.text+0x0): first defined here
\sketch\remote.ino.cpp.o (symbol from plugin): In function `FeedbackLEDControl':
(.text+0x0): multiple definition of `IRrecv::decode()'
\sketch\IRHandler.cpp.o (symbol from plugin):(.text+0x0): first defined here
\sketch\remote.ino.cpp.o (symbol from plugin): In function `FeedbackLEDControl':
(.text+0x0): multiple definition of `IRrecv::read()'
\sketch\IRHandler.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status```