Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created March 30, 2014 18:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/9877328 to your computer and use it in GitHub Desktop.
Save peterhellberg/9877328 to your computer and use it in GitHub Desktop.
Gobot: Controlling LEDs connected to an Arduino over Firmata using a PS3 DualShock 3 controller.
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-firmata"
"github.com/hybridgroup/gobot-gpio"
"github.com/hybridgroup/gobot-joystick"
)
func main() {
firmata := new(gobotFirmata.FirmataAdaptor)
firmata.Name = "firmata"
firmata.Port = "/dev/tty.usbmodem1411"
joystickAdaptor := new(gobotJoystick.JoystickAdaptor)
joystickAdaptor.Name = "ps3 adaptor"
// Config for the PS3 controller downloaded from
// https://github.com/hybridgroup/gobot-joystick/raw/master/configs/dualshock3.json
//
joystickAdaptor.Params = map[string]interface{}{
"config": "./dualshock3.json",
}
joystick := gobotJoystick.NewJoystick(joystickAdaptor)
joystick.Name = "ps3 joystick"
green := gobotGPIO.NewLed(firmata)
green.Pin = "11"
red1 := gobotGPIO.NewLed(firmata)
red1.Pin = "10"
red2 := gobotGPIO.NewLed(firmata)
red2.Pin = "9"
red3 := gobotGPIO.NewLed(firmata)
red3.Pin = "6"
red4 := gobotGPIO.NewLed(firmata)
red4.Pin = "5"
red5 := gobotGPIO.NewLed(firmata)
red5.Pin = "3"
work := func() {
// The digital buttons have a <name>_press event
gobot.On(joystick.Events["triangle_press"], func(interface{}) {
fmt.Println("triangle_press")
green.Toggle()
})
// The digital buttons also have a <name>_release event
gobot.On(joystick.Events["up_release"], func(interface{}) {
fmt.Println("up_release")
})
// The data interface contains values between -32767 and 32767
gobot.On(joystick.Events["left_x"], func(data interface{}) {
// Use type assertion to make sure that we have a value of type int16
v := data.(int16)
// Ignore values of 5k and below
// (garbage precision between -1500 and 1500)
if v > 5000 && v < 10000 {
fmt.Println("* ", v)
red1.On()
red2.Off()
red3.Off()
red4.Off()
red5.Off()
} else if v > 10000 && v < 15000 {
fmt.Println("** ", v)
red1.On()
red2.On()
red3.Off()
red4.Off()
red5.Off()
} else if v > 15000 && v < 20000 {
fmt.Println("*** ", v)
red1.On()
red2.On()
red3.On()
red4.Off()
red5.Off()
} else if v > 20000 && v < 25000 {
fmt.Println("**** ", v)
red1.On()
red2.On()
red3.On()
red4.On()
red5.Off()
} else if v > 25000 {
fmt.Println("*****", v)
red1.On()
red2.On()
red3.On()
red4.On()
red5.On()
} else {
red1.Off()
red2.Off()
red3.Off()
red4.Off()
red5.Off()
}
})
}
robot := gobot.Robot{
Connections: []gobot.Connection{firmata, joystickAdaptor},
Devices: []gobot.Device{joystick},
Work: work,
}
robot.Start()
}
@peterhellberg
Copy link
Author

Issues

I’m seeing 100% CPU usage for some reason, still not sure why though.
I was unable to pinpoint the problem using pprof.
My current theory is that the problem lies somewhere in the usage of SDL.

EDIT: This has now been fixed.

Breadboard

Arduino

I replaced the leftmost red LED with a green one for this experiment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment