/*
# Date: 2016-11-10
# Author: simonzhang
# web: www.simonzhang.net
# Email: simon-zzm@163.com
*/
package main
import (
"os"
"github.com/stianeikeland/go-rpio"
"time"
)
var (
aio = rpio.Pin(14)
fio = rpio.Pin(4)
alertCount int = 0
)
// 风扇端口
func fWrite(status string){
fio.Output()
if status == "high"{
fio.High()
}else if status == "low"{
fio.Low()
}
}
// 监控端口
func aRead() int{
aio.Input()
return int(aio.Read())
}
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
// 因为没有while 直接用for
for {
// 数字不能太大
// 1是内存问题,2是停止风扇范围
if aRead() == 1 {
if alertCount > 0{
alertCount -= 1
}
}else{
if alertCount <= 10{
alertCount += 1
}
}
//print(alertCount, " ", aRead(), "\n")
//
if alertCount > 3{
fWrite("high")
} else {
fWrite("low")
}
// 每次次循环等待一秒钟
time.Sleep(1000 * time.Millisecond)
}
}
有疑问加站长微信联系(非本文作者)