Skip to content

Commit

Permalink
Merge pull request #5 from Gnyblast/master
Browse files Browse the repository at this point in the history
Adding water flow sensor to the library
  • Loading branch information
shanghuiyang authored Aug 13, 2024
2 parents 533f13a + 3dc177e commit e5276fe
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func main() {
|SW-420|![](img/sw-420.jpg)|Shaking sensor|[example](/example/sw420/main.go)|[auto-air-out](https://github.com/shanghuiyang/rpi-projects/tree/main/projects/autoairout)|
|US-100|![](img/us-100.jpg)|Ultrasonic distance meter|[example](/example/us100/main.go)|[car](https://github.com/shanghuiyang/rpi-projects/tree/main/projects/car)|
|Voice Detector|![](img/voice.jpg)|Voice detector|N/A|N/A|
|Water Flow Sensor|![](img/water_flow_sensor.jpg)|Water flow sensor|[example](/example/water_flow_sensor/main.go)|N/A|
|ZE08-CH2O|![](img/ze08-ch2o.jpg)|CH2O sensor|[example](/example/ze08ch2o/main.go)|[ch2o-monitor](https://github.com/shanghuiyang/rpi-projects/tree/main/projects/ch2omonitor)|
|ZP16|![](img/zp16.jpg)|Gas detector|[example](/example/zp16/main.go)|[home-asst](https://github.com/shanghuiyang/rpi-projects/tree/main/projects/homeasst)|

Expand Down
21 changes: 21 additions & 0 deletions dev/water_flow_meter.go
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
}
25 changes: 25 additions & 0 deletions example/water_flow_sensor/main.go
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)
}
}
Binary file added img/water_flow_sensor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e5276fe

Please sign in to comment.