From c1dba90a5a00d96e60fc2762e9a4f97ab91a3667 Mon Sep 17 00:00:00 2001 From: Sverre <59171289+sverben@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:34:17 +0100 Subject: [PATCH] feat: add setUSPins() to allow overriding distance sensor pins (#5) --- library.properties | 2 +- src/Leaphyoriginal1.cpp | 17 ++++++++++++----- src/Leaphyoriginal1.h | 8 ++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/library.properties b/library.properties index a5bf9af..3378823 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Leaphy Original Extension -version=0.0.2 +version=0.0.3 author=Leaphy Robotics maintainer=Leaphy Robotics sentence=Provides functionality to program Leaphy Original robots diff --git a/src/Leaphyoriginal1.cpp b/src/Leaphyoriginal1.cpp index 3eb1090..d2f1756 100644 --- a/src/Leaphyoriginal1.cpp +++ b/src/Leaphyoriginal1.cpp @@ -16,18 +16,25 @@ void setMotorPins(int m1_pwm, int m1_dir, int m2_pwm, int m2_dir) } +void setUSPins(int echo, int trig) +{ + us_pins.echo = echo; + us_pins.trig = trig; +} + + float getDistance() { float duration, distance; int tries = 0; - pinMode(US_TRIG, OUTPUT); - digitalWrite(US_TRIG, LOW); + pinMode(us_pins.trig, OUTPUT); + digitalWrite(us_pins.trig, LOW); delayMicroseconds(2); - digitalWrite(US_TRIG, HIGH); + digitalWrite(us_pins.trig, HIGH); delayMicroseconds(10); - digitalWrite(US_TRIG, LOW); - duration = pulseIn(US_ECHO, HIGH, 30000); + digitalWrite(us_pins.trig, LOW); + duration = pulseIn(us_pins.echo, HIGH, 30000); distance = 0.034 * duration / 2; if(distance == 0 ){ distance = distance + 1313; diff --git a/src/Leaphyoriginal1.h b/src/Leaphyoriginal1.h index 5c72320..c36d2b2 100644 --- a/src/Leaphyoriginal1.h +++ b/src/Leaphyoriginal1.h @@ -32,8 +32,6 @@ #define LED1_BLUE (3) // PWM-OUT #define LINE_LEFT (16) // DIG-IN A2 tinkerkit In2 #define LINE_RIGHT (17) // DIG-IN A3 tinkerkit In3 -#define US_ECHO (8) // DIG-IN/OUT -#define US_TRIG (7) // DIG-OUT struct { int m1_pwm = 10; @@ -42,7 +40,13 @@ struct { int m2_dir = 13; } motor_pins; +struct { + int echo = 8; + int trig = 7; +} us_pins; + void setMotorPins(int m1_pwm, int m1_dir, int m2_pwm, int m2_dir); +void setUSPins(int echo, int trig); float getDistance(); int getLineFollower(int fpSide); void setTone( int fpTone, int fpBeat);