/
buttonled.go
56 lines (45 loc) · 1.14 KB
/
buttonled.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"github.com/mrmorphic/hwio"
"fmt"
"time"
)
const (
buttonPin = "gpio4"
ledPin = "gpio7"
)
func main() {
//value readed from button, initially set to 0, because the button will not pressed
oldValue := 0
// Set up 'button' as an input
button, e := hwio.GetPinWithMode(buttonPin, hwio.INPUT)
if e != nil {
panic(e)
}
// Set up 'led' as an output
led, e := hwio.GetPinWithMode(ledPin, hwio.OUTPUT)
if e != nil {
panic(e)
}
fmt.Printf("Beginning.....\n");
t0:= time.Now() // time 0
for {
// Read the button value
value, e := hwio.DigitalRead(button)
t1:= time.Now() // time at this point
if e != nil {
panic(e)
}
// Did value change?
if value != oldValue {
fmt.Printf("[%v] %d\n",t1.Sub(t0),value)
oldValue = value
// Write the value to the led.
if (value == 1) {
hwio.DigitalWrite(led, hwio.HIGH)
} else {
hwio.DigitalWrite(led, hwio.LOW)
}
}
}
}