-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Gnyblast/master
Adding water flow sensor to the library
- Loading branch information
Showing
4 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev | ||
|
||
import "github.com/stianeikeland/go-rpio/v4" | ||
|
||
// WaterFlowMeter implements Detector interface | ||
type WaterFlowMeter struct { | ||
pin rpio.Pin | ||
} | ||
|
||
// NewWaterFlowMeter ... | ||
func NewWaterFlowMeter(pin uint8) *WaterFlowMeter { | ||
w := &WaterFlowMeter{ | ||
pin: rpio.Pin(pin), | ||
} | ||
w.pin.Input() | ||
return w | ||
} | ||
|
||
func (w *WaterFlowMeter) Detected() bool { | ||
return w.pin.Read() == rpio.High | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/shanghuiyang/rpi-devices/dev" | ||
) | ||
|
||
const pulsesPerLiter float32 = 450 | ||
|
||
func main() { | ||
w := dev.NewWaterFlowMeter(17) | ||
var numberOfPulsesCounted int = 0 | ||
|
||
for { | ||
if w.Detected() { | ||
numberOfPulsesCounted++ | ||
fmt.Printf("MiliLiters Flowed: %f", (float32(1000*numberOfPulsesCounted) / pulsesPerLiter)) | ||
continue | ||
} | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.