Created
July 30, 2018 13:16
-
-
Save hidsh/9351b545bab2d6708520f937f57db06e to your computer and use it in GitHub Desktop.
arduino test for zero adjust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define JOY_L_X A0 | |
#define JOY_L_Y A1 | |
#define ZERO_THRESH 10 | |
#define MAX 490 | |
void setup() { | |
pinMode(JOY_L_X, INPUT); | |
pinMode(JOY_L_Y, INPUT); | |
Serial.begin(38400); | |
} | |
short zero_adjust(short v) | |
{ | |
short v_abs, sign; | |
v_abs = abs(v); | |
sign = (v < 0)? -1:1; | |
if(v_abs > MAX) return MAX * sign; | |
if(v_abs < ZERO_THRESH) return 0; | |
return (v_abs - ZERO_THRESH) * sign; | |
} | |
void loop() { | |
static short lx = 0; | |
static short ly = 0; | |
char s[255]; | |
lx = lx * 0.9 + zero_adjust(analogRead(JOY_L_X) - 512) * 0.1; | |
ly = ly * 0.9 + zero_adjust(analogRead(JOY_L_Y) - 512) * 0.1; | |
sprintf(s, "X:%4d Y:%4d\n", lx, ly); | |
Serial.print(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment