NES controller w/ Accelerometer...
hello all, i'm building nes controller build in accelerometer. i based of source code , practicaly else off:
3xw.ladyada.net/make/usbgamepad/index.html
(can't post link believe spoofing show credit acceptable)
so big guide!
i'm using teensy 2.0 , arduino 1.8 , patch listed on guide above.
my code posted below, i'm running 2 issues, easy fix i'm stumped!
first, code below works when press buttons, however, when hold start , select want fcnmouse loop, don't want keyboard inputs. currently mouse move when hold both down (woooot!) outputs keyboard letters (baaaad!).
the second issue, i've commented code out, last part of fcnmouse want use pb , pa left , right click...but reason won't compile mouse.click(1,0,0) i'm useing mouse.click wrong? i tried find can't seem find it.
also, suggestions making mouse more fluent or else (lady's guide says there's easier way....but if there's more effecient i'm it!)
code:
3xw.ladyada.net/make/usbgamepad/index.html
(can't post link believe spoofing show credit acceptable)
so big guide!
i'm using teensy 2.0 , arduino 1.8 , patch listed on guide above.
my code posted below, i'm running 2 issues, easy fix i'm stumped!
first, code below works when press buttons, however, when hold start , select want fcnmouse loop, don't want keyboard inputs. currently mouse move when hold both down (woooot!) outputs keyboard letters (baaaad!).
the second issue, i've commented code out, last part of fcnmouse want use pb , pa left , right click...but reason won't compile mouse.click(1,0,0) i'm useing mouse.click wrong? i tried find can't seem find it.
also, suggestions making mouse more fluent or else (lady's guide says there's easier way....but if there's more effecient i'm it!)
code:
code: [select]
const int panxin = 3;
const int panyin = 2;
const int panzin = 1;
const int pandumb = 0;
#define keyrepeat 100 // milliseconds
#define keydelay 200 // delay first second character
const int pup = 0;
const int pright = 1;
const int pdown = 2;
const int pleft = 3;
const int pb = 4;
const int pa = 5;
const int pselect = 6;
const int pstart = 7;
const int pinled = 11;
//variables states of nes buttons
byte buttons[] = {pup, pright, pdown, pleft, pb, pa, pselect, pstart};
byte keys[] = {key_a, key_b, key_c, key_d, key_e, key_f, key_g, key_h};
#define numbuttons sizeof(buttons)
//how far accelerometer tilted before controller starts moving mouse:
const int cintmovementthreshold = 18;
//the average 0 acceleration values read accelerometer each axis:
const int cintzeroxvalue = 328;
const int cintzeroyvalue = 328;
const int cintzerozvalue = 328;
//the maximum (positive) acceleration values read accelerometer each axis:
const int cintmaxxvalue = 396;
const int cintmaxyvalue = 396;
const int cintmaxzvalue = 396;
//the minimum (negative) acceleration values read accelerometer each axis:
const int cintminxvalue = 256;
const int cintminyvalue = 256;
const int cintminzvalue = 256;
//the sign of mouse movement relative acceleration:
const int cintxsign = -1;
const int cintysign = -1;
const int cintzsign = 1;
//const float cfloatmovementmultiplier = 1;
//the maximum speed in each axis (x , y) cursor should move:
const int cintmaxmousemovement = 15;
//this reduces 'twitchiness' of cursor calling delay function @ end of main loop.
const int cintmousedelay = 6;
void setup() {
//analog reference, use default
analogreference( default );
//setup pin modes.
pinmode( pinled, output );
//enable pullup resitor on pins.
for (byte i=0; i< numbuttons; i++) {
pinmode(buttons[i], input_pullup);
}
}
void loop() {
//determine if chip used mouse or controller
if (digitalread(buttons[pstart]) == 0 && digitalread(buttons[pselect]) ==0) {
//process accelerometer make cursor move.
fcnmouse();
}
else {
//progess nes controller buttons send keystrokes.
fcncontroller();
}
//delay avoid 'twitchiness' , bouncing inputs due fast of sampling.
delay(cintmousedelay);
}
//function process acclerometer mouse , send clicks
void fcnmouse()
{
//initialize values mouse cursor movement.
int intmousexmovement = 0;
int intmouseymovement = 0;
//read dummy analog channel must done first because x analog channel first , unstable, dropped or pegged periodically regardless of pin or source.
analogread( pandumb );
//read accelerometer readings
int intanalogxreading = analogread(panxin);
int intanalogyreading = analogread(panyin);
int intanalogzreading = analogread(panzin);
//calculate mouse movement if analog x reading ouside of 0 threshold...
if( cintmovementthreshold < abs( intanalogxreading - cintzeroxvalue ) ) {
//...calculate x mouse movement based on how far x acceleration 0 value.
intmousexmovement = cintxsign * ( ( ( (float)( 2 * cintmaxmousemovement ) / ( cintmaxxvalue - cintminxvalue ) ) * ( intanalogxreading - cintminxvalue ) ) - cintmaxmousemovement );
}
else {
//within 0 threshold, cursor not move in x.
intmousexmovement = 0;
}
//if analog y reading outside of 0 threshold...
if( cintmovementthreshold < abs( intanalogyreading - cintzeroyvalue ) ) {
//...calculate y mouse movement based on how far y acceleration 0 value.
intmouseymovement = cintysign * ( ( ( (float)( 2 * cintmaxmousemovement ) / ( cintmaxyvalue - cintminyvalue ) ) * ( intanalogyreading - cintminyvalue ) ) - cintmaxmousemovement );
//it use improvement, making trigonometric.
}
else {
//within 0 threshold, cursor not move in y.
intmouseymovement = 0;
}
mouse.move(intmousexmovement, intmouseymovement);
// //left , right click buttons
//
// if (digitalread(buttons[pb])==0) {
//
// mouse.click(1,0,0);
//
// }
//
// else if (digitalread(buttons[pa])==0) {
//
// mouse.click(0,0,1);
//
// }
//
// else {mouse.click(0,0,0);
//
// }
}
//function process buttons nes controller
void fcncontroller() {
static long currentkey = 0;
byte nothingpressed = 1;
// run through buttons
for (byte = 0; < numbuttons; i++) {
// of them pressed?
if (! digitalread(buttons[i])) {
nothingpressed = 0; // @ least 1 button pressed!
// if new button, release old one, , press new one
if (currentkey != keys[i]) {
keyboard.set_key1(0);
keyboard.send_now();
keyboard.set_key1(keys[i]);
currentkey = keys[i];
keyboard.send_now();
delay(keydelay);
} else {
// same button pressed, repeat!
keyboard.set_key1(keys[i]);
keyboard.send_now();
delay(keyrepeat);
}
}
}
if (nothingpressed) {
// release keys
keyboard.set_key1(0);
keyboard.send_now();
}
}
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > NES controller w/ Accelerometer...
arduino
Comments
Post a Comment