Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define deadzone_xbox 15 //center deadzone
- #define offset_xbox 7 //compensate for center on stick
- #define deadzone_ps3 7
- #define offset_ps3 2
- int vals[3]; //stoors the values, 1=steering position, 2=acceleration, 3=brake/reverse
- int pos, negOff, posOff, l2, r2;
- //The fallowing code works in a way to retain full range out of the sticks using deadzones. Each input range is calculated from the center offset+deadzone defined earlier to min and max values.
- //Corresponding adjustments are made for both >center position or < center position and added together. This way you can adjust the deadzone as much as you please and the value once you leave that deadzone will still be 1 instead of some higher value where the inputs are ignored until then.
- void calcXboxInput(){
- pos=Xbox.getAnalogHat(LeftHatX_XBOX)>>8; //value returned is 16 bit(but only 10 bit ADC) and since lower bits are noisy just concatonate number to upper 8 bits, could also devide here but bit shifting is much faster
- negOff=offset_xbox-deadzone_xbox;
- posOff=offset_xbox+deadzone_xbox;
- l2=Xbox.getButton(L2_XBOX);
- r2=Xbox.getButton(R2_XBOX);
- vals[0]=map(min(pos,negOff),negOff,-128,127,0)+map(max(pos,posOff),posOff,127,0,128);
- vals[1]=((l2>0)? l2: r2);
- vals[2]=((l2>0)? 1: 0);
- }
- void calcPs3Input(){
- pos=PS3.getAnalogHat(LeftHatX)-128;
- negOff=offset_ps3-deadzone_ps3;
- posOff=offset_ps3+deadzone_ps3;
- l2=PS3.getAnalogButton(L2_ANALOG);
- r2=PS3.getAnalogButton(R2_ANALOG);
- vals[0]=map(min(pos,negOff),negOff,-128,127,0)+map(max(pos,posOff),posOff,127,0,128);
- vals[1]=((l2>0)? l2: r2);
- vals[2]=((l2>0)? 1: 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement