I need to read the UID of a card. Make some measurements and then send the data to my receiver.
When i use the code just to read data , my project works perfectly. When i add the part of the code that is responsible for the data transmission it enters always the if(!mfrc522.PICC_ReadCardSerial()). It's the same code. What is wrong. I've tested it on different arduinos and PCs.
Code without Transmission
#include <SPI.h>
#include <MFRC522.h>
#include <HX711.h>
#define SS_PIN 10
#define RST_PIN 9
#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT 3
#define CLK 2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
HX711 scale(DOUT, CLK);
int counter;
float weight;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
Serial.println("SPI initiated");
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("MFRC22 initiated");
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
Serial.println("Weight Cell Initiated");
}
void loop() {
// put your main code here, to run repeatedly:
// Look for new cards
if ( !mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( !mfrc522.PICC_ReadCardSerial())
{
return;
}
else{
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
//Show UID on serial monitor
//Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
//Read UID on serial monitor
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
if (content.substring(1) == "B0 09 BA 28"){
Serial.print("New Chicken:");
for (byte i = 0; i < mfrc522.uid.size; i++){
//Show UID on serial monitor
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
counter = 0;
weight = 0;
while(scale.get_units()*0.453592 > 0.2){
weight = weight + scale.get_units()*0.453592;
counter++;
}
weight = weight / counter;
Serial.print("Chicken weights : ");
Serial.println(weight);
if( scale.get_units()*0.453592 > 0.1){
//send info
Serial.println("New Egg");
}
}
else{
Serial.println("No registered Chicken");
}
}
}
Code with Transmission
#include <SPI.h>
#include <MFRC522.h>
#include <HX711.h>
#include <RF22.h>
#include <RF22Router.h>
#define SS_PIN 10
#define RST_PIN 9
#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT 3
#define CLK 2
#define SOURCE_ADDRESS 4
#define DESTINATION_ADDRESS 26
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
HX711 scale(DOUT, CLK); // Create scale instance
RF22Router rf22(SOURCE_ADDRESS); // Create RF22 instance
char data_read[RF22_ROUTER_MAX_MESSAGE_LEN]; // Allocate some space for the string
uint8_t data_send[RF22_ROUTER_MAX_MESSAGE_LEN];
int successful_transmissions=0;
int failed_transmissions=0;
int counter;
float weight;
uint8_t rssi;
float Pr = -90;
float throughput = 0;
String content= "";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
if (!rf22.init()) //Initiate RF22
Serial.println("RF22 init failed");
if (!rf22.setFrequency(440.0))
Serial.println("setFrequency Fail");
rf22.setTxPower(RF22_TXPOW_20DBM);
rf22.setModemConfig(RF22::GFSK_Rb125Fd125);
rf22.addRouteTo(DESTINATION_ADDRESS, DESTINATION_ADDRESS);
Serial.print("Setup is Completed");
}
void loop() {
if ( !mfrc522.PICC_IsNewCardPresent()){ // Look for new cards
return;
}
else{
delay(100);
}
if ( !mfrc522.PICC_ReadCardSerial()){ // Select one of the cards
return;
}
else{
//Read UID
Serial.print("UID tag :");
content= "";
for (byte i = 0; i < mfrc522.uid.size; i++){
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
if (content.substring(1) == "B0 09 BA 28"){
printUID(mfrc522);
counter = 0;
weight = 0;
while(mfrc522.PICC_ReadCardSerial()){
weight = weight + scale.get_units()*0.453592;
counter++;
}
weight = weight / counter;
Serial.print("weight:");
Serial.println(weight);
//If there is an egg
if(scale.get_units()*0.453592 > 0.1){
sendData(content, &weight);
}
}
}
}
void printUID(MFRC522 mfrc522){
for (byte i = 0; i < mfrc522.uid.size; i++){
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
}
void sendData(String content, float *weight){
memset(data_read, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
memset(data_send, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
sprintf(data_read, "NEWEGG %s %f", content.c_str(), *weight);
data_read[RF22_ROUTER_MAX_MESSAGE_LEN - 1] = '\0';
memcpy(data_send, data_read, RF22_ROUTER_MAX_MESSAGE_LEN);
while(rf22.sendtoWait(data_send, sizeof(data_send), DESTINATION_ADDRESS) != RF22_ROUTER_ERROR_NONE){
Serial.println("sendtoWait failed");
failed_transmissions=failed_transmissions+1;
}
Serial.println("sendtoWait SUCCESS");
successful_transmissions=successful_transmissions+1;
rssi = rf22.rssiRead();
Pr = ((float)rssi - 230.0) / 1.8;
}