Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Wednesday, 23 April 2014

Wii Mouse

One of the fun things you can do on the Leonardo and Yun boards is send "HID" messages over USB to a computer. HID stands for "Human Interface Devce", and basically means any kind of Joystick, Trackpad, Mouse or Keyboard.  While this features main application appears to be practical jokes, such as moving someones mouse randomly, or logging them out at unexpected times, it can actually be useful.

One thing you can easily do is hook up a wii nunchuck controller and pretend its a mouse - handy if you have to stand at the front of a room demoing some software in front of a projector, and don't want to be tied to a desk. Similar "Air Mouse" devices are available commercially, but building one yourself is far more fun, and totally configurable (and its cheaper even if you buy all the parts especially for the project - you can get a Leo + wii nunchuck for less than $10 on ebay).

Lets start with the nun-chuck: It turns out Nintendo used i2c to communicate between the Wii and the Nunchuck controller, so you can just cut the Wii connector off a cheap controller and solder on some pins to connect it to an Arduino. Unfortunately you may not know what each wire does! You can figure it out pretty easily with a multimeter, or you can by an adapter on ebay which plugs into the Wii plug and breaks out the pins. I took the middle route - I bought an adapter, got it working, then used the adapter to help me find the correct wires.

There are 4 wires to connect - ground, power, data and clock. Strictly the wii controller is a 3.3V device, but it works OK at 5V (I used 3.3v for mine). Watch out when connecting Data and Clock - on an Uno these are on pins A4 and A5 respectively, but on an Leonardo or Yun they're on the last two (probably unmarked!) pins next to D13, and ARef. Data is the one next to ARef, and Clock is the final pin. Strictly speaking we're taking some liberties with the voltages here, but the Ninchuck's don't seem to mind - If you're wiring this up permanently you might like to use a slightly more legitimate approach to i2C wiring, but this is fine for a fun test...

On to the code:

make i2c device
make wiiNunchuck device
make wiiZbutton boolean
make wiiCbutton boolean
make wiiJoyX number
make wiiJoyY number
make wiiAccX number
make wiiAccY number
make wiiAccZ number

when start
.forever
..tell wiiNunchuck to "read"

OK that's a lot of lines, but actually that's just because the Wii controller collects a lot of data! For a couple of dollars you get a Joystick, two buttons and an accelerometer. Each time we call read on the nunChuck device it updates all of these values.

To pretent to be a fake mouse, we need to create a usb and a mouse device:

make usb device
make fakeMouse device
make mouseDeltaX number
make mouseDeltaY number

Now we can code the actions we want to perform:

when start
.forever
..set mouseDeltaX to wiiJoyX*0.5
..set mouseDeltaY to -wiiJoyY*0.5
..tell fakeMouse to "move"
..wait 0.02 secs

when start
.forever
..wait until wiiZbutton
..tell fakeMouse to "leftClick"
..wait until not wiiZbutton

when start
.forever
..wait until wiiCbutton
..tell fakeMouse to "rightClick"
..wait until not wiiCbutton


I first mapped the Joystick position to the mouse movement. Of course you could also use the accelerometer.  You can also play with the scaling and timing to adjust the speed of the mouse pointer. Once we've decided how much to move the mouse pointer just 'tell fakeMouse to "move"'.

Simlarly I mapped the two wii buttons to left and right click. Currently the fakeMouse device restricts mouse actions to clicks rather than up/down so that there's always a valid up/down->click pairing, rather than potentially sending a down, and forgetting to send an up. This could be changed - feedback is always appreciated.

And that's it - plug it together, double click the leo/yun reset button, then run leo-sniff (yun-sniff). Occasionally the code downloaded into a yun/leo doesn't start running immediately, so if it doesn't work, hit reset once more. Downloading is a bit more fiddly with these Arduinos - unfortunately its known problem that happens even when you're running the official Arduino software.

There's lots more you could do with this - you could map the accelerometer the mouse movement, and use the joystick for send pre-defined key presses, for example to move forwards and backwards through a presentation. You could even add some buttons and a screen to the Arduino, to select different configurations, or maybe even automatically type predefined messages.

2 comments:

  1. If you are a gaming lover then you must have already been aware of the Top gaming mice, and in case if you are not aware then bestgamingmice.net we are here to make you aware the best options.

    ReplyDelete
  2. Great Platform. Are you also searching for nursing essay writers ? we are the best solution for you. We are best known for offering the best nursing essay writing services

    ReplyDelete