EmbDev.net

Forum: µC & Digital Electronics Arduino RFID Card Reader connecting with Processing


von Anna Angold (Guest)



Rate this post
useful
not useful
Hey,

I'm currently working on a project where I want to send data from 
Arduino to processing. Every time I place an rfid chip on the Arduino 
reader that switches the colors of a square in processing. So everytime 
I place a rfid-chip on my rfid-reader, arduino should send the data to 
processing, and depending on the card-number the color of the square in 
processing should change.
The connection is somehow working but I the colors of the square in 
processing are not switching.
I use the Arduino Elegoo Uno R3 and the joy-it RFID Reader.
Would love to get some help, how to solve this problem!
I attached a screenshot with the serial motor of the Arduino :)

Here is my Arduino-Code:
1
/*
2
 * ----------------------------------------------------------------------
3
 * Example program showing how to read new NUID from a PICC to serial.
4
 * ----------------------------------------------------------------------
5
 * https://circuits4you.com
6
 * 
7
 * RC522 Interfacing with NodeMCU
8
 * 
9
 Typical pin layout used:
10
 * ---------------------------------------------------------------------
11
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
12
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
13
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
14
 * ----------------------------------------------------------------------
15
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
16
 * SPI SS      SDA(SS)      10            53        D10        10               10
17
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
18
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
19
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
20
 */
21
22
#include <SPI.h>
23
#include <MFRC522.h>
24
25
constexpr uint8_t RST_PIN = 9;     // Configurable, see typical pin layout above
26
constexpr uint8_t SS_PIN = 10;     // Configurable, see typical pin layout above
27
 
28
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
29
30
MFRC522::MIFARE_Key key; 
31
32
// Init array that will store new NUID 
33
byte nuidPICC[4];
34
35
void setup() { 
36
  Serial.begin(9600);
37
  delay(1000);
38
  SPI.begin(); // Init SPI bus
39
  rfid.PCD_Init(); // Init MFRC522 
40
41
  for (byte i = 0; i < 6; i++) {
42
    key.keyByte[i] = 0xFF;
43
  }
44
45
  Serial.println(F("This code scan the MIFARE Classsic NUID."));
46
  Serial.print(F("Using the following key:"));
47
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
48
}
49
 
50
void loop() {
51
52
  // Look for new cards
53
  if ( ! rfid.PICC_IsNewCardPresent())
54
    return;
55
56
  // Verify if the NUID has been readed
57
  if ( ! rfid.PICC_ReadCardSerial())
58
    return;
59
60
  Serial.print(F("PICC type: "));
61
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
62
  Serial.println(rfid.PICC_GetTypeName(piccType));
63
64
  // Check is the PICC of Classic MIFARE type
65
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
66
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
67
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
68
    Serial.println(F("Your tag is not of type MIFARE Classic."));
69
    return;
70
  }
71
72
  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
73
    rfid.uid.uidByte[1] != nuidPICC[1] || 
74
    rfid.uid.uidByte[2] != nuidPICC[2] || 
75
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
76
    Serial.println(F("A new card has been detected."));
77
78
    // Store NUID into nuidPICC array
79
    for (byte i = 0; i < 4; i++) {
80
      nuidPICC[i] = rfid.uid.uidByte[i];
81
    }
82
   
83
    /*Serial.println(F("The NUID tag is:"));
84
    Serial.print(F("In hex: "));
85
    printHex(rfid.uid.uidByte, rfid.uid.size);
86
    Serial.println();
87
    Serial.print(F("In dec: "));
88
    printDec(rfid.uid.uidByte, rfid.uid.size);
89
    Serial.println();
90
  }
91
  */
92
  Serial.println(F(""));
93
    //Serial.print(F(""));
94
    printHex(rfid.uid.uidByte, rfid.uid.size);
95
    Serial.println();
96
97
    }
98
  else Serial.println(F("Card read previously."));
99
100
  // Halt PICC
101
  rfid.PICC_HaltA();
102
103
  // Stop encryption on PCD
104
  rfid.PCD_StopCrypto1();
