First of all Hello to everyone! I'm New!
I started using arduino just recently, and I'd like to use simple analog joystick to control stepper motors.
(That's the project that led me to Arduino in the first place).
Now - for pure learning purposes - I wrote this code for my joystick. I treated it's axes simply as two pots. I didn't want to use any ready examples nor libraries - first I want to understand what's going on "under the hood".
And here is my code - for some reason raw values from Y-axis are inverted on my joystick so I had to revert them somehow. Can anyone tell if my approach is OK? Can the same (or better) result can be achieved using even more simple, efficient and/or neater code? (Ignore "print to serial" section).
Thank You in advance!
 void setup() {
 Serial.begin(9600);
}
void loop() {
Â
 int raw_X = analogRead(A0);
 int raw_Y = analogRead(A1);
 int X_left;
 int X_right;
 int Y_down;
 int Y_up;
// assign ranges and deadzones
 X_left = map(raw_X, 490, 15, 0, -512);
 X_right = map(raw_X, 525, 1000, 0, 512);
 Y_down = map(raw_Y, 520, 1000, 0, -512);
 Y_up  = map(raw_Y, 490, 15, 0, 512);Â
 if (X_left > 0) (X_left = 0);
 if (X_right < 0) (X_right = 0);
 if (Y_down > 0) (Y_down = 0);
 if (Y_up  < 0) (Y_up  = 0);
// print to serial monitor;
Â
 Serial.print("raw X:");
 Serial.print(raw_X);
 Serial.print("  ");
 Serial.print("\t");
Â
 Serial.print("raw Y:");
 Serial.print(raw_Y);
 Serial.print("  ");
 Serial.print("\t");
Â
 Serial.print("Y up:");
 Serial.print(Y_up);
 Serial.print("  ");
 Serial.print("\t");
Â
 Serial.print("X right:");
 Serial.print(X_right);
 Serial.print("  ");
 Serial.print("\t");
Â
 Serial.print("Y down:");
 Serial.print(Y_down);
 Serial.print("  ");
 Serial.print("\t");
 Serial.print("X left:");
 Serial.print(X_left);
 Serial.print("  ");
 Serial.println("\t");
 delay(1); Â
}