Compare commits
12 Commits
b957a0701b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f849e3455 | |||
| a2bb6c557d | |||
| 05af6951b0 | |||
| 7585dfe3e9 | |||
| 0a4a0b862c | |||
| 6c0a1fc15b | |||
| c053a296a5 | |||
| 23d5003788 | |||
| 17f888a11c | |||
| a80a5bd9a7 | |||
| 899212d7b9 | |||
| febf1215db |
@@ -4,13 +4,11 @@
|
||||
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;
|
||||
// // Odległościomierz
|
||||
const int TRIG_AND_ECHO_PIN = 3;
|
||||
|
||||
const long SONAR_MAX_DIST_CM = 400;
|
||||
|
||||
@@ -25,7 +23,18 @@ 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
|
||||
// Prawy silnik
|
||||
const int IN_1 = 9;
|
||||
const int IN_2 = 8;
|
||||
const int ENA_1 = 5;
|
||||
|
||||
// Lewy silnik
|
||||
const int IN_3 = 4;
|
||||
const int IN_4 = 2;
|
||||
const int ENA_2 = 6;
|
||||
|
||||
+10
-28
@@ -2,22 +2,23 @@
|
||||
|
||||
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.noDisplay();
|
||||
|
||||
_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::startOrEnd(bool enable) {
|
||||
if (enable) {
|
||||
delay(3000);
|
||||
_lcd.display();
|
||||
} else {
|
||||
_lcd.noDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
void Display::updateSpeed(int kmh) {
|
||||
if (kmh < 0) kmh = 0;
|
||||
if (kmh > 999) kmh = 999;
|
||||
@@ -44,22 +45,3 @@ void Display::updateGear(char gearChar) {
|
||||
_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));
|
||||
}
|
||||
|
||||
@@ -2,30 +2,18 @@
|
||||
#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 startOrEnd(bool enable);
|
||||
|
||||
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 };
|
||||
};
|
||||
|
||||
+45
-74
@@ -1,4 +1,5 @@
|
||||
#include "Engine.h"
|
||||
#include "Config.h"
|
||||
using namespace EngineConsts;
|
||||
|
||||
Engine::Engine(uint8_t throttlePin,
|
||||
@@ -6,84 +7,10 @@ Engine::Engine(uint8_t throttlePin,
|
||||
uint8_t brakePin)
|
||||
: _throttlePin(throttlePin), _clutchPin(clutchPin), _brakePin(brakePin) {}
|
||||
|
||||
void Engine::begin() {
|
||||
}
|
||||
|
||||
float Engine::readNormalized(uint8_t pin) const {
|
||||
return analogRead(pin) / 1023.0f;
|
||||
}
|
||||
|
||||
void Engine::update(float dt, uint8_t gearIdx) {
|
||||
Serial.println("----------------");
|
||||
|
||||
const float throttle = readNormalized(_throttlePin);
|
||||
Serial.print("Throttle: ");
|
||||
Serial.println(throttle);
|
||||
|
||||
const float clutch = readNormalized(_clutchPin);
|
||||
Serial.print("Clutch: ");
|
||||
Serial.println(clutch);
|
||||
|
||||
const float brake = readNormalized(_brakePin);
|
||||
Serial.print("Brake: ");
|
||||
Serial.println(brake);
|
||||
|
||||
const float targetRPM = RPM_IDLE + throttle * (RPM_MAX - RPM_IDLE);
|
||||
Serial.print("targetRPM: ");
|
||||
Serial.println(targetRPM);
|
||||
|
||||
const float wheelRPM = (_speedMs / (2.0f * PI * WHEEL_RADIUS)) * 60.0f;
|
||||
Serial.print("wheelRPM: ");
|
||||
Serial.println(wheelRPM);
|
||||
|
||||
const float gearRatio = GEAR_RATIO[gearIdx];
|
||||
Serial.print("gearRatio: ");
|
||||
Serial.println(gearRatio);
|
||||
|
||||
const float drivenRPM = wheelRPM * fabs(gearRatio) * FINAL_DRIVE;
|
||||
Serial.print("drivenRPM: ");
|
||||
Serial.println(drivenRPM);
|
||||
|
||||
_engineRPM = clutch * drivenRPM + (1.0f - clutch) * targetRPM;
|
||||
Serial.print("_engineRPM: ");
|
||||
Serial.println(_engineRPM);
|
||||
|
||||
_engineRPM = constrain(_engineRPM, RPM_IDLE, 7500.0f);
|
||||
Serial.print("_engineRPM (constrain): ");
|
||||
Serial.println(_engineRPM);
|
||||
|
||||
const float engineForce = (_engineRPM / RPM_MAX) * (1.0f - clutch) * ENGINE_FORCE_FACTOR;
|
||||
Serial.print("engineForce: ");
|
||||
Serial.println(engineForce);
|
||||
|
||||
const float brakeForce = brake * BRAKE_FORCE_MAX;
|
||||
Serial.print("brakeForce: ");
|
||||
Serial.println(brakeForce);
|
||||
|
||||
const float dragForce = 0.5f * DRAG_COEFF * FRONTAL_AREA * (_speedMs * _speedMs);
|
||||
Serial.print("dragForce: ");
|
||||
Serial.println(dragForce);
|
||||
|
||||
const int direction = (gearIdx == 5) ? -1 : 1;
|
||||
Serial.print("direction: ");
|
||||
Serial.println(direction);
|
||||
|
||||
const float netForce = direction * engineForce - brakeForce - dragForce;
|
||||
Serial.print("netForce: ");
|
||||
Serial.println(netForce);
|
||||
|
||||
const float acceleration = netForce / CAR_MASS;
|
||||
Serial.print("acceleration: ");
|
||||
Serial.println(acceleration);
|
||||
|
||||
_speedMs += acceleration * dt;
|
||||
if (_speedMs < 0.0f) _speedMs = 0.0f;
|
||||
Serial.print("_speedMs: ");
|
||||
Serial.println(_speedMs);
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
int Engine::getRPM() const {
|
||||
return static_cast<int>(_engineRPM + 0.5f);
|
||||
}
|
||||
@@ -91,3 +18,47 @@ int Engine::getRPM() const {
|
||||
int Engine::getSpeedKmh() const {
|
||||
return static_cast<int>((_speedMs * 3.6f) + 0.5f);
|
||||
}
|
||||
|
||||
uint8_t Engine::rpmToPwm(float rpm) {
|
||||
rpm = constrain(rpm, 0.0f, RPM_MAX);
|
||||
return static_cast<uint8_t>(roundf(rpm / RPM_MAX * 255.0f));
|
||||
}
|
||||
|
||||
void Engine::setMotor(uint8_t inA, uint8_t inB, uint8_t ena,
|
||||
float rpm, bool reverse) {
|
||||
digitalWrite(inA, reverse ? LOW : HIGH);
|
||||
digitalWrite(inB, reverse ? HIGH : LOW);
|
||||
|
||||
analogWrite(ena, rpmToPwm(rpm));
|
||||
}
|
||||
|
||||
void Engine::update(float dt, uint8_t gearIdx) {
|
||||
const float throttle = readNormalized(_throttlePin);
|
||||
const float clutch = readNormalized(_clutchPin);
|
||||
const float brake = readNormalized(_brakePin);
|
||||
|
||||
const float targetRPM = RPM_IDLE + throttle * (RPM_MAX - RPM_IDLE);
|
||||
|
||||
const float wheelRPM = (_speedMs / (2.0f * PI * WHEEL_RADIUS)) * 60.0f;
|
||||
|
||||
const float gearRatio = GEAR_RATIO[gearIdx];
|
||||
const float drivenRPM = wheelRPM * fabs(gearRatio) * FINAL_DRIVE;
|
||||
|
||||
_engineRPM = clutch * drivenRPM + (1.0f - clutch) * targetRPM;
|
||||
_engineRPM = constrain(_engineRPM, RPM_IDLE, 7500.0f);
|
||||
|
||||
const float engineForce = (_engineRPM / RPM_MAX) * (1.0f - clutch) * ENGINE_FORCE_FACTOR;
|
||||
const float brakeForce = brake * BRAKE_FORCE_MAX;
|
||||
const float dragForce = 0.5f * DRAG_COEFF * FRONTAL_AREA * (_speedMs * _speedMs);
|
||||
const int direction = (gearIdx == -1) ? -1 : 1;
|
||||
const float netForce = direction * engineForce - brakeForce - dragForce;
|
||||
const float acceleration = netForce / _carMass;
|
||||
|
||||
_speedMs += acceleration * dt;
|
||||
if (_speedMs < 0.0f) _speedMs = 0.0f;
|
||||
|
||||
setMotor(IN_3, IN_4, ENA_2, targetRPM, false);
|
||||
|
||||
bool reverseWheel = (gearIdx == -1);
|
||||
setMotor(IN_1, IN_2, ENA_1, wheelRPM, reverseWheel);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ constexpr float GEAR_RATIO[6] = {
|
||||
|
||||
constexpr float FINAL_DRIVE = 3.74f; // przekładnia różnicowa
|
||||
constexpr float WHEEL_RADIUS = 0.30f; // m
|
||||
constexpr float CAR_MASS = 1200.0f; // kg
|
||||
constexpr float RPM_IDLE = 800.0f; // obroty jałowe
|
||||
constexpr float RPM_MAX = 7000.0f; // maksymalne obroty
|
||||
|
||||
@@ -29,13 +28,16 @@ public:
|
||||
uint8_t clutchPin,
|
||||
uint8_t brakePin);
|
||||
|
||||
void begin();
|
||||
void setCarMass(float mass) { _carMass = mass; }
|
||||
|
||||
void update(float dt, uint8_t gearIdx);
|
||||
|
||||
int getRPM() const;
|
||||
int getSpeedKmh() const;
|
||||
|
||||
uint8_t rpmToPwm(float rpm);
|
||||
void setMotor(uint8_t inA, uint8_t inB, uint8_t ena, float rpm, bool reverse = false);
|
||||
|
||||
private:
|
||||
const uint8_t _throttlePin;
|
||||
const uint8_t _clutchPin;
|
||||
@@ -43,6 +45,7 @@ private:
|
||||
|
||||
float _engineRPM = EngineConsts::RPM_IDLE;
|
||||
float _speedMs = 0.0f;
|
||||
float _carMass = 1200.0f;
|
||||
|
||||
float readNormalized(uint8_t pin) const;
|
||||
};
|
||||
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
#include <Wire.h>
|
||||
|
||||
#include "Config.h"
|
||||
#include "Display.h"
|
||||
#include "Gear.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);
|
||||
RFIDReader rfid(SS_PIN, RST_PIN);
|
||||
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
|
||||
|
||||
bool leftBlink = false;
|
||||
bool rightBlink = false;
|
||||
|
||||
unsigned long prevUpdate = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(TS_LEFT, INPUT);
|
||||
pinMode(TS_RIGHT, INPUT);
|
||||
|
||||
rfid.begin();
|
||||
display.begin(LCD_COLS, LCD_ROWS);
|
||||
engine.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (gear.update()) {
|
||||
display.updateGear(gear.getGearChar());
|
||||
}
|
||||
|
||||
leftBlink = digitalRead(TS_LEFT);
|
||||
rightBlink = digitalRead(TS_RIGHT);
|
||||
display.updateDirection(leftBlink, rightBlink);
|
||||
|
||||
unsigned long now = millis();
|
||||
float dt = (now - prevUpdate) / 1000.0f;
|
||||
if (dt >= 0.02f) {
|
||||
prevUpdate = now;
|
||||
engine.update(dt, gear.getGear());
|
||||
display.updateRPM(engine.getRPM());
|
||||
display.updateSpeed(engine.getSpeedKmh());
|
||||
}
|
||||
|
||||
long dist = sonar.measure();
|
||||
// Serial.println(dist);
|
||||
|
||||
if (rfid.check()) {
|
||||
rfid.printUID();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <Wire.h>
|
||||
|
||||
#include "Config.h"
|
||||
#include "Display.h"
|
||||
#include "Gear.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_AND_ECHO_PIN, SONAR_MAX_DIST_CM);
|
||||
RFIDReader rfid(SS_PIN, RST_PIN);
|
||||
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
|
||||
|
||||
unsigned long prevEngineUpdate = 0;
|
||||
bool carEnabled = false;
|
||||
unsigned long prevSonarUpdate = 0;
|
||||
bool seatBelts = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
Serial.println(F("Podaj wagę samochodu w kg (np. 1200):"));
|
||||
while (true) {
|
||||
if (Serial.available() > 0) {
|
||||
float mass = Serial.parseFloat();
|
||||
if (mass > 0) {
|
||||
engine.setCarMass(mass);
|
||||
Serial.print(F("Waga samochodu ustawiona na: "));
|
||||
Serial.print(mass);
|
||||
Serial.println(F(" kg"));
|
||||
break;
|
||||
} else {
|
||||
Serial.println(F("Niepoprawna wartość, spróbuj ponownie:"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pinMode(IN_1, OUTPUT);
|
||||
pinMode(IN_2, OUTPUT);
|
||||
pinMode(ENA_1, OUTPUT);
|
||||
pinMode(IN_3, OUTPUT);
|
||||
pinMode(IN_4, OUTPUT);
|
||||
pinMode(ENA_2, OUTPUT);
|
||||
|
||||
rfid.begin();
|
||||
display.begin(LCD_COLS, LCD_ROWS);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) {
|
||||
carEnabled = !carEnabled;
|
||||
|
||||
if (carEnabled)
|
||||
Serial.println("Uruchamiam auto");
|
||||
else
|
||||
Serial.println("Gaszę auto");
|
||||
|
||||
display.startOrEnd(carEnabled);
|
||||
};
|
||||
|
||||
if (carEnabled) {
|
||||
if (gear.update())
|
||||
display.updateGear(gear.getGearChar());
|
||||
|
||||
unsigned long now = millis();
|
||||
float dt = (now - prevEngineUpdate) / 1000.0f;
|
||||
if (dt >= 0.02f) {
|
||||
prevEngineUpdate = now;
|
||||
engine.update(dt, gear.getGear());
|
||||
display.updateRPM(engine.getRPM());
|
||||
display.updateSpeed(engine.getSpeedKmh());
|
||||
}
|
||||
|
||||
unsigned long now = millis();
|
||||
if (now - prevSonarUpdate >= 100) {
|
||||
prevSonarUpdate = now;
|
||||
long dist = sonar.measure();
|
||||
if (!seatBelts) {
|
||||
if (dist < 200) seatBelts = true;
|
||||
}
|
||||
else {
|
||||
if (dist >= 200) seatBelts = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,18 +11,28 @@ void RFIDReader::begin() {
|
||||
bool RFIDReader::check() {
|
||||
if (!_rfid.PICC_IsNewCardPresent()) return false;
|
||||
if (!_rfid.PICC_ReadCardSerial()) return false;
|
||||
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:"));
|
||||
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);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
const byte* RFIDReader::getUID() const {
|
||||
|
||||
@@ -10,8 +10,12 @@ public:
|
||||
|
||||
bool check();
|
||||
|
||||
void reset();
|
||||
|
||||
void printUID() const;
|
||||
|
||||
bool matches(const byte* expectedUid, byte expectedSize) const;
|
||||
|
||||
const byte* getUID() const;
|
||||
byte getUIDSize() const;
|
||||
|
||||
|
||||
@@ -1,21 +1,26 @@
|
||||
#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);
|
||||
Sonar::Sonar(int sharedPin, long maxDistCm)
|
||||
: _trigPin(sharedPin),
|
||||
_echoPin(sharedPin),
|
||||
_singlePinMode(true),
|
||||
_maxDistCm(maxDistCm),
|
||||
_lastDistance(0) {
|
||||
}
|
||||
|
||||
long Sonar::measure() {
|
||||
digitalWrite(_trigPin, LOW);
|
||||
delayMicroseconds(2);
|
||||
digitalWrite(_trigPin, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(_trigPin, LOW);
|
||||
if (_singlePinMode) {
|
||||
_lastDistance = _measureSinglePin();
|
||||
} else {
|
||||
digitalWrite(_trigPin, LOW);
|
||||
delayMicroseconds(2);
|
||||
digitalWrite(_trigPin, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(_trigPin, LOW);
|
||||
|
||||
long duration = pulseIn(_echoPin, HIGH);
|
||||
_lastDistance = duration / 58;
|
||||
long duration = pulseIn(_echoPin, HIGH, 30000UL);
|
||||
_lastDistance = duration / 58;
|
||||
}
|
||||
|
||||
if (_lastDistance > _maxDistCm) {
|
||||
_lastDistance = _maxDistCm;
|
||||
@@ -24,6 +29,24 @@ long Sonar::measure() {
|
||||
return _lastDistance;
|
||||
}
|
||||
|
||||
long Sonar::_measureSinglePin() {
|
||||
pinMode(_trigPin, OUTPUT);
|
||||
digitalWrite(_trigPin, LOW);
|
||||
delayMicroseconds(4);
|
||||
|
||||
digitalWrite(_trigPin, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(_trigPin, LOW);
|
||||
|
||||
pinMode(_echoPin, INPUT);
|
||||
|
||||
delayMicroseconds(15);
|
||||
|
||||
long duration = pulseIn(_echoPin, HIGH, 30000UL);
|
||||
|
||||
return duration / 58;
|
||||
}
|
||||
|
||||
long Sonar::getDistance() const {
|
||||
return _lastDistance;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
class Sonar {
|
||||
public:
|
||||
Sonar(int trigPin, int echoPin, long maxDistCm = 400);
|
||||
Sonar(int sharedPin, long maxDistCm = 400);
|
||||
|
||||
long measure();
|
||||
|
||||
@@ -12,6 +12,9 @@ public:
|
||||
private:
|
||||
int _trigPin;
|
||||
int _echoPin;
|
||||
bool _singlePinMode;
|
||||
long _maxDistCm;
|
||||
long _lastDistance;
|
||||
|
||||
long _measureSinglePin();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user