Bee Buzzing [PN532]
Note: This project requires quite a few components and can be convoluted to follow.
How does it works?
There will be 1 Arduino Mega and 4 Arduino Nanos.
The 4 Arduino Nanos will each be connected to a PN532 RFID Reader as well as a KY009 Multi Color LED.
While the Arduino Mega will be connected to a serial MP3 player.
Arduino UNO Connections
Connecting PN532 RFID Reader
5V can be connect to either a [+] or [VCC]
GND can be connected to either a [-] or [GND]
SCK [2]
MI [5]
MO/SDA/TX [3]
NSS/SCL/RX [4]
IRO [NOT CONNECTED]
RST [NOT CONNECTED]
Connecting KY009 LED
B [10 + 100Ω]
G [9 + 100Ω]
R [11 + 100Ω]
- [GND]
Sending Data to Arduino Mega
Connect [A4] and [A5] individually in a row on a breadboard
Arduino Mega Connections
Connecting the MP3 Player
Power can be connected to the breadboard power rails if required. But for this setup, Power is connected directly to the VCC and Ground is connected directly to the GRD of MP3 Player. TX is connected to [P10] while RX is connect to [P11].
Receiving Data from Arduino Unos
Connect [A4 Row] to SDA 20
Connect [A5 Row] to SCL 21
The Arduino Code [Arduino Uno]
The arduino code has been included in the file. Or provided below.
You will need the Adafruit PN532 Library.
[code]<font color="#5e6d03">#include</font> <font color="#434f54"><</font><font color="#d35400">Wire</font><font color="#434f54">.</font><font color="#000000">h</font><font color="#434f54">></font> <font color="#5e6d03">#include</font> <font color="#434f54"><</font><b><font color="#d35400">SPI</font></b><font color="#434f54">.</font><font color="#000000">h</font><font color="#434f54">></font> <font color="#5e6d03">#include</font> <font color="#434f54"><</font><font color="#000000">Adafruit_PN532</font><font color="#434f54">.</font><font color="#000000">h</font><font color="#434f54">></font> <font color="#434f54">// If using the breakout with SPI, define the pins for SPI communication.</font> <font color="#5e6d03">#define</font> <font color="#000000">PN532_SCK</font> <font color="#000000">(</font><font color="#000000">2</font><font color="#000000">)</font> <font color="#5e6d03">#define</font> <font color="#000000">PN532_MOSI</font> <font color="#000000">(</font><font color="#000000">3</font><font color="#000000">)</font> <font color="#5e6d03">#define</font> <font color="#000000">PN532_SS</font> <font color="#000000">(</font><font color="#000000">4</font><font color="#000000">)</font> <font color="#5e6d03">#define</font> <font color="#000000">PN532_MISO</font> <font color="#000000">(</font><font color="#000000">5</font><font color="#000000">)</font> <font color="#00979c">int</font> <font color="#000000">redpin</font> <font color="#434f54">=</font> <font color="#000000">11</font><font color="#000000">;</font> <font color="#00979c">int</font> <font color="#000000">bluepin</font> <font color="#434f54">=</font><font color="#000000">10</font><font color="#000000">;</font> <font color="#00979c">int</font> <font color="#000000">greenpin</font> <font color="#434f54">=</font><font color="#000000">9</font><font color="#000000">;</font> <font color="#434f54">//int state=0;</font> <font color="#00979c">int</font> <font color="#d35400">val</font> <font color="#434f54">=</font> <font color="#000000">1</font><font color="#000000">;</font> <font color="#434f54">// Use this line for a breakout with a SPI connection:</font> <font color="#000000">Adafruit_PN532</font> <font color="#000000">nfc</font><font color="#000000">(</font><font color="#000000">PN532_SCK</font><font color="#434f54">,</font> <font color="#000000">PN532_MISO</font><font color="#434f54">,</font> <font color="#000000">PN532_MOSI</font><font color="#434f54">,</font> <font color="#000000">PN532_SS</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#00979c">void</font> <font color="#5e6d03">setup</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">{</font> <font color="#d35400">pinMode</font> <font color="#000000">(</font><font color="#000000">redpin</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#d35400">pinMode</font> <font color="#000000">(</font><font color="#000000">greenpin</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#d35400">pinMode</font> <font color="#000000">(</font><font color="#000000">bluepin</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#d35400">Wire</font><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">1</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// join i2c bus with address #2</font> <font color="#d35400">Wire</font><font color="#434f54">.</font><font color="#d35400">onRequest</font><font color="#000000">(</font><font color="#000000">requestEvent</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// register event</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">115200</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#5e6d03">while</font> <font color="#000000">(</font><font color="#434f54">!</font><b><font color="#d35400">Serial</font></b><font color="#000000">)</font> <font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">10</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// for Leonardo/Micro/Zero</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"Hello!"</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">nfc</font><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#00979c">uint32_t</font> <font color="#000000">versiondata</font> <font color="#434f54">=</font> <font color="#000000">nfc</font><font color="#434f54">.</font><font color="#000000">getFirmwareVersion</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#434f54">!</font> <font color="#000000">versiondata</font><font color="#000000">)</font> <font color="#000000">{</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"Didn't find PN53x board"</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#5e6d03">while</font> <font color="#000000">(</font><font color="#000000">1</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// halt</font> <font color="#000000">}</font> <font color="#434f54">// Got ok data, print it out!</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"Found chip PN5"</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">(</font><font color="#000000">versiondata</font><font color="#434f54">>></font><font color="#000000">24</font><font color="#000000">)</font> <font color="#434f54">&</font> <font color="#000000">0xFF</font><font color="#434f54">,</font> <font color="#00979c">HEX</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"Firmware ver. "</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">(</font><font color="#000000">versiondata</font><font color="#434f54">>></font><font color="#000000">16</font><font color="#000000">)</font> <font color="#434f54">&</font> <font color="#000000">0xFF</font><font color="#434f54">,</font> <font color="#00979c">DEC</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#00979c">'.'</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">(</font><font color="#000000">versiondata</font><font color="#434f54">>></font><font color="#000000">8</font><font color="#000000">)</font> <font color="#434f54">&</font> <font color="#000000">0xFF</font><font color="#434f54">,</font> <font color="#00979c">DEC</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Set the max number of retry attempts to read from a card</font> <font color="#434f54">// This prevents us from waiting forever for a card, which is</font> <font color="#434f54">// the default behaviour of the PN532.</font> <font color="#000000">nfc</font><font color="#434f54">.</font><font color="#000000">setPassiveActivationRetries</font><font color="#000000">(</font><font color="#000000">0xFF</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// configure board to read RFID tags</font> <font color="#000000">nfc</font><font color="#434f54">.</font><font color="#000000">SAMConfig</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#d35400">val</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"Waiting for an ISO14443A card"</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">}</font> <font color="#00979c">void</font> <font color="#5e6d03">loop</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">{</font> <font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">100</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">redpin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// LED will be switched off </font> <font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">greenpin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// LED will be switched off </font> <font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">bluepin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// LED will be switched on </font> <font color="#434f54">// Serial.println(c);</font> <font color="#00979c">boolean</font> <font color="#000000">success</font><font color="#000000">;</font> <font color="#00979c">uint8_t</font> <font color="#000000">uid</font><font color="#000000">[</font><font color="#000000">]</font> <font color="#434f54">=</font> <font color="#000000">{</font> <font color="#000000">0</font><font color="#434f54">,</font> <font color="#000000">0</font><font color="#434f54">,</font> <font color="#000000">0</font><font color="#434f54">,</font> <font color="#000000">0</font><font color="#434f54">,</font> <font color="#000000">0</font><font color="#434f54">,</font> <font color="#000000">0</font><font color="#434f54">,</font> <font color="#000000">0</font> <font color="#000000">}</font><font color="#000000">;</font> <font color="#434f54">// Buffer to store the returned UID</font> <font color="#00979c">uint8_t</font> <font color="#000000">uidLength</font><font color="#000000">;</font> <font color="#434f54">// Length of the UID (4 or 7 bytes depending on ISO14443A card type)</font> <font color="#434f54">// Wait for an ISO14443A type cards (Mifare, etc.). When one is found</font> <font color="#434f54">// 'uid' will be populated with the UID, and uidLength will indicate</font> <font color="#434f54">// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)</font> <font color="#000000">success</font> <font color="#434f54">=</font> <font color="#000000">nfc</font><font color="#434f54">.</font><font color="#000000">readPassiveTargetID</font><font color="#000000">(</font><font color="#000000">PN532_MIFARE_ISO14443A</font><font color="#434f54">,</font> <font color="#434f54">&</font><font color="#000000">uid</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font><font color="#434f54">,</font> <font color="#434f54">&</font><font color="#000000">uidLength</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">requestEvent</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">success</font><font color="#000000">)</font> <font color="#000000">{</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"Found a card!"</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"UID Length: "</font><font color="#000000">)</font><font color="#000000">;</font><b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">uidLength</font><font color="#434f54">,</font> <font color="#00979c">DEC</font><font color="#000000">)</font><font color="#000000">;</font><b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">" bytes"</font><font color="#000000">)</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"UID Value: "</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#5e6d03">for</font> <font color="#000000">(</font><font color="#00979c">uint8_t</font> <font color="#000000">i</font><font color="#434f54">=</font><font color="#000000">0</font><font color="#000000">;</font> <font color="#000000">i</font> <font color="#434f54"><</font> <font color="#000000">uidLength</font><font color="#000000">;</font> <font color="#000000">i</font><font color="#434f54">++</font><font color="#000000">)</font> <font color="#000000">{</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">" 0x"</font><font color="#000000">)</font><font color="#000000">;</font><b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">uid</font><font color="#000000">[</font><font color="#000000">i</font><font color="#000000">]</font><font color="#434f54">,</font> <font color="#00979c">HEX</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">}</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">""</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">(</font><font color="#000000">uid</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font> <font color="#434f54">==</font> <font color="#000000">0x50</font><font color="#000000">)</font> <font color="#434f54">&&</font> <font color="#000000">(</font><font color="#000000">uid</font><font color="#000000">[</font><font color="#000000">1</font><font color="#000000">]</font> <font color="#434f54">==</font> <font color="#000000">0x66</font><font color="#000000">)</font> <font color="#434f54">&&</font> <font color="#000000">(</font><font color="#000000">uid</font><font color="#000000">[</font><font color="#000000">2</font><font color="#000000">]</font> <font color="#434f54">==</font> <font color="#000000">0x96</font><font color="#000000">)</font> <font color="#434f54">&&</font> <font color="#000000">uid</font><font color="#000000">[</font><font color="#000000">3</font><font color="#000000">]</font> <font color="#434f54">==</font> <font color="#000000">0x4A</font><font color="#000000">)</font> <font color="#000000">{</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"correct validation"</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#d35400">val</font> <font color="#434f54">=</font> <font color="#000000">2</font><font color="#000000">;</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#d35400">val</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">requestEvent</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">}</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">""</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Wait 1 second before continuing</font> <font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">}</font> <font color="#5e6d03">else</font> <font color="#000000">{</font> <font color="#434f54">// PN532 probably timed out waiting for a card</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"Timed out waiting for a card"</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#d35400">val</font> <font color="#434f54">=</font> <font color="#000000">1</font><font color="#000000">;</font> <font color="#000000">}</font> <font color="#d35400">val</font> <font color="#434f54">=</font> <font color="#000000">1</font><font color="#000000">;</font> <font color="#000000">}</font> <font color="#434f54">// function that executes whenever data is requested by master</font> <font color="#434f54">// this function is registered as an event, see setup()</font> <font color="#00979c">void</font> <font color="#000000">requestEvent</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">{</font> <font color="#434f54">// Serial.println("running");</font> <font color="#5e6d03">if</font><font color="#000000">(</font><font color="#d35400">val</font><font color="#434f54">==</font><font color="#000000">1</font><font color="#000000">)</font> <font color="#000000">{</font> <font color="#d35400">Wire</font><font color="#434f54">.</font><font color="#d35400">write</font><font color="#000000">(</font><font color="#000000">1</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// respond with message of 6 bytes</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"done nc"</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">}</font> <font color="#5e6d03">if</font><font color="#000000">(</font><font color="#d35400">val</font><font color="#434f54">==</font><font color="#000000">2</font><font color="#000000">)</font> <font color="#000000">{</font> <font color="#d35400">Wire</font><font color="#434f54">.</font><font color="#d35400">write</font><font color="#000000">(</font><font color="#000000">2</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// respond with message of 6 bytes</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"done gc"</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">correct</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">}</font> <font color="#000000">}</font> <font color="#00979c">void</font> <font color="#000000">correct</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">{</font> <font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">redpin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// LED will be switched on</font> <font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">greenpin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// LED will be switched off</font> <font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">bluepin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// LED will be switched off</font> <font color="#d35400">delay</font> <font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Wait for 3 seconds</font> <font color="#000000">}</font>
[/code]
Code Explanation
When the RFID reader recognises a tag string that is stored in the code, it will activate the relevant function in requestEvent() which will then send data to the main Arduino Mega and changes the color of the KY009 LED to green IF the card recognised is declared and had written the function with println("done gc").
When the RFID reader does not recognises a tag or in standby, it will activate the relevant function in requestEvent() which will then send data to the main Arduino Mega. Executing the the function with println("done nc").
Modifying the Code for Arduino Uno
To modify the stored RFID tag code
if ((uid[0] == 0x50) && (uid[1] == 0x66) && (uid[2] == 0x96) && uid[3] == 0x4A) { Serial.println("correct validation"); val = 2; Serial.println(val); requestEvent(); delay(1000); }
There are 4 uid[1-4] == 0x50. Change accordingly to the card data
To modify the color of the led [KY009]
digitalWrite (redpin, HIGH); // LED will be switched on digitalWrite (greenpin, LOW); // LED will be switched off digitalWrite (bluepin, LOW); // LED will be switched off
Modify the digitalwrite followed by the respective color pin (HIGH/LOW)
To modify the data sent to the Arduino Mega
void requestEvent() { if(val==1) { Wire.write(1); // respond with message of 6 bytes Serial.println("done nc"); } if(val==2) { Wire.write(2); // respond with message of 6 bytes Serial.println("done gc"); correct(); } }
Modify the Wire.write(x) value to be sent to the Arduino Mega
The Arduino Code [Arduino Mega]
The arduino code has been included in this file. Or provided below.
You will need the Serial MP3 library.
#include <Wire.h> #include "SerialMP3Player.h" #define TX 11 #define RX 10 SerialMP3Player mp3(RX,TX); int a; int b; int c; int d; int tt; void setup() { Wire.begin(); // join i2c bus (address optional for master) Serial.begin(9600); // start serial for output mp3.begin(9600); // start mp3-communication delay(500); // wait for init mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card delay(500); // wait for init } void loop() { // mp3.play(); // Play "hello.mp3". You must hear "Hello World" // delay(3000); // Serial.println("NO DATA"); // print the character Wire.requestFrom(1, 1); // request 6 bytes from slave device #1 while (Wire.available()) { // slave may send less than requested // int t = Wire.read(); // receive a byte as character a = Wire.read(); // receive a byte as character Serial.print(a); Serial.print("");// print the character } Wire.requestFrom(2, 1); // request 6 bytes from slave device #2 while (Wire.available()) { // slave may send less than requested // int c = Wire.read(); // receive a byte as character b = Wire.read(); // receive a byte as character Serial.print(b); Serial.print("");// print the character } Wire.requestFrom(3, 1); // request 6 bytes from slave device #3 while (Wire.available()) { // slave may send less than requested // int c = Wire.read(); // receive a byte as character c = Wire.read(); // receive a byte as character Serial.print(c); Serial.print("");// print the character } Wire.requestFrom(4, 1); // request 6 bytes from slave device #4 while (Wire.available()) { // slave may send less than requested // int c = Wire.read(); // receive a byte as character d = Wire.read(); // receive a byte as character Serial.print(d); Serial.print("");// print the character } tt = a+b+c+d; Serial.println(tt); if(tt==20){ mp3.play(); // Play "hello.mp3". You must hear "Hello World" delay(10000); } else{ mp3.reset(); // Play "hello.mp3". You must hear "Hello World" delay(3000); } delay(100); }
Modifying the Code for Arduino Mega
Wire.requestFrom(1, 1); // request 6 bytes from slave device #1 while (Wire.available()) { // slave may send less than requested // int t = Wire.read(); // receive a byte as character a = Wire.read(); // receive a byte as character Serial.print(a); Serial.print("");// print the character } Wire.requestFrom(2, 1); // request 6 bytes from slave device #2 while (Wire.available()) { // slave may send less than requested // int c = Wire.read(); // receive a byte as character b = Wire.read(); // receive a byte as character Serial.print(b); Serial.print("");// print the character } Wire.requestFrom(3, 1); // request 6 bytes from slave device #3 while (Wire.available()) { // slave may send less than requested // int c = Wire.read(); // receive a byte as character c = Wire.read(); // receive a byte as character Serial.print(c); Serial.print("");// print the character } Wire.requestFrom(4, 1); // request 6 bytes from slave device #4 while (Wire.available()) { // slave may send less than requested // int c = Wire.read(); // receive a byte as character d = Wire.read(); // receive a byte as character Serial.print(d); Serial.print("");// print the character }
To receive data from the Arduino Uno
Wire.requestFrom(3, 1)
Format [ID of Arduino Uno declared,1]
a = Wire.read(); // receive a byte as character
Serial.print(a);
To add up all of the data received from the Arduino Uno
a,b,c,d are all variables declared at the Wire.read()
New Variable tt = a+b+c+d;