Easy Relay 4 - Go example
package main
import (
"fmt"
"github.com/stianeikeland/go-rpio"
"os"
"time"
)
var (
// the relays have io numbers 16,17,22,23 when connected via the 2x20 header
// if you wire manually via the 8-pin header then these numbers can be different
rel1 = rpio.Pin(16)
rel2 = rpio.Pin(17)
rel3 = rpio.Pin(22)
rel4 = rpio.Pin(23)
)
func main() {
// open map and memory access
if err :=rpio.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// unmap gpio memory when done
defer rpio.Close()
// set pins to output mode
rel1.Output()
rel2.Output()
rel3.Output()
rel4.Output()
// turn each relay on/off for a second, repeating 10 times
for x := 0; x < 10; x++ {
rel1.High()
time.Sleep(1*time.Second)
rel1.Low()
rel2.High()
time.Sleep(1*time.Second)
rel1.Low()
rel3.High()
time.Sleep(1*time.Second)
rel3.Low()
rel4.High()
time.Sleep(1*time.Second)
rel4.Low()
}
}