func main() { board := edison.NewAdaptor() board.SetBoard("miniboard") accel := i2c.NewGroveAccelerometerDriver(board) work := func() { gobot.Every(500*time.Millisecond, func() { if x, y, z, err := accel.XYZ(); err == nil { fmt.Println(x, y, z) fmt.Println(accel.Acceleration(x, y, z)) } else { fmt.Println(err) } }) } robot := gobot.NewRobot("accelBot", []gobot.Connection{board}, []gobot.Device{accel}, work, ) robot.Start() }
func main() { board := edison.NewAdaptor() screen := i2c.NewGroveLcdDriver(board) work := func() { screen.Write("hello") screen.SetRGB(255, 0, 0) gobot.After(5*time.Second, func() { screen.Clear() screen.Home() screen.SetRGB(0, 255, 0) // set a custom character in the first position screen.SetCustomChar(0, i2c.CustomLCDChars["smiley"]) // add the custom character at the end of the string screen.Write("goodbye\nhave a nice day " + string(byte(0))) gobot.Every(500*time.Millisecond, func() { screen.Scroll(false) }) }) screen.Home() time.Sleep(1 * time.Second) screen.SetRGB(0, 0, 255) } robot := gobot.NewRobot("screenBot", []gobot.Connection{board}, []gobot.Device{screen}, work, ) robot.Start() }
func main() { master := gobot.NewMaster() api.NewAPI(master).Start() e := edison.NewAdaptor() button := gpio.NewButtonDriver(e, "2") led := gpio.NewLedDriver(e, "4") work := func() { button.On(gpio.ButtonPush, func(data interface{}) { led.On() }) button.On(gpio.ButtonRelease, func(data interface{}) { led.Off() }) } robot := gobot.NewRobot("buttonBot", []gobot.Connection{e}, []gobot.Device{led, button}, work, ) master.AddRobot(robot) master.Start() }
func main() { e := edison.NewAdaptor() led := gpio.NewLedDriver(e, "3") work := func() { brightness := uint8(0) fadeAmount := uint8(15) gobot.Every(100*time.Millisecond, func() { led.Brightness(brightness) brightness = brightness + fadeAmount if brightness == 0 || brightness == 255 { fadeAmount = -fadeAmount } }) } robot := gobot.NewRobot("pwmBot", []gobot.Connection{e}, []gobot.Device{led}, work, ) robot.Start() }
// Example of a simple led toggle without the initialization of // the entire gobot framework. // This might be useful if you want to use gobot as another // golang library to interact with sensors and other devices. func main() { e := edison.NewAdaptor() e.Connect() led := gpio.NewLedDriver(e, "13") led.Start() for { led.Toggle() time.Sleep(1000 * time.Millisecond) } }
func main() { board := edison.NewAdaptor() sensor := aio.NewGrovePiezoVibrationSensorDriver(board, "0") work := func() { sensor.On(aio.Vibration, func(data interface{}) { fmt.Println("got one!") }) } robot := gobot.NewRobot("bot", []gobot.Connection{board}, []gobot.Device{sensor}, work, ) robot.Start() }
func main() { board := edison.NewAdaptor() sensor := aio.NewGroveRotaryDriver(board, "0") work := func() { sensor.On(aio.Data, func(data interface{}) { fmt.Println("sensor", data) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{board}, []gobot.Device{sensor}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() led := gpio.NewGroveLedDriver(e, "4") work := func() { gobot.Every(1*time.Second, func() { led.Toggle() }) } robot := gobot.NewRobot("blinkBot", []gobot.Connection{e}, []gobot.Device{led}, work, ) robot.Start() }
func main() { board := edison.NewAdaptor() sensor := aio.NewGroveTemperatureSensorDriver(board, "0") work := func() { gobot.Every(500*time.Millisecond, func() { fmt.Println("current temp (c): ", sensor.Temperature()) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{board}, []gobot.Device{sensor}, work, ) robot.Start() }
func main() { board := edison.NewAdaptor() buzzer := gpio.NewBuzzerDriver(board, "3") work := func() { type note struct { tone float64 duration float64 } song := []note{ {gpio.C4, gpio.Quarter}, {gpio.C4, gpio.Quarter}, {gpio.G4, gpio.Quarter}, {gpio.G4, gpio.Quarter}, {gpio.A4, gpio.Quarter}, {gpio.A4, gpio.Quarter}, {gpio.G4, gpio.Half}, {gpio.F4, gpio.Quarter}, {gpio.F4, gpio.Quarter}, {gpio.E4, gpio.Quarter}, {gpio.E4, gpio.Quarter}, {gpio.D4, gpio.Quarter}, {gpio.D4, gpio.Quarter}, {gpio.C4, gpio.Half}, } for _, val := range song { buzzer.Tone(val.tone, val.duration) time.Sleep(10 * time.Millisecond) } } robot := gobot.NewRobot("bot", []gobot.Connection{board}, []gobot.Device{buzzer}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() led := gpio.NewRgbLedDriver(e, "3", "5", "6") work := func() { gobot.Every(1*time.Second, func() { r := uint8(gobot.Rand(255)) g := uint8(gobot.Rand(255)) b := uint8(gobot.Rand(255)) led.SetRGB(r, g, b) }) } robot := gobot.NewRobot("rgbBot", []gobot.Connection{e}, []gobot.Device{led}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() e.Connect() led := gpio.NewLedDriver(e, "13") led.Start() led.Off() button := gpio.NewButtonDriver(e, "5") button.Start() buttonEvents := button.Subscribe() for { select { case event := <-buttonEvents: fmt.Println("Event:", event.Name, event.Data) if event.Name == gpio.ButtonPush { led.Toggle() } } } }
func main() { e := edison.NewAdaptor() led := gpio.NewLedDriver(e, "13") // Uncomment the line below if you are using a Sparkfun // Edison board with the GPIO block // e.SetBoard("sparkfun") work := func() { gobot.Every(1*time.Second, func() { led.Toggle() }) } robot := gobot.NewRobot("blinkBot", []gobot.Connection{e}, []gobot.Device{led}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() touch := gpio.NewGroveTouchDriver(e, "2") work := func() { touch.On(gpio.ButtonPush, func(data interface{}) { fmt.Println("On!") }) touch.On(gpio.ButtonRelease, func(data interface{}) { fmt.Println("Off!") }) } robot := gobot.NewRobot("blinkBot", []gobot.Connection{e}, []gobot.Device{touch}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() blinkm := i2c.NewBlinkMDriver(e) work := func() { gobot.Every(3*time.Second, func() { r := byte(gobot.Rand(255)) g := byte(gobot.Rand(255)) b := byte(gobot.Rand(255)) blinkm.Rgb(r, g, b) color, _ := blinkm.Color() fmt.Println("color", color) }) } robot := gobot.NewRobot("blinkmBot", []gobot.Connection{e}, []gobot.Device{blinkm}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() button := gpio.NewButtonDriver(e, "5") led := gpio.NewLedDriver(e, "13") work := func() { button.On(gpio.ButtonPush, func(data interface{}) { led.On() }) button.On(gpio.ButtonRelease, func(data interface{}) { led.Off() }) } robot := gobot.NewRobot("buttonBot", []gobot.Connection{e}, []gobot.Device{led, button}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() sensor := aio.NewAnalogSensorDriver(e, "0") led := gpio.NewLedDriver(e, "3") work := func() { sensor.On(aio.Data, func(data interface{}) { brightness := uint8( gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 4096), 0, 255), ) fmt.Println("sensor", data) fmt.Println("brightness", brightness) led.Brightness(brightness) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{e}, []gobot.Device{sensor, led}, work, ) master.Start() }
func main() { master := gobot.NewMaster() a := api.NewAPI(master) a.Start() board := edison.NewAdaptor() red := gpio.NewLedDriver(board, "3") green := gpio.NewLedDriver(board, "5") blue := gpio.NewLedDriver(board, "6") button := gpio.NewButtonDriver(board, "7") enabled := true work := func() { red.Brightness(0xff) green.Brightness(0x00) blue.Brightness(0x00) flash := false on := true gobot.Every(250*time.Millisecond, func() { if enabled { if flash { if on { red.Brightness(0x00) green.Brightness(0xff) blue.Brightness(0x00) on = false } else { red.Brightness(0xff) green.Brightness(0x00) blue.Brightness(0x00) on = true } } } }) button.On(gpio.ButtonPush, func(data interface{}) { flash = true }) button.On(gpio.ButtonRelease, func(data interface{}) { flash = false red.Brightness(0x00) green.Brightness(0x00) blue.Brightness(0xff) }) } robot := gobot.NewRobot( "square of fire", []gobot.Connection{board}, []gobot.Device{red, green, blue, button}, work, ) robot.AddCommand("enable", func(params map[string]interface{}) interface{} { enabled = !enabled return enabled }) master.AddRobot(robot) master.Start() }