Remove turn signal & sonar, add RFID auth
Define AUTH_UID and AUTH_UID_SIZE constants. Introduce motor control pins IN_1‑IN_4 and set them as outputs. Comment out turn‑signal and sonar pin definitions and related code. Refactor Display to remove updateDirection functionality. Extend RFIDReader with reset() and matches() methods; use them to toggle carEnabled based on RFID authentication. Update the main loop to operate only when the RFID tag matches the authorized UID.
This commit is contained in:
@@ -4,13 +4,16 @@
|
||||
const int SS_PIN = 10;
|
||||
const int RST_PIN = 7;
|
||||
|
||||
// Kierunkowskazy
|
||||
const int TS_RIGHT = 9;
|
||||
const int TS_LEFT = 8;
|
||||
const byte AUTH_UID[4] = {145, 136, 97, 102};
|
||||
const byte AUTH_UID_SIZE = sizeof(AUTH_UID);
|
||||
|
||||
// Odległościomierz
|
||||
const int TRIG_PIN = 6;
|
||||
const int ECHO_PIN = 5;
|
||||
// // Kierunkowskazy
|
||||
// const int TS_RIGHT = 9;
|
||||
// const int TS_LEFT = 8;
|
||||
|
||||
// // Odległościomierz
|
||||
// const int TRIG_PIN = 6;
|
||||
// const int ECHO_PIN = 5;
|
||||
|
||||
const long SONAR_MAX_DIST_CM = 400;
|
||||
|
||||
@@ -25,7 +28,13 @@ const int GEAR_THRESH_HIGH = 682;
|
||||
const int LCD_COLS = 16;
|
||||
const int LCD_ROWS = 2;
|
||||
|
||||
// Silnik
|
||||
// Skrzynia biegów
|
||||
const uint8_t CLUTCH_PIN = A7;
|
||||
const uint8_t BRAKE_PIN = A2;
|
||||
const uint8_t THROTTLE_PIN = A3;
|
||||
|
||||
// Silniki
|
||||
const int IN_1 = 9;
|
||||
const int IN_2 = 8;
|
||||
const int IN_3 = 4;
|
||||
const int IN_4 = 3;
|
||||
|
||||
+16
-16
@@ -45,21 +45,21 @@ void Display::updateGear(char gearChar) {
|
||||
_lcd.print(' ');
|
||||
}
|
||||
|
||||
void Display::updateDirection(bool leftOn, bool rightOn) {
|
||||
if (leftOn && rightOn) {
|
||||
_lcd.setCursor(12, 0);
|
||||
_lcd.write((uint8_t)CHAR_LEFT);
|
||||
_lcd.print(' ');
|
||||
_lcd.write((uint8_t)CHAR_RIGHT);
|
||||
return;
|
||||
}
|
||||
// void Display::updateDirection(bool leftOn, bool rightOn) {
|
||||
// if (leftOn && rightOn) {
|
||||
// _lcd.setCursor(12, 0);
|
||||
// _lcd.write((uint8_t)CHAR_LEFT);
|
||||
// _lcd.print(' ');
|
||||
// _lcd.write((uint8_t)CHAR_RIGHT);
|
||||
// return;
|
||||
// }
|
||||
|
||||
unsigned long now = millis();
|
||||
bool leftBlink = leftOn && ((now / 1000UL) % 2 == 0);
|
||||
bool rightBlink = rightOn && ((now / 1000UL) % 2 == 0);
|
||||
// unsigned long now = millis();
|
||||
// bool leftBlink = leftOn && ((now / 1000UL) % 2 == 0);
|
||||
// bool rightBlink = rightOn && ((now / 1000UL) % 2 == 0);
|
||||
|
||||
_lcd.setCursor(12, 0);
|
||||
_lcd.write((uint8_t)(leftBlink ? CHAR_LEFT_BLINK : CHAR_LEFT));
|
||||
_lcd.print(' ');
|
||||
_lcd.write((uint8_t)(rightBlink ? CHAR_RIGHT_BLINK : CHAR_RIGHT));
|
||||
}
|
||||
// _lcd.setCursor(12, 0);
|
||||
// _lcd.write((uint8_t)(leftBlink ? CHAR_LEFT_BLINK : CHAR_LEFT));
|
||||
// _lcd.print(' ');
|
||||
// _lcd.write((uint8_t)(rightBlink ? CHAR_RIGHT_BLINK : CHAR_RIGHT));
|
||||
// }
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
|
||||
void updateGear(char gearChar);
|
||||
|
||||
void updateDirection(bool leftOn, bool rightOn);
|
||||
// void updateDirection(bool leftOn, bool rightOn);
|
||||
|
||||
private:
|
||||
rgb_lcd _lcd;
|
||||
|
||||
+26
-14
@@ -3,26 +3,28 @@
|
||||
#include "Config.h"
|
||||
#include "Display.h"
|
||||
#include "Gear.h"
|
||||
#include "Sonar.h"
|
||||
// #include "Sonar.h"
|
||||
#include "RFID.h"
|
||||
#include "Engine.h"
|
||||
|
||||
Display display;
|
||||
GearSelector gear(GEAR_X_PIN, GEAR_Y_PIN, GEAR_THRESH_LOW, GEAR_THRESH_HIGH);
|
||||
Sonar sonar(TRIG_PIN, ECHO_PIN, SONAR_MAX_DIST_CM);
|
||||
// Sonar sonar(TRIG_PIN, ECHO_PIN, SONAR_MAX_DIST_CM);
|
||||
RFIDReader rfid(SS_PIN, RST_PIN);
|
||||
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
|
||||
|
||||
bool leftBlink = false;
|
||||
bool rightBlink = false;
|
||||
|
||||
unsigned long prevUpdate = 0;
|
||||
|
||||
bool carEnabled = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(TS_LEFT, INPUT);
|
||||
pinMode(TS_RIGHT, INPUT);
|
||||
// pinMode(TS_LEFT, INPUT);
|
||||
// pinMode(TS_RIGHT, INPUT);
|
||||
|
||||
pinMode(IN_1, OUTPUT);
|
||||
pinMode(IN_2, OUTPUT);
|
||||
|
||||
rfid.begin();
|
||||
display.begin(LCD_COLS, LCD_ROWS);
|
||||
@@ -30,12 +32,25 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
bool check = rfid.check();
|
||||
bool matches = rfid.matches(AUTH_UID, AUTH_UID_SIZE);
|
||||
|
||||
bool rfidOk = check && matches;
|
||||
|
||||
if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) {
|
||||
delay(3000);
|
||||
carEnabled = !carEnabled;
|
||||
};
|
||||
|
||||
if (carEnabled) {
|
||||
Serial.println("Działa");
|
||||
|
||||
if (gear.update())
|
||||
display.updateGear(gear.getGearChar());
|
||||
|
||||
display.updateDirection(
|
||||
digitalRead(TS_LEFT),
|
||||
digitalRead(TS_RIGHT));
|
||||
// display.updateDirection(
|
||||
// digitalRead(TS_LEFT),
|
||||
// digitalRead(TS_RIGHT));
|
||||
|
||||
unsigned long now = millis();
|
||||
float dt = (now - prevUpdate) / 1000.0f;
|
||||
@@ -46,10 +61,7 @@ void loop() {
|
||||
display.updateSpeed(engine.getSpeedKmh());
|
||||
}
|
||||
|
||||
long dist = sonar.measure();
|
||||
// long dist = sonar.measure();
|
||||
// Serial.println(dist);
|
||||
|
||||
if (rfid.check()) {
|
||||
rfid.printUID();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,18 +11,30 @@ void RFIDReader::begin() {
|
||||
bool RFIDReader::check() {
|
||||
if (!_rfid.PICC_IsNewCardPresent()) return false;
|
||||
if (!_rfid.PICC_ReadCardSerial()) return false;
|
||||
// _rfid.PICC_HaltA(); // stop the tag
|
||||
// _rfid.PCD_StopCrypto1(); // clear RC522 crypto
|
||||
return true;
|
||||
}
|
||||
|
||||
void RFIDReader::reset() {
|
||||
_rfid.PICC_HaltA();
|
||||
_rfid.PCD_StopCrypto1();
|
||||
}
|
||||
|
||||
bool RFIDReader::matches(const byte* expectedUid, byte expectedSize) const {
|
||||
if (_rfid.uid.size != expectedSize) return false;
|
||||
for (byte i = 0; i < expectedSize; ++i) {
|
||||
if (_rfid.uid.uidByte[i] != expectedUid[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void RFIDReader::printUID() const {
|
||||
Serial.print(F("RFID Tag UID:"));
|
||||
for (byte i = 0; i < _rfid.uid.size; i++) {
|
||||
Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
|
||||
Serial.print(_rfid.uid.uidByte[i], HEX);
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println(F("RFID Tag UID:"));
|
||||
// for (byte i = 0; i < _rfid.uid.size; i++) {
|
||||
// Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
|
||||
// Serial.print(_rfid.uid.uidByte[i], HEX);
|
||||
// }
|
||||
}
|
||||
|
||||
const byte* RFIDReader::getUID() const {
|
||||
|
||||
Reference in New Issue
Block a user