Skip to content

Instantly share code, notes, and snippets.

@DevilWAH
Created May 7, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevilWAH/60ad144d2858f53845ac2e80f84bb070 to your computer and use it in GitHub Desktop.
Save DevilWAH/60ad144d2858f53845ac2e80f84bb070 to your computer and use it in GitHub Desktop.
Morse-RFID
//we are going to read each buffer in turn then convert to morse so for ease of coding the temp buffers will be set here.
// define pins for RFID and create an instance for it
//// this block prints takes a block numbet and key and prints out the details with in it to serial.
void readblock(int block, MFRC522::MIFARE_Key key)
{
byte len = 18;
//------------------------------------------- GET FIRST NAME
MFRC522::StatusCode status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, tempbuffer, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT FIRST NAME
}
//*****************************************************************************************//
/*
* Initial Author: ryand1011 (https://github.com/ryand1011)
*
* Reads data written by a program such as "rfid_write_personal_data.ino"
*
* See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
*
* Uses MIFARE RFID card using RFID-RC522 reader
* Uses MFRC522 - Library
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
*
*
*
*/
#include <SPI.h>
#include <MFRC522.h>
#include <FastGPIO.h>
#define APA102_USE_FAST_GPIO
#include <APA102.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//we are going to read each buffer in turn then convert to morse so for ease of coding the temp buffers will be set here.
byte tempbuffer[18];
//*****************************************************************************************//
void setup() {
Serial.begin(115200); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
pinMode(LED_BUILTIN, OUTPUT); // LED_BUILTIN = 13 // set up internal LED for blinking
setupstrip();
}
//*****************************************************************************************//
void loop() {
// at the start of each loop reiniclise the reader to see the card as new.
/// start by seeting up Auth key
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
// checking untill new card is presented to reader-------------------------------------------
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println(F("**Card Detected:**"));
// Not needed but good for debugging -------------------------------------------
// mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
//-------------------------------------------
//once a new card is detected we loop though all the data blocks one by one in the to the 16 byte buffer.
Serial.println(F("\n**Start Reading**\n"));
for (uint8_t i = 1; i < 15; i++)
{
if (i != 3 && i!= 7 && i != 11 && i != 15 ) // Skip the non data blocks
{
readblock (i, key);
for (uint8_t i = 0; i < 16; i++)
{
updatestrip(tempbuffer[i]);
Serial.write(tempbuffer[i]);
}
}
}
//----------------------------------------
Serial.println(F("\n\n**End Reading**\n"));
delay(10); //change value if you want to read cards faster
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay (1000);
mfrc522.PCD_Init();
}
/// Set up the APA102 Array
#include <FastGPIO.h>
#define APA102_USE_FAST_GPIO
#include <APA102.h>
// Define which pins to use.
const uint8_t dataPin = 7;
const uint8_t clockPin = 8;
// Create an object for writing to the LED strip.
APA102<dataPin, clockPin> ledStrip;
// Set the number of LEDs to control.
const uint16_t ledCount = 60;
// Create a buffer for holding the colors (3 bytes per color).
rgb_color colors[ledCount];
const rgb_color cred(255,0,0);
const rgb_color cgreen(0,255,0);
const rgb_color cblack(0,0,0);
// Set the brightness to use (the maximum is 31).
const uint8_t brightness = 5;
// set lenth of dot
const uint16_t delaydot = 100;
// next set up the morse conversion array
const char morseCode[36][6] = {
"01", // a -- 97 ascii
"1000", // b
"1010", // c
"100", // d
"0", // e
"0010", // f
"110", // g
"0000", // h
"00", // i
"0111", // j
"101", // k
"0100", // l
"11", // m
"10", // n
"111", // o
"0110", // p
"1101", // q
"010", // r
"000", // s
"1", // t
"001", // u
"0001", // v
"011", // w
"1001", // x
"1011", // y
"1100", // z -- 122 ascii - position 26 in array
"11111", // 0 -- 48 ascii
"01111", // 1
"00111", // 2
"00011", // 3
"00001", // 4
"00000", // 5
"10000", // 6
"11000", // 7
"11100", // 8
"11110", // 9
};
// set all stip led colour to red then send a shif to make the correct colours along its lenth, writing any non 0 value to red or greeen before shift will work.
// the shift function only check is a led is black (off) and if so it keeps it black, other wise it is set to the correct colour for postion on the strip.
void setupstrip()
{
for(uint16_t i = 0; i < ledCount; i++) {
colors[i] = cred;
}
//do a shift to move all LED to correct colour andd add one last green
shiftLED();
colors[0] = cgreen;
ledStrip.write(colors, ledCount, brightness);
digitalWrite(LED_BUILTIN, LOW);
}
/// morse part of void loop
void updatestrip(char input)
{
if (input >= 'a' && input <= 'z') {
getmorse(input, true);
}
else if (input >= '0' && input <= '9') {
getmorse(input, false);
}
else if (input = ' ') {
for(uint16_t i = 0; i < 4 ; i++)
{
shiftLED();
colors[0] = cred;
ledStrip.write(colors, ledCount, brightness);
delay (delaydot);
}
}
}
void getmorse(char find, bool letter) {
// Serial.print(find); Serial.print(" = ");
byte arrayPosition = find - '\0'; // char to ascii decimal
if (letter) {
// Serial.println(morseCode[arrayPosition - 97]);
flashLED(arrayPosition - 97);
}
else {
// Serial.println(morseCode[arrayPosition - 22]);
flashLED(arrayPosition - 22);
}
}
void flashLED(byte address) { // you could add a beeper to pin 13 too
for (byte i = 0; i < 6; i++) {
if (morseCode[address][i] == '0') { // short pulse
digitalWrite(LED_BUILTIN, HIGH);
shiftLED();
colors[0]= cblack;
ledStrip.write(colors, ledCount, brightness);
delay(delaydot);
digitalWrite(LED_BUILTIN, LOW);
shiftLED();
colors[0] = cred;
ledStrip.write(colors, ledCount, brightness);
delay(delaydot);
}
else if (morseCode[address][i] == '1') { // long pulse
for(uint16_t i = 0; i < 3 ; i++)
{
digitalWrite(LED_BUILTIN, HIGH);
shiftLED();
colors[0] = cblack;
ledStrip.write(colors, ledCount, brightness);
delay (delaydot);
}
digitalWrite(LED_BUILTIN, LOW);
shiftLED();
colors[0] = cred;
ledStrip.write(colors, ledCount, brightness);
delay (delaydot);
}
}
// write 2 dots at the end of each letter to give a 3 dot gap between letters in code.
for(uint16_t i = 0; i < 2 ; i++)
{
shiftLED();
colors[0] = cred;
ledStrip.write(colors, ledCount, brightness);
delay (delaydot);
}
}
void shiftLED() // shift all LED up 1
{
for(uint16_t i = ledCount; i > 0; i--)
{
if ((colors[i - 1].red == 0 ) && (colors[i - 1].green == 0 ))
{
colors[i] = cblack;
}
else if (i > 19 && i < 40)
{
colors[i] = cred;
}
else
{
colors[i] = cgreen;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment