Skip to content
Permalink
Browse files

A few tweaks and comments to arduino code. Also started on shock sens…

…or code
  • Loading branch information...
dpoulson committed Sep 19, 2019
1 parent cd7b19e commit 0316be9a3c07f84fcf3932c5cab5a6a604ee5260
@@ -23,10 +23,7 @@ products from Adafruit!
*/
/**************************************************************************/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
//#include <SoftSPI.h>
// #include <Adafruit_PN532.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>
@@ -41,9 +38,6 @@ products from Adafruit!
#define SPEAKER_PIN 25
#define TONE_PIN_CHANNEL 0

// Use this line for a breakout with a software SPI connection (recommended):
//Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
//SoftSPI mySPI(27,26,32);
PN532_SPI pn532spi(SPI, 21);
NfcAdapter nfc = NfcAdapter(pn532spi);
Adafruit_ST7735 tft = Adafruit_ST7735(16, 17, 23, 5, 9);
@@ -89,13 +83,24 @@ static unsigned char r2_logo_bits[] = {
0xfc, 0x03, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x80, 0xff, 0x0f,
0x00, 0x00, 0x00, 0x80, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x37, 0x00 };

void tone(uint8_t frequency, uint8_t duration) {
void tone(uint16_t frequency, uint16_t duration) {
Serial.print("Playing tone of ");
Serial.print(frequency);
Serial.print("Hz for ");
Serial.print(duration);
Serial.println("ms");
ledcWriteTone(TONE_PIN_CHANNEL, frequency);
_count = millis() + duration;
speaker_on = 1;
Serial.print("Current millis() = ");
Serial.print(millis());
Serial.print(" count = ");
Serial.println(_count);
}

void drawScreen() {
Serial.print("Current millis() = ");
Serial.println(millis());
tft.fillScreen(ST7735_BLACK);
tft.setCursor(4,0);
tft.print("R2 Builders");
@@ -107,6 +112,8 @@ void drawScreen() {
tft.print("Please Swipe");
tft.setCursor(55,56);
tft.print("Tag");
Serial.print("Current millis() = ");
Serial.println(millis());
}

void setup(void) {
@@ -152,14 +159,14 @@ void setup(void) {
tft.print("Initialising Reader");
tft.setCursor(4,36);
nfc.begin();

tft.print("Done");
delay(500);
Serial.println("Waiting for an ISO14443A Card ...");

// Setup done
drawScreen();
tone(1000,100);
tone(350,10);

}

@@ -170,6 +177,8 @@ void loop(void) {

// Mute Speaker
if(speaker_on) {
Serial.println("Speaker is on....");
Serial.println(millis());
if(millis() > _count) {
speaker_on = 0;
Serial.println("Muting Speaker");
@@ -180,12 +189,14 @@ void loop(void) {

// Clear screen of names
if (millis() > lastRead + 2000 && card_displayed) {
Serial.println("Drawing screen.... ");
card_displayed = 0;
drawScreen();
}

if (nfc.tagPresent())
if (nfc.tagPresent(100) && millis() > lastRead + 2000)
{
Serial.println("Tag Present");
NfcTag tag = nfc.read();
Serial.println(tag.getTagType());
Serial.print("UID: ");Serial.println(tag.getUidString());
@@ -205,7 +216,7 @@ void loop(void) {

if (message.getRecordCount() != 4) {
Serial.println("Not enough records");
tone(400, 100);
tone(155, 500);
tft.fillRect(0,35,200,200,ST7735_BLACK);
tft.setCursor(4, 44);
tft.print("Invalid Tag");
@@ -231,7 +242,7 @@ void loop(void) {
Serial.println(payloadAsString);
if (record.getType() != "T") {// || payloadAsString != "enUK R2 Builders Club" ){
Serial.print("First record is not a Text block");
tone(400, 100);
tone(155, 500);
tft.fillRect(0,35,200,200,ST7735_BLACK);
tft.setCursor(4, 44);
tft.print("Not a builders card");
@@ -326,12 +337,10 @@ void loop(void) {

// Lets notify that a card was read ok
// Wait a bit before reading the card again
tone(1000, 100);
tone(440, 100);
card_displayed = 1;
}
}
}
}
delay(2000);

}
@@ -1,19 +1,51 @@
/*
* ######################
* Overview
*
* Takes in a CSV line of the format via serial:
*
* droid name, droid ID, member name, member ID, ID code
*
* You will the place a tag on the reader/writer to write the correct details
*
*
* ######################
* Hardware
*
* This runs on an Arduino Mega (Needed for the memory), with SPI connections on:
*
* MISO - 50
* MOSI - 51
* SCK - 52
* SS - 53
*
* Along with power and ground
*
*
*/

#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_SPI pn532spi(SPI, 10);
PN532_SPI pn532spi(SPI, 53);
NfcAdapter nfc = NfcAdapter(pn532spi);

String droid_name;
String droid_uid;
String member_name;
String member_uid;
String title = "UK R2D2 Builders Club";
String id;
bool dataReady;
char droid_name[16] = "R2D2";
char droid_uid[4] = "0";
char member_name[32] = "Kenny Baker";
char member_uid[4] = "0";
char id[60] = "";

char id_url[100] = "";
char main_info[56] = "";

char title[22] = "UK R2D2 Builders Club"; // Main text file
char site_url[32] = "http://astromech.info/"; // Site URL
char mot_url[40] = "https://mot.astromech.info/id.php?id="; // Base URL for ID link

char line[128] = "";
int l=0;

void setup() {
@@ -24,91 +56,106 @@ void setup() {

void loop() {
Serial.println("Enter data");
delay(100);
char line[128];
char inByte;
memset(line, 0, sizeof(line));
Serial.println(line);
id[0] = (char)0;
line[0] = (char)0;
droid_name[0] = (char)0;
droid_uid[0] = (char)0;
member_name[0] = (char)0;
member_uid[0] = (char)0;


id_url[0] = (char)0;
main_info[0] = (char)0;
delay(1000);
bool dataReady = false;
l=0;
while(!dataReady) {
if (Serial.available() > 0) {
inByte = Serial.read();
//Serial.println(inByte);
char inByte = Serial.read();
Serial.print(inByte);

if (inByte == 13 || inByte == 10) {
dataReady = true;
Serial.println();
Serial.print("End of line detected: ");
Serial.print(inByte, OCT);
Serial.println();
line[l] = char(0);
} else {
line[l] = inByte;
}

line[l] = inByte;

//Serial.println((char)line[l]);
l++;
}
}
Serial.println("Data Ready");
line[l] = NULL;

droid_name = "";
droid_uid = "";
member_name = "";
member_uid = "";
id="";
int i=0;
while(line[i] != 44)
{
droid_name += (char)line[i];
i++;
}
Serial.print("Droid Name: ");
Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");

char * strtokIndx;

strtokIndx = strtok(line,",");
strcpy(droid_name, strtokIndx);

strtokIndx = strtok(NULL,",");
strcpy(droid_uid, strtokIndx);

strtokIndx = strtok(NULL,",");
strcpy(member_name, strtokIndx);

strtokIndx = strtok(NULL,",");
strcpy(member_uid, strtokIndx);

strtokIndx = strtok(NULL,",");
strcpy(id, strtokIndx);

strcat(main_info, droid_name);
strcat(main_info, "\n");
strcat(main_info, droid_uid);
strcat(main_info, "\n");
strcat(main_info, member_name);
strcat(main_info, "\n");
strcat(main_info, member_uid);

Serial.print("Droid Name: ");
Serial.println(droid_name);
i++;
while(line[i] != 44)
{
droid_uid += (char)line[i];
i++;
}
Serial.print("Droid ID: ");

Serial.print("Droid ID: ");
Serial.println(droid_uid);
i++;
while(line[i] != 44)
{
member_name += (char)line[i];
i++;
}
Serial.print("Member Name: ");

Serial.print("Member Name: ");
Serial.println(member_name);
i++;
while(line[i] != 44)
{
member_uid += (char)line[i];
i++;
}
Serial.print("Member ID: ");

Serial.print("Member ID: ");
Serial.println(member_uid);
i++;
for (int x=i; x < i+60; x++)
//while(line[i] != NULL || line[i] != 10 || line[i] != 13 || line[i] != 32)
{
id += (char)line[x];
//Serial.println(id);
//i++;
}
Serial.print("Badge ID: ");

Serial.print("Badge ID: ");
Serial.println(id);
Serial.print("Title: ");

Serial.print("Title: ");
Serial.println(title);
Serial.println("Details: ");
Serial.println(main_info);

Serial.print("MOT URL: ");
Serial.println(mot_url);
strcat(id_url, mot_url);
strcat(id_url, id);
Serial.print("PLI Status URL: ");
Serial.println(id_url);

delay(1000);

Serial.println("\nPlace a formatted Mifare Classic or Ultralight NFC tag on the reader.");
bool writetag=false;
while(!writetag) {
if (nfc.tagPresent()) {
Serial.println("Trying to write.....");
NdefMessage message = NdefMessage();
message.addTextRecord(title);
message.addUriRecord("http://astromech.info");
message.addTextRecord(droid_name + "\n" + droid_uid + "\n" + member_name + "\n" + member_uid + "\n");
message.addUriRecord("https://mot.r2djp.co.uk/id.php?id=" + id);
message.addUriRecord(site_url);
message.addTextRecord(main_info);
message.addUriRecord(id_url);

bool success = nfc.write(message);
if (success) {
@@ -121,5 +168,6 @@ void loop() {
}
delay(1000);
}

dataReady = false;
}

0 comments on commit 0316be9

Please sign in to comment.
You can’t perform that action at this time.