init
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// RFID
|
||||||
|
const int SS_PIN = 10;
|
||||||
|
const int RST_PIN = 7;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// Skrzynia biegów
|
||||||
|
const uint8_t GEAR_X_PIN = A0;
|
||||||
|
const uint8_t GEAR_Y_PIN = A1;
|
||||||
|
|
||||||
|
const int GEAR_THRESH_LOW = 341;
|
||||||
|
const int GEAR_THRESH_HIGH = 682;
|
||||||
|
|
||||||
|
// LCD
|
||||||
|
const int LCD_COLS = 16;
|
||||||
|
const int LCD_ROWS = 2;
|
||||||
+65
@@ -0,0 +1,65 @@
|
|||||||
|
#include "Display.h"
|
||||||
|
|
||||||
|
void Display::begin(int cols, int rows) {
|
||||||
|
_lcd.begin(cols, rows);
|
||||||
|
|
||||||
|
_lcd.createChar(CHAR_LEFT, _leftChar);
|
||||||
|
_lcd.createChar(CHAR_RIGHT, _rightChar);
|
||||||
|
_lcd.createChar(CHAR_LEFT_BLINK, _leftCharBlink);
|
||||||
|
_lcd.createChar(CHAR_RIGHT_BLINK, _rightCharBlink);
|
||||||
|
|
||||||
|
_lcd.setCursor(0, 0);
|
||||||
|
_lcd.print("km/h: 000 | ");
|
||||||
|
_lcd.write((uint8_t)CHAR_LEFT);
|
||||||
|
_lcd.print(" ");
|
||||||
|
_lcd.write((uint8_t)CHAR_RIGHT);
|
||||||
|
|
||||||
|
_lcd.setCursor(0, 1);
|
||||||
|
_lcd.print("rpm: 0000 | G:0");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Display::updateSpeed(int kmh) {
|
||||||
|
if (kmh < 0) kmh = 0;
|
||||||
|
if (kmh > 999) kmh = 999;
|
||||||
|
|
||||||
|
_lcd.setCursor(6, 0);
|
||||||
|
if (kmh < 100) _lcd.print('0');
|
||||||
|
if (kmh < 10) _lcd.print('0');
|
||||||
|
_lcd.print(kmh);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Display::updateRPM(int rpm) {
|
||||||
|
if (rpm < 0) rpm = 0;
|
||||||
|
if (rpm > 9999) rpm = 9999;
|
||||||
|
|
||||||
|
_lcd.setCursor(5, 1);
|
||||||
|
if (rpm < 1000) _lcd.print('0');
|
||||||
|
if (rpm < 100) _lcd.print('0');
|
||||||
|
if (rpm < 10) _lcd.print('0');
|
||||||
|
_lcd.print(rpm);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Display::updateGear(char gearChar) {
|
||||||
|
_lcd.setCursor(14, 1);
|
||||||
|
_lcd.print(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <rgb_lcd.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
enum LcdChar : uint8_t {
|
||||||
|
CHAR_LEFT = 0,
|
||||||
|
CHAR_RIGHT = 1,
|
||||||
|
CHAR_LEFT_BLINK = 2,
|
||||||
|
CHAR_RIGHT_BLINK = 3
|
||||||
|
};
|
||||||
|
|
||||||
|
class Display {
|
||||||
|
public:
|
||||||
|
void begin(int cols, int rows);
|
||||||
|
|
||||||
|
void updateSpeed(int kmh);
|
||||||
|
|
||||||
|
void updateRPM(int rpm);
|
||||||
|
|
||||||
|
void updateGear(char gearChar);
|
||||||
|
|
||||||
|
void updateDirection(bool leftOn, bool rightOn);
|
||||||
|
|
||||||
|
private:
|
||||||
|
rgb_lcd _lcd;
|
||||||
|
|
||||||
|
byte _leftChar[8] = { 0x00, 0x04, 0x08, 0x1F, 0x1F, 0x08, 0x04, 0x00 };
|
||||||
|
byte _leftCharBlink[8] = { 0x1F, 0x1B, 0x17, 0x00, 0x00, 0x17, 0x1B, 0x1F };
|
||||||
|
byte _rightChar[8] = { 0x00, 0x04, 0x02, 0x1F, 0x1F, 0x02, 0x04, 0x00 };
|
||||||
|
byte _rightCharBlink[8] = { 0x1F, 0x1B, 0x1D, 0x00, 0x00, 0x1D, 0x1B, 0x1F };
|
||||||
|
};
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include "Gear.h"
|
||||||
|
|
||||||
|
static const int8_t GEAR_MAP[3][3] = {
|
||||||
|
{ 5, 3, 1 },
|
||||||
|
{ -2, 0, -2 },
|
||||||
|
{ -1, 4, 2 },
|
||||||
|
};
|
||||||
|
|
||||||
|
GearSelector::GearSelector(uint8_t xPin, uint8_t yPin, int threshLow, int threshHigh)
|
||||||
|
: _xPin(xPin), _yPin(yPin),
|
||||||
|
_threshLow(threshLow), _threshHigh(threshHigh),
|
||||||
|
_currentGear(0) {}
|
||||||
|
|
||||||
|
bool GearSelector::update() {
|
||||||
|
AxisPos x = _quantize(analogRead(_xPin));
|
||||||
|
AxisPos y = _quantize(analogRead(_yPin));
|
||||||
|
|
||||||
|
int newGear = _mapToGear(x, y);
|
||||||
|
|
||||||
|
if (newGear == -2) return false;
|
||||||
|
|
||||||
|
if (newGear != _currentGear) {
|
||||||
|
delay(100);
|
||||||
|
_currentGear = newGear;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GearSelector::getGear() const {
|
||||||
|
return _currentGear;
|
||||||
|
}
|
||||||
|
|
||||||
|
char GearSelector::getGearChar() const {
|
||||||
|
if (_currentGear == -1) return 'R';
|
||||||
|
return '0' + _currentGear;
|
||||||
|
}
|
||||||
|
|
||||||
|
AxisPos GearSelector::_quantize(int val) const {
|
||||||
|
if (val < _threshLow) return POS_LOW;
|
||||||
|
if (val > _threshHigh) return POS_HIGH;
|
||||||
|
return POS_CENTRE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GearSelector::_mapToGear(AxisPos x, AxisPos y) const {
|
||||||
|
int xi = (int)x + 1;
|
||||||
|
int yi = (int)y + 1;
|
||||||
|
return GEAR_MAP[xi][yi];
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
enum AxisPos : int8_t {
|
||||||
|
POS_LOW = -1,
|
||||||
|
POS_CENTRE = 0,
|
||||||
|
POS_HIGH = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
class GearSelector {
|
||||||
|
public:
|
||||||
|
GearSelector(uint8_t xPin, uint8_t yPin, int threshLow, int threshHigh);
|
||||||
|
|
||||||
|
bool update();
|
||||||
|
|
||||||
|
int getGear() const;
|
||||||
|
|
||||||
|
char getGearChar() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t _xPin, _yPin;
|
||||||
|
int _threshLow, _threshHigh;
|
||||||
|
int _currentGear;
|
||||||
|
|
||||||
|
AxisPos _quantize(int val) const;
|
||||||
|
|
||||||
|
int _mapToGear(AxisPos x, AxisPos y) const;
|
||||||
|
};
|
||||||
+42
@@ -0,0 +1,42 @@
|
|||||||
|
#include <Wire.h>
|
||||||
|
|
||||||
|
#include "Config.h"
|
||||||
|
#include "Display.h"
|
||||||
|
#include "Gear.h"
|
||||||
|
#include "Sonar.h"
|
||||||
|
#include "RFID.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);
|
||||||
|
RFIDReader rfid(SS_PIN, RST_PIN);
|
||||||
|
|
||||||
|
bool leftBlink = false;
|
||||||
|
bool rightBlink = false;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
|
pinMode(TS_LEFT, INPUT);
|
||||||
|
pinMode(TS_RIGHT, INPUT);
|
||||||
|
|
||||||
|
rfid.begin();
|
||||||
|
display.begin(LCD_COLS, LCD_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (gear.update()) {
|
||||||
|
display.updateGear(gear.getGearChar());
|
||||||
|
}
|
||||||
|
|
||||||
|
leftBlink = digitalRead(TS_LEFT);
|
||||||
|
rightBlink = digitalRead(TS_RIGHT);
|
||||||
|
display.updateDirection(leftBlink, rightBlink);
|
||||||
|
|
||||||
|
long dist = sonar.measure();
|
||||||
|
// Serial.println(dist);
|
||||||
|
|
||||||
|
if (rfid.check()) {
|
||||||
|
rfid.printUID();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include "RFID.h"
|
||||||
|
|
||||||
|
RFIDReader::RFIDReader(int ssPin, int rstPin)
|
||||||
|
: _rfid(ssPin, rstPin) {}
|
||||||
|
|
||||||
|
void RFIDReader::begin() {
|
||||||
|
SPI.begin();
|
||||||
|
_rfid.PCD_Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RFIDReader::check() {
|
||||||
|
if (!_rfid.PICC_IsNewCardPresent()) return false;
|
||||||
|
if (!_rfid.PICC_ReadCardSerial()) return false;
|
||||||
|
|
||||||
|
_rfid.PICC_HaltA();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
const byte* RFIDReader::getUID() const {
|
||||||
|
return _rfid.uid.uidByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte RFIDReader::getUIDSize() const {
|
||||||
|
return _rfid.uid.size;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SPI.h>
|
||||||
|
#include <MFRC522.h>
|
||||||
|
|
||||||
|
class RFIDReader {
|
||||||
|
public:
|
||||||
|
RFIDReader(int ssPin, int rstPin);
|
||||||
|
|
||||||
|
void begin();
|
||||||
|
|
||||||
|
bool check();
|
||||||
|
|
||||||
|
void printUID() const;
|
||||||
|
|
||||||
|
const byte* getUID() const;
|
||||||
|
byte getUIDSize() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
MFRC522 _rfid;
|
||||||
|
MFRC522::MIFARE_Key _key;
|
||||||
|
};
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include "Sonar.h"
|
||||||
|
|
||||||
|
Sonar::Sonar(int trigPin, int echoPin, long maxDistCm)
|
||||||
|
: _trigPin(trigPin), _echoPin(echoPin),
|
||||||
|
_maxDistCm(maxDistCm), _lastDistance(0) {
|
||||||
|
pinMode(_trigPin, OUTPUT);
|
||||||
|
pinMode(_echoPin, INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
long Sonar::measure() {
|
||||||
|
digitalWrite(_trigPin, LOW);
|
||||||
|
delayMicroseconds(2);
|
||||||
|
digitalWrite(_trigPin, HIGH);
|
||||||
|
delayMicroseconds(10);
|
||||||
|
digitalWrite(_trigPin, LOW);
|
||||||
|
|
||||||
|
long duration = pulseIn(_echoPin, HIGH);
|
||||||
|
_lastDistance = duration / 58;
|
||||||
|
|
||||||
|
if (_lastDistance > _maxDistCm) {
|
||||||
|
_lastDistance = _maxDistCm;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _lastDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
long Sonar::getDistance() const {
|
||||||
|
return _lastDistance;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user