gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_direct_pin.go (about) 1 //go:build example 2 // +build example 3 4 // 5 // Do not build by default. 6 7 package main 8 9 import ( 10 "fmt" 11 "time" 12 13 "gobot.io/x/gobot/v2" 14 "gobot.io/x/gobot/v2/drivers/gpio" 15 "gobot.io/x/gobot/v2/platforms/adaptors" 16 "gobot.io/x/gobot/v2/platforms/tinkerboard" 17 ) 18 19 // Wiring 20 // PWR Tinkerboard: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND) 21 // GPIO Tinkerboard: header pin 21 is input, pin 24 used as normal output, pin 26 used as inverted output 22 // Button: the input pin is wired with a button to GND, an external pull up resistor is needed (e.g. 1K) 23 // LED's: the output pins are wired to the cathode of a LED, the anode is wired with a resistor (70-130Ohm for 20mA) to VCC 24 // Expected behavior: always one LED is on, the other in opposite state, on button press the state changes 25 func main() { 26 const ( 27 inPinNum = "21" 28 outPinNum = "24" 29 outPinInvertedNum = "26" 30 ) 31 board := tinkerboard.NewAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum)) 32 inPin := gpio.NewDirectPinDriver(board, inPinNum) 33 outPin := gpio.NewDirectPinDriver(board, outPinNum) 34 outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum) 35 36 work := func() { 37 level := byte(1) 38 39 gobot.Every(500*time.Millisecond, func() { 40 read, err := inPin.DigitalRead() 41 fmt.Printf("pin %s state is %d\n", inPinNum, read) 42 if err != nil { 43 fmt.Println(err) 44 } else { 45 level = byte(read) 46 } 47 48 err = outPin.DigitalWrite(level) 49 fmt.Printf("pin %s is now %d\n", outPinNum, level) 50 if err != nil { 51 fmt.Println(err) 52 } 53 54 err = outPinInverted.DigitalWrite(level) 55 fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level) 56 if err != nil { 57 fmt.Println(err) 58 } 59 60 if level == 1 { 61 level = 0 62 } else { 63 level = 1 64 } 65 66 }) 67 } 68 69 robot := gobot.NewRobot("pinBot", 70 []gobot.Connection{board}, 71 []gobot.Device{inPin, outPin, outPinInverted}, 72 work, 73 ) 74 75 robot.Start() 76 }