Skip to content

Commit 52476c5

Browse files
committed
add setType and setPin function, fix INPUT_PULLUP
1 parent a3bddfc commit 52476c5

File tree

4 files changed

+79
-26
lines changed

4 files changed

+79
-26
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ Choose FlowSensor-Arduino-master.zip that previously downloaded.
5959
| Sensor Type | Code | Pulse/Liter |
6060
| ------------- | ------- | ----------: |
6161
| YF-S201 | YFS201 | 450 |
62+
| YF-S401 | YFS401 | 5880 |
6263
| YF-B1 | YFB1 | 660 |
64+
| YF-B1-S | YFB1S | 1077 |
6365
| OF10ZAT | OF10ZAT | 400 |
6466
| OF10ZZT | OF10ZZT | 400 |
6567
| OF05ZAT | OF05ZAT | 2174 |
6668
| OF05ZZT | OF05ZZT | 2174 |
67-
| YF-B1-S | YFB1S | 1077 |
6869

6970
## ✨ Add New Sensor
7071
If you want to add new sensor you can edit [FlowSensor_Type.h](https://github.com/hafidhh/FlowSensor-Arduino/blob/master/src/FlowSensor_Type.h) file in src and create pull request or you can use dynamic declaration.
@@ -74,6 +75,7 @@ See [all examples](https://github.com/hafidhh/FlowSensor-Arduino/blob/master/exa
7475

7576
### Initialization
7677
If your sensor is not available in sensor list you can use dynamic declaration by use sensor pulse/liter in type, example 450 for YF-S201.
78+
7779
```cpp
7880
#include <FlowSensor.h>
7981

@@ -83,10 +85,20 @@ uint8_t pin = D2; // pin : interrupt pin
8385

8486
FlowSensor Sensor(type, pin);
8587
```
86-
or if you sensor is not available in sensor list you can use pulse/liter sensor in type.
88+
89+
if you want to change sensor pin and sensor type you can use setPin and setType.
90+
if you change the pin with setPin() you have to do sensor.begin() again
91+
92+
```cpp
93+
Sensor.setPin(pin);
94+
Sensor.setType(type);
95+
```
96+
8797

8898
### Sensor begin
89-
param **`userFunc`**
99+
param **`userFunc`**
100+
param **`pullup`**
101+
if you have an external pull up you can set pull up to true
90102
```cpp
91103
void count()
92104
{
@@ -99,6 +111,18 @@ void setup()
99111
}
100112
```
101113

114+
### Set Sensor Pin
115+
param **`pin`**
116+
```cpp
117+
Sensor.setPin(pin);
118+
```
119+
120+
### Set Sensor Type
121+
param **`type`**
122+
```cpp
123+
Sensor.setType(type);
124+
```
125+
102126
### Sensor Read
103127
```cpp
104128
Sensor.read()

keyword.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ FlowSensor KEYWORD1
1515
begin KEYWORD2
1616
read KEYWORD2
1717
count KEYWORD2
18+
setPin KEYWORD2
19+
setType KEYWORD2
1820
getPulse KEYWORD2
19-
resetPulse KEYWORD2
2021
getFlowRate_s KEYWORD2
2122
getFlowRate_m KEYWORD2
2223
getFlowRate_h KEYWORD2
2324
getVolume KEYWORD2
25+
resetPulse KEYWORD2
2426
resetVolume KEYWORD2

src/FlowSensor.cpp

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,39 @@ FlowSensor::FlowSensor(uint16_t type ,uint8_t pin)
2525
}
2626

2727
/**
28-
* @brief
28+
* @brief setType, change type
29+
*
30+
* @param type
31+
*/
32+
void FlowSensor::setType(uint16_t type)
33+
{
34+
this->_pulse1liter = type;
35+
}
36+
37+
/**
38+
* @brief setPin, change pin
39+
*
40+
* @param pin
41+
*/
42+
void FlowSensor::setPin(uint8_t pin)
43+
{
44+
detachInterrupt(this->_pin);
45+
this->_pin = pin;
46+
}
47+
48+
/**
49+
* @brief sensor begin
2950
*
3051
* @param userFunc
52+
* @param pullup default value is fasle (0), if you have external pull up you can set to true (1)
3153
*/
32-
void FlowSensor::begin(void (*userFunc)(void))
54+
void FlowSensor::begin(void (*userFunc)(void), bool pullup = false)
3355
{
34-
pinMode(this->_pin, INPUT);
35-
digitalWrite(this->_pin, INPUT_PULLUP); // Optional Internal Pull-Up
56+
if (pullup == true)
57+
pinMode(this->_pin, INPUT);
58+
else
59+
pinMode(this->_pin, INPUT_PULLUP); // Optional Internal Pull-Up
60+
3661
attachInterrupt(digitalPinToInterrupt(this->_pin), userFunc, RISING); // For better compatibility with any board, for example Arduino Leonardo Boards
3762
}
3863

@@ -46,7 +71,7 @@ void FlowSensor::count()
4671
}
4772

4873
/**
49-
* @brief
74+
* @brief read sensor, get value
5075
*
5176
* @param calibration Calibration pulse/liter
5277
*/
@@ -60,7 +85,7 @@ void FlowSensor::read(long calibration)
6085
}
6186

6287
/**
63-
* @brief
88+
* @brief get total pulse
6489
*
6590
* @return unsigned long _totalpulse
6691
*/
@@ -70,17 +95,7 @@ unsigned long FlowSensor::getPulse()
7095
}
7196

7297
/**
73-
* @brief Reset pulse count
74-
*
75-
*/
76-
void FlowSensor::resetPulse()
77-
{
78-
this->_pulse=0;
79-
this->_totalpulse=0;
80-
}
81-
82-
/**
83-
* @brief
98+
* @brief get value flowrate/hour (L/h)
8499
*
85100
* @return float flow rate / hour
86101
*/
@@ -91,7 +106,7 @@ float FlowSensor::getFlowRate_h()
91106
}
92107

93108
/**
94-
* @brief
109+
* @brief get value flowrate/minute (L/m)
95110
*
96111
* @return float flow rate / minute
97112
*/
@@ -102,17 +117,17 @@ float FlowSensor::getFlowRate_m()
102117
}
103118

104119
/**
105-
* @brief
120+
* @brief get value flowrate/second (L/s)
106121
*
107-
* @return float flow rate / secound
122+
* @return float flow rate / second
108123
*/
109124
float FlowSensor::getFlowRate_s()
110125
{
111126
return this->_flowratesecound;
112127
}
113128

114129
/**
115-
* @brief
130+
* @brief get Volume value (L)
116131
*
117132
* @return float volume
118133
*/
@@ -121,6 +136,16 @@ float FlowSensor::getVolume()
121136
return this->_volume;
122137
}
123138

139+
/**
140+
* @brief Reset pulse count
141+
*
142+
*/
143+
void FlowSensor::resetPulse()
144+
{
145+
this->_pulse=0;
146+
this->_totalpulse=0;
147+
}
148+
124149
/**
125150
* @brief reset Volume
126151
*

src/FlowSensor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ class FlowSensor
3838
void begin(void (*userFunc)(void));
3939
void count();
4040
void read(long calibration = 0);
41+
void setType(uint16_t type);
42+
void setPin(uint8_t pin);
4143
unsigned long getPulse();
42-
void resetPulse();
4344
float getFlowRate_h();
4445
float getFlowRate_m();
4546
float getFlowRate_s();
4647
float getVolume();
48+
void resetPulse();
4749
void resetVolume();
4850
};
4951

0 commit comments

Comments
 (0)