Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ----------------------------------------------------------------------------
- * Arduino Uno distance NFC reader with OLED display
- *
- * ----------------------------------------------------------------------------
- * The following sketch is intended to be used to mesaure the ditance between the
- * NFC board and NFC tag. Once a tag is detected an interrupt is fired and the
- * meassured distance displayed.
- *
- * Typical pin layout used:
- * ------------------------------------------------
- *
- * Arduino MFRC5522 Ultrasound OLED Display
- * Uno Pin Pin Pin
- * ------------------------------------------------
- * 2 IRQ - -
- * 3 - ECHO -
- * 4 - TRIGGER -
- * SCL - - SCL
- * SDA - - SDA
- * 9 RST - -
- * 10 SDA - -
- * 11 MOSI - -
- * 12 MISO - -
- * 13 SCK - -
- *
- */
- #include <SPI.h>
- #include <MFRC522.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include "U8x8lib.h" //Using u8g2 library
- // Pin setup
- constexpr uint8_t RST_PIN = 9; // NFC Reset
- constexpr uint8_t SS_PIN = 10; // Select
- constexpr uint8_t IRQ_PIN = 2; // Interrupt
- constexpr int TRIGGER_PIN = 4; // US trigger
- constexpr int ECHO_PIN = 3; // US echo
- constexpr int LED = 7; // US echo
- constexpr uint8_t BUZZ_PIN = 8; // Buzzer
- // Variables
- volatile long duration;
- volatile int distance;
- volatile bool bNewInt = false;
- byte regVal = 0x7F;
- int offset=20;
- String distStr;
- // Config variables
- const float USONIC_DIV = 58.0;
- const int MEASURE_SAMPLE_DELAY = 3;
- const int MEASURE_SAMPLES = 20;
- const int MEASURE_DELAY = 50;
- // NFC Operation init.
- MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
- MFRC522::MIFARE_Key key;
- void activateRec(MFRC522 mfrc522);
- void clearInt(MFRC522 mfrc522);
- U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8( /* clock=*/ A5, /* data=*/ A4, /* reset=*/ U8X8_PIN_NONE); // OLEDs without Reset of the Display
- /**
- * Initialize.
- */
- void setup() {
- Serial.begin(115200); // Initialize serial communications with the PC
- SPI.begin(); // Init SPI bus
- pinMode(TRIGGER_PIN, OUTPUT); // Trigger Output
- pinMode(ECHO_PIN, INPUT); // Echo Input
- pinMode(BUZZ_PIN, OUTPUT); // Buzzer Output
- pinMode(LED, OUTPUT); // LED Output
- mfrc522.PCD_Init(); // Init MFRC522 card
- /* setup the IRQ pin*/
- pinMode(IRQ_PIN, INPUT_PULLUP);
- Serial.println("1");
- /*
- * Allow the ... irq to be propagated to the IRQ pin
- * For test purposes propagate the IdleIrq and loAlert
- */
- regVal = 0xA0; //rx irq
- mfrc522.PCD_WriteRegister(mfrc522.ComIEnReg, regVal);
- bNewInt = false; //interrupt flag
- /*Activate the interrupt*/
- attachInterrupt(digitalPinToInterrupt(IRQ_PIN), readCard, FALLING);
- /* Clear distance sensor */
- digitalWrite(TRIGGER_PIN, LOW);
- delayMicroseconds(500);
- }
- /**
- * Main loop.
- */
- void loop() {
- delay(MEASURE_DELAY);
- long distance = measure();
- digitalWrite(BUZZ_PIN, HIGH);
- //new read interrupt
- if (bNewInt)
- {
- distStr = String(distance);
- u8x8.begin();
- u8x8.setPowerSave(0);
- u8x8.setFont(u8x8_font_amstrad_cpc_extended_r);
- u8x8.setCursor(0, 3);
- u8x8.print(distance);
- u8x8.print("mm");
- digitalWrite(LED, HIGH);
- dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
- Serial.println();
- digitalWrite(BUZZ_PIN, LOW);
- delay(100);
- digitalWrite(BUZZ_PIN, HIGH);
- delay(3000);
- digitalWrite(LED, LOW);
- clearInt(mfrc522);
- mfrc522.PICC_HaltA();
- bNewInt = false;
- u8x8.clearDisplay();
- }
- // The receiving block needs regular retriggering (tell the tag it should transmit??)
- // (mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg,mfrc522.PICC_CMD_REQA);)
- activateRec(mfrc522);
- delay(100);
- }
- /**
- * Returns mean value of samples
- */
- long measure(){
- long value = 0;
- long measureSum = 0;
- for (int i = 0; i < MEASURE_SAMPLES; i++)
- {
- delay(MEASURE_SAMPLE_DELAY);
- measureSum += singleMeasurement();
- }
- value = (measureSum / MEASURE_SAMPLES)-offset;
- if(value < 0){
- return 0;
- }
- else{
- return value;
- }
- }
- /**
- * Does a single measuremet
- */
- long singleMeasurement()
- {
- long duration = 0;
- // Measure: Put up Trigger...
- digitalWrite(TRIGGER_PIN, HIGH);
- // ... wait for 11 µs ...
- delayMicroseconds(11);
- // ... put the trigger down ...
- digitalWrite(TRIGGER_PIN, LOW);
- // ... and wait for the echo ...
- duration = pulseIn(ECHO_PIN, HIGH);
- return (long) (((float) duration / USONIC_DIV) * 10.0);
- }
- /**
- * Helper routine to dump a byte array as hex values to Serial.
- */
- void dump_byte_array(byte *buffer, byte bufferSize) {
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- Serial.print(buffer[i], HEX);
- }
- }
- /**
- * MFRC522 interrupt serving routine
- */
- void readCard() {
- bNewInt = true;
- }
- /*
- * The function sending to the MFRC522 the needed commands to activate the reception
- */
- void activateRec(MFRC522 mfrc522) {
- mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
- mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
- mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);
- }
- /*
- * The function to clear the pending interrupt bits after interrupt serving routine
- */
- void clearInt(MFRC522 mfrc522) {
- mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement