I’m trying to connect two RFID-readers (RC522) to my Uno. I need two because i’m working on a gate with bidirectional access.
I tried to change the chipSelectPin value and initialize again, but thad didn’t work. When I change the pins in setup it works, when I do it in an if/else loop it don’t.
pinMode(6, OUTPUT);
digitalWrite(6, HIGH); //Disable RFID1
pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
Is there some reason to set the mode for the pin by number AND name?
Which of the two RFID readers is now supposed to be active? You need a different chip select pin for each one, although it doesn't appear as though the library even knows about the chip select pin.
pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
Is there some reason to set the mode for the pin by number AND name?
Which of the two RFID readers is now supposed to be active? You need a different chip select pin for each one, although it doesn't appear as though the library even knows about the chip select pin.
No, that's a mistake.
On top is chipSelectPin set to 6.
if(sensor == 1){
int chipSelectPin = 14;
int NRSTPD = 15;
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
sensor = 2;
}
else{
int chipSelectPin = 6;
int NRSTPD = 5;
digitalWrite(14, HIGH);
digitalWrite(15, LOW);
sensor = 1;
//}
digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
digitalWrite(NRSTPD, HIGH);
MFRC522_Init();
This code was set in comments (in the loop), but here I tried to change the CSP.
Here, chipSelectPin is a local variable - it exists only until the } that ends the if statement. The second line is NOT affecting the value of the global chipSelectPin variable.
This works, but when I submit the commented code it doesn't...
Who are you submitting it to? What does the code do, when some stuff (what stuff?) is uncommented?
How are the two readers attached? How do they know which chip select pin is selecting them?
All the pins are connected to each board the same. Exept the chipSelectPin and NRSTPD (I don't know what it does) pins, those are going to only 1 reader.
The code (with some lines in comment) works, it scans the number and continues the programme.
My idea with the lines uncommented is to scan in loop 1 reader 1, in loop 2 reader 2, in loop 3 reader 1 etc. by checking the sensor variable (1 or 2) and setting the chipSelectPin to the other.
You need to post a link to the RFID reader you are using, and a link to the library you are using. The assumption that you are making is that the chip select pin actually does something, and that each reader is attached to a separate chip select pin. I need to verify that those are valid assumptions.
When I change the first chipSelectPin, I can switch between the readers.
But when I try it in the loop by using the sensor (1 or 2) variable, it won't switch...
The readers and ethernetshield communicate over the SPI bus.
But when I try it in the loop by using the sensor (1 or 2) variable, it won't switch...
Something happens. You need to tell us what happens. If changing the chipSelectPin variable's value doesn't change the active reader, that's one thing.
All the pins are connected to each board the same. Exept the chipSelectPin and NRSTPD (I don't know what it does) pins, those are going to only 1 reader.
How? Have you bent the pins on the shields so that they don't engage? Have you then connected a jumper wire from each of the chip select pins to the Arduino? A picture showing this might be helpful.