加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

raspberry pi 2用golang 控制GPIO

发布时间:2020-12-16 18:20:56 所属栏目:大数据 来源:网络整理
导读:/* # 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 ) // 风

/*
# 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) } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读