Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

GOBOT+Arduinoでサーボモーターを動かしてみる その3:複数個動かしてみる

shuzo-kino.hateblo.jp

package main

import (
	"fmt"
	"time"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/drivers/gpio"
	"gobot.io/x/gobot/platforms/firmata"
)

func main() {
	firmataAdaptor := firmata.NewAdaptor("/dev/tty.usbmodem1421")
	servo1 := gpio.NewServoDriver(firmataAdaptor, "3")
	servo2 := gpio.NewServoDriver(firmataAdaptor, "4")
	count := 1

	work := func() {
		gobot.Every(1*time.Second, func() {
			count = (count + 1) % 254
			if Odd(count) {
				moveVal := (count * 20) % 180
				fmt.Println("Turning Servo1", moveVal)
				servo1.Move(uint8(moveVal))
			} else {
				moveVal := (count * 20) % 180
				fmt.Println("Turning Servo1", moveVal)
				fmt.Println("Turning Servo2", moveVal+100)
				servo1.Move(uint8(moveVal))
				servo2.Move(uint8(moveVal))
			}
		})
	}

	robot := gobot.NewRobot("servoBot",
		[]gobot.Connection{firmataAdaptor},
		[]gobot.Device{servo1, servo2},
		work,
	)

	robot.Start()
}