Here is my Christmas project, to create a USB joystick using an accelerometer.
The accelerometer I used is an ADXL345 (http://www.analog.com/static/imported-f ... DXL345.pdf ) bought on ebay. This device outputs its data via a serial bus, either I2C or SPI, I have used I2C.
The USB joystick settings came from http://www.matrixmultimedia.com/mmforum ... =26&t=6536
Here is a breakdown of the program:
Main, initializes USB Hid, calls macro to setup the sensor and then has an infinite loop it which it reads the accelerometer every 10ms.
setup_adxl345 which does the following:
- Register 0x2C is used to set the data rate (200Hz)
Register 0x31 is used to set the range and resolution (16g and full,13bit)
Register 0x2D is used to start the measurement.
read_adxl345 this does the following:
- Reads in 6 bytes of data.
Combine 2 bytes of data for each channel to give a signed integer.
Use adjustReading macro to add offset and scale the channels, this uses floating point calculation.
Use limiter macro to limit the channel output to ±120.
Gary
PS Have just modified it so it works as a mouse, so apart from changing the USB Hid properties the only other change was to reduce the x and y values by dividing them by 10, this value is used for the speed value. My next version will use a 3 axis gyro, as this should work a lot more intuitively for user.