Skip to content

Commit c7a594c

Browse files
committed
add number of sensor in constructor multi
1 parent d28f90d commit c7a594c

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void loop()
2828
```ino
2929
#include <HCSR04.h>
3030
31-
HCSR04 hc(2,new int[6]{5,6,7,8,9,10});//initialisation class HCSR04 (trig pin , echo pin)
31+
HCSR04 hc(2,new int[6]{5,6,7,8,9,10},6);//initialisation class HCSR04 (trig pin , echo pins, number of sensors)
3232
3333
void setup()
3434
{ Serial.begin(9600); }

examples/HCSR04_multi/HCSR04_multi.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <HCSR04.h>
22

3-
HCSR04 hc(2,new int[6]{5,6,7,8,9,10});//initialisation class HCSR04 (trig pin , echo pin)
3+
HCSR04 hc(2,new int[6]{5,6,7,8,9,10},6);//initialisation class HCSR04 (trig pin , echo pin, number of sensor)
44

55
void setup()
66
{ Serial.begin(9600); }

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=HCSR04 ultrasonic sensor
2-
version=2.0.0
2+
version=2.0.1
33
author=gamegine
44
maintainer=gamegine
55
sentence=Allows an Arduino board to use HCSR04 module.

src/HCSR04.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#include "HCSR04.h"
22
////////////////////////////////////consttruct/destruct
3-
void HCSR04::init(int out,int echo[])
3+
void HCSR04::init(int out,int echo[],int n)
44
{
55
this->out = out;
66
this->echo = echo;
7+
this->n = n;
78
pinMode(this->out, OUTPUT);
8-
for(int i=0;i<sizeof(echo);i++)
9-
{ pinMode(this->echo[i], INPUT); }
9+
for(int i=0;i<n;i++){ pinMode(this->echo[i], INPUT); }
1010
}
11-
HCSR04::HCSR04(int out,int echo){this->init(out,new int[1]{echo});}
12-
HCSR04::HCSR04(int out,int echo[]){this->init(out,echo);}
11+
HCSR04::HCSR04(int out,int echo){this->init(out,new int[1]{echo},1);}
12+
HCSR04::HCSR04(int out,int echo[],int n){this->init(out,echo,n);}
1313
HCSR04::~HCSR04()
1414
{
1515
~this->out;
1616
delete[] this->echo;
17+
~this->n;
1718
}
1819
///////////////////////////////////////////////////dist
1920
float HCSR04::dist(int n) const

src/HCSR04.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ class HCSR04
33
{
44
public:
55
HCSR04(int out,int echo); //initialisation class HCSR04 (trig pin , echo pin)
6-
HCSR04(int out,int echo[]); //initialisation class HCSR04 (trig pin , echo pin)
6+
HCSR04(int out,int echo[],int n); //initialisation class HCSR04 (trig pin , echo pin)
77
~HCSR04(); //destructor
88
float dist() const; //return curent distance of element 0
99
float dist(int n) const; //return curent distance of element 0
1010

1111
private:
12-
void init(int out,int echo[]); //for constructor
12+
void init(int out,int echo[],int n); //for constructor
1313
int out; //out pin
1414
int *echo; //echo pin list
15+
int n; //number of el
1516
};

0 commit comments

Comments
 (0)