Dana Mah
Created August 25, 2018 © GPL3+

Pet Diet Event Recorder

This project is to record when you pet goes to the food dish to eat.

IntermediateFull instructions provided1 hour43
Pet Diet Event Recorder

Things used in this project

Hardware components

MAX32630FTHR
Maxim Integrated MAX32630FTHR
This was the closest part number, it was actually the MAX32620FTHR.
×1
RFID reader (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Main code

Arduino
this code will read the rfid tag, allow you to send in a start date (unix seconds). Then it will send out to the serial monitor the tag number along with the date.
#include <SPI.h>
#include <MFRC522.h>
#include "TimeLib.h"

#define RST_PIN         P3_1          // Configurable, see typical pin layout above
#define SS_PIN          P3_0         // Configurable, see typical pin layout above

#define TIME_HEADER  "T"
#define TIME_FORMAT  "F"

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
int format = 1;   // 0 = unix seconds. 1 = hh:mm:ss YYYY/MM/DD

void setup() {
  Serial.begin(9600);		// Initialize serial communications with the PC
  while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();			// Init SPI bus
  mfrc522.PCD_Init();		// Init MFRC522
  mfrc522.PCD_WriteRegister(mfrc522.RFCfgReg, (0x07 << 4));
  mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

  setTime(1533130394);
}

void loop() {
  if (Serial.available()) {
    processSyncMessage();
  }

  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  for (byte i = 0; i < mfrc522.uid.size; i++) {
    if (mfrc522.uid.uidByte[i] < 0x10)
      Serial.print(F("0"));
    else
      Serial.print(F(""));
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.print(" ");

  digitalClockDisplay();
  delay(5000);

  //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}

void processSyncMessage() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1533130394;
  
  if (Serial.find(TIME_HEADER)) {
    pctime = Serial.parseInt();
    if ( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
      setTime(pctime); // Sync Arduino clock to the time received on the serial port
    }
  }
  if (Serial.find(TIME_FORMAT)) {
    format = Serial.parseInt();
    Serial.println(format);
  }
}

void digitalClockDisplay() {
  // digital clock display of the time
  if (format == 1) {
    Serial.print(hour());
    printDigits(minute());
    printDigits(second());
    Serial.print(" ");
    Serial.print(year());
    Serial.print("/");
    if (month() < 10)
      Serial.print("0");
    Serial.print(month());
    Serial.print("/");
    if (day() < 10)
      Serial.print("0");
    Serial.print(day());
    Serial.println();
  } else {
    Serial.println(now());
  }
}

void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Credits

Dana Mah

Dana Mah

12 projects • 27 followers
I'm a hobbyist interested in microcontroller solutions to simple problems.

Comments