105
}
106
107
108
/**
109
 * Helper routine to dump a byte array as hex values to Serial. 
110
 */
111
void printHex(byte *buffer, byte bufferSize) {
112
  for (byte i = 0; i < bufferSize; i++) {
113
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
114
    Serial.print(buffer[i], HEX);
115
    
116
  }
117
}
118
119
/**
120
 * Helper routine to dump a byte array as dec values to Serial.
121
 */
122
void printDec(byte *buffer, byte bufferSize) {
123
  for (byte i = 0; i < bufferSize; i++) {
124
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
125
    Serial.print(buffer[i], DEC);
126
    
127
  }
128
}

Here is my Processing-Code:
1
import processing.serial.*;
2
3
Serial myPort;         // The serial port  
4
String finalRead = ""; // Variable with final data  
5
String inputRead = ""; // Variable that recieves the data  
6
int state = 0;         // Variable to store the program state  
7
8
void setup() {
9
10
  size(800, 800); //Size of the program window
11
  
12
13
  // List all the available serial ports
14
  printArray(Serial.list());
15
16
  // Open the port you are using at the rate you want:
17
  myPort = new Serial(this, Serial.list()[4], 9600);
18
19
  // The rectangle will be placed based on it's own center
20
  rectMode(CENTER);
21
22
}
23
24
void draw() {
25
  //Background colour
26
background(50, 50, 150); 
27
  /*When the serial port is available the data from the RFID is 
28
    loaded into the finalRead variable.*/
29
30
  while (myPort.available() > 0)
31
  {
32
    //The c variable is updated with the read method
33
34
    char c=(char)myPort.read();
35
36
    /*
37
      When processing recieves data from Arduinos println command 
38
      it is recieved with a \r in the beginning
39
      and a c\ in the end. 
40
      Therefore we add c to inputRead when 
41
      we are not encountering \r.
42
43
      If we encounter \n we know that the string is done reading. 
44
      The string is loaded into the finalRead variable and 
45
      the data is cleared from inputRead.
46
     */
47
48
    if (c=='\n')
49
    {
50
      finalRead = inputRead;
51
      inputRead = ""; 
52
      println("data: " + finalRead);
53
    } else if (c!='\r')
54
    {
55
      inputRead+=c;
56
    }
57
  } 
58
59
   /*Now the program checks if the data is equal to the data
60
   on a wanted RFID-chip. If thats the case the state of the   
61
   program changes. Here you need to fill in data from a specific 
62
   RFID-chip*/
63
64
  if (finalRead.equals("D36BEF02")) {
65
    state = 1;
66
  }
67
68
  /*Another RFID-chip if-statement with another state. You can do this with as many RFID-chips you want.*/
69
70
  if (finalRead.equals("49 CD 4A A3")) {
71
    state = 2;
72
  }
73
74
  /*If the state is changed to 1, the colour of the center Rectangle 
75
    is changed to red.*/
76
77
  if (state == '1') {
78
    background(#BC4A4A); 
79
    //fill(#ffffff);
80
  }
81
82
  /*If the state is change to 2, the colour of the center Rectangle is changed to green.*/
83
  if (state == '2') {
84
    background(#BC4A4A); 
85
    //fill(#000000);
86
  }
87
88
  /*The program now draws a rectangle with a fill colour dependent on the state variable*/
89
  rect(width/2, height/2, width-100, height-100);
90
}

von Marc V. (Company: Vescomp) (logarithmus)


Rate this post
useful
not useful
Anna Angold wrote:
> The connection is somehow working but I the colors of the square in
> processing are not switching.

How should the colour change?
1
if (state == '1') {
2
  background(#BC4A4A); // !!!!!!!!!
3
  //fill(#ffffff);
4
}
5
/*If the state is change to 2, the colour of the center Rectangle is changed to green.*/
6
if (state == '2') {
7
  background(#BC4A4A); //  !!!!!!!!!!!
8
  //fill(#000000);
9
}

Background colour stays the same for both options...

: Edited by User

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig