Just started learning - can someone help me refine this simple Joystick Code?

First of all Hello to everyone! I'm New! :wink:
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);     
}

Maybe see the code I suggested in this recent Thread

Can anyone tell if my approach is OK?

Does it work? That's the principal test.

Test early and often.

...R

Yes, it works properly. It might need some fine-tune on exact values to reduce input noise but it works.

My concern is - can it be done better/simpler/more efficient on "code side"?
Or is it good enough?

Or is it good enough?

If it works then it is good enough

What you might usefully do would be to put the "print to serial monitor" code in a function and call it when required. There is strictly no need to do this but it would be good practice at writing and using functions. You will need to pass the values to be printed to the function otherwise they will not be in scope.

As a further exercise you could put the printing of the variables and associated text in a function and call that with the text and variable to be printed as parameters. Doing so would reduce the number of lines in the sketch which some might regard as being better

Ok! Thank You very much for tips!
I'll get to "functions" a bit later :slight_smile:

I'll get to "functions" a bit later

Get to them soon and use them often