mirror of
https://github.com/zzzzDev4/IAS-Better-Tea.git
synced 2025-04-21 07:31:20 +02:00
Added demo of RFID scanner
This commit is contained in:
parent
1fe3ec02a3
commit
54c4a57bb6
2 changed files with 99 additions and 29 deletions
|
@ -14,3 +14,4 @@ board = d1
|
|||
framework = arduino
|
||||
lib_deps =
|
||||
adafruit/Adafruit SSD1306@^2.5.9
|
||||
miguelbalboa/MFRC522@^1.4.11
|
||||
|
|
127
src/main.cpp
127
src/main.cpp
|
@ -3,44 +3,73 @@
|
|||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <MFRC522.h>
|
||||
|
||||
#include "TeaTimer.hpp"
|
||||
#include "SmartServo.hpp"
|
||||
#include "ContinuousServo.hpp"
|
||||
|
||||
#define WIRE Wire
|
||||
|
||||
#define DISPLAY_PIN 4 // D2 (SDA)
|
||||
#define SERVO_PIN 2 // D4
|
||||
#define TOUCH_BTN_PIN 0 // D3
|
||||
#define DISPLAY_PIN 4 // D2 (SDA)
|
||||
#define SERVO_PIN 0 // D3
|
||||
#define TOUCH_BTN_PIN 16 // D0
|
||||
|
||||
// RFID
|
||||
#define RST_PIN 2 // D4
|
||||
uint8_t scanDelay = 5; // 5 second delay between reads
|
||||
#define SS_PIN 15 // D8
|
||||
MFRC522 mfrc522(SS_PIN, RST_PIN); // create RFID instance
|
||||
MFRC522::MIFARE_Key key;
|
||||
|
||||
auto display = Adafruit_SSD1306(128, 32, &WIRE);
|
||||
|
||||
ContinuousServo contServo(SERVO_PIN);
|
||||
TeaTimer teaTimer;
|
||||
|
||||
void serial_dump_byte_array(byte *buffer, byte bufferSize);
|
||||
void printMsg(const String &s);
|
||||
void printMsg(unsigned long l);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Serial.begin(9600);
|
||||
Serial.begin(9600);
|
||||
|
||||
teaTimer.init(&contServo);
|
||||
Serial.println("Initializing SPI...");
|
||||
SPI.begin();
|
||||
|
||||
Serial.println("Initializing RFID...");
|
||||
mfrc522.PCD_Init();
|
||||
Serial.print(F("Ver: 0x"));
|
||||
byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||
Serial.println(readReg, HEX);
|
||||
|
||||
Serial.println("Initializing OLED...");
|
||||
display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32
|
||||
printMsg("Tea is Great!");
|
||||
delay(3000);
|
||||
|
||||
// Prepare the key (used both as key A and key B)
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
key.keyByte[i] = 0xFF;
|
||||
}
|
||||
|
||||
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate basic recognition."));
|
||||
Serial.print(F("Using key (for A and B):"));
|
||||
serial_dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
|
||||
Serial.println();
|
||||
|
||||
// teaTimer.init(&contServo);
|
||||
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(TOUCH_BTN_PIN, INPUT);
|
||||
|
||||
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
|
||||
// Show image buffer on the display hardware.
|
||||
// Since the buffer is intialized with an Adafruit splashscreen
|
||||
// internally, this will display the splashscreen.
|
||||
display.display();
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void printMsg(const String &s)
|
||||
{
|
||||
display.clearDisplay();
|
||||
display.setTextColor(WHITE);
|
||||
display.setTextSize(2);
|
||||
display.setTextSize(1);
|
||||
display.setCursor(0, 10);
|
||||
display.println(s);
|
||||
display.display();
|
||||
|
@ -49,7 +78,7 @@ void printMsg(unsigned long l)
|
|||
{
|
||||
display.clearDisplay();
|
||||
display.setTextColor(WHITE);
|
||||
display.setTextSize(2);
|
||||
display.setTextSize(1);
|
||||
display.setCursor(0, 10);
|
||||
display.println(l);
|
||||
display.display();
|
||||
|
@ -61,26 +90,66 @@ bool isBtnPressed()
|
|||
return false;
|
||||
return digitalRead(TOUCH_BTN_PIN) == HIGH;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
teaTimer.tick();
|
||||
// Look for new cards
|
||||
if (!mfrc522.PICC_IsNewCardPresent())
|
||||
return;
|
||||
|
||||
if (isBtnPressed() && !teaTimer.isSteeping())
|
||||
{
|
||||
teaTimer.beginSteeping(5000);
|
||||
}
|
||||
if (teaTimer.isSteeping())
|
||||
{
|
||||
auto r = teaTimer.remainingSteepTimeSeconds();
|
||||
printMsg(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
printMsg("Default");
|
||||
}
|
||||
// Select one of the cards
|
||||
if (!mfrc522.PICC_ReadCardSerial())
|
||||
return;
|
||||
|
||||
// Show some details of the PICC (that is: the tag/card)
|
||||
// UID is a unique four hex pairs, e.g. E6 67 2A 12
|
||||
Serial.print(F("Card UID:"));
|
||||
serial_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
|
||||
display.println("Card UID: ");
|
||||
display.println();
|
||||
// PICC type, e.g. "MIFARE 1K0"
|
||||
Serial.print(F("PICC type: "));
|
||||
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
|
||||
Serial.println(mfrc522.PICC_GetTypeName(piccType));
|
||||
|
||||
// Halt PICC
|
||||
mfrc522.PICC_HaltA();
|
||||
|
||||
Serial.print("Delay scanning...");
|
||||
delay(scanDelay * 1000); // Turn seconds into milliseconds
|
||||
Serial.println(" ...Ready to scan again!");
|
||||
|
||||
/*
|
||||
teaTimer.tick();
|
||||
|
||||
if (isBtnPressed() && !teaTimer.isSteeping())
|
||||
{
|
||||
teaTimer.beginSteeping(5000);
|
||||
}
|
||||
if (teaTimer.isSteeping())
|
||||
{
|
||||
auto r = teaTimer.remainingSteepTimeSeconds();
|
||||
printMsg(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
printMsg("Default");
|
||||
}*/
|
||||
}
|
||||
|
||||
void toggle_led()
|
||||
{
|
||||
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper routine to dump a byte array as hex values to Serial.
|
||||
*/
|
||||
void serial_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);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue