This is the third and final step of producing a GEMMA bracelet which can have its LED pattern programmed using an NFC capable phone.
The first step is to produce a dummy or fake input and have the GEMMA process it and play the relevant pattern on the LEDs. To do this we'll set up a new sketch and a string which will be the dummy input:
Here we have a method which takes a string of input and a number of LEDs. It will then take each colour and number of seconds, turn the LEDs on in sequence and turn them off again according to the seconds you've given it.
Let's put this to the test and upload it to the GEMMA. You should see each one light up according to the colour and time you've put in. Each LED has 8 letters in the string - the first 6 represent a hexadecimal number which gives the LED its colour - if you google "colour picker" most online colour picking tools will provide you with this. It's also provided in the app we created in phase 2. The second 2 are the number of seconds the light will be on before being switched off.
Now that we have a processing section, we need to pull in the actual input from the NFC antenna connected to the GEMMA. We're going to use the default library for ATTiny85 processors called TinyWire. In order to make the antenna work with this project, though, we need to modify it, so let's download the library from Github and open up TinyWireM.h.
Find the line beginning
This buffer needs changing so that it can handle at minimum 12 bits. On top of this, the buffer needs to be able to handle the maximum number of characters in your NFC text string. So, if you have 3 LEDs, you need 12 + 3 x 8 = 36.
Now let's actually implement TinyWire. Import it into the sketch. According to the M24SR documentation, this is the sequence along with the commands we need to send the NFC antenna in order to get the relevant data back:
SEND: [AC] 0x52
select NFC application
SEND: [AC] 0x2 0x0 0xa4 0x4 0x0 0x7 0xd2 0x76 0x0 0x0 0x85 0x1 0x1 0x0 0x35 0xc0
select CC file:
SEND: [AC] 0x3 0x0 0xa4 0x0 0xc 0x2 0xe1 0x3 0xd2 0xaf
read CC file length:
SEND: [AC] 0x2 0x0 0xb0 0x0 0x0 0x2 0x6b 0x7d
read CC file:
SEND: [AC] 0x3 0x0 0xb0 0x0 0x0 0xf 0xa5 0xa2
select NDEF file:
SEND: [AC] 0x2 0x0 0xa4 0x0 0xc 0x2 0x0 0x1 0x3e 0xfd
read NDEF message length:
SEND: [AC] 0x3 0x0 0xb0 0x0 0x0 0x2 0x40 0x79
read NDEF message
SEND: [AC] 0x2 0x0 0xb0 0x0 0x2 0x14 0x6c 0x3b
SEND: [AC] 0xc2 0xe0 0xb4
I2C essentially works by sending a command down the wire and handling the response, so essentially we just need to send each command and process the response in order, then when we get the final "message" from the antenna's stored memory, send that to the method we made earlier. Here's the full sketch including the output code:
A big thank you and round of applause to David Whale, AKA Whaleygeek for writing the vast majority of this part of the code. I had a degree to complete at the time of writing, so I unfortunately couldn't dedicate enough time to the project to write and test the code. The full repo of David's work can be found here on Github, including work to connect the NFC antenna with a Raspberry Pi.
You can check out the video of this here. Let me know if you have any new ideas for where we can take this project!