Need help with code for reading analog stick

I wanted to make a custom controller for Intellivision. The disc is unusual in that it has 16 digital directions :fearful: while just about all other digital controller has only 8 so finding a generic 16 directional digital controller would probably be as easy as finding hen with teeth.

Analog controller is the obvious solution as it'd have 360 degree of control and with Arduino, hundred of directions when converted from analog to digital. Someone showed me a sample code:

// returns the direction from 1..16, or 0 if the joystick is not being used
uint8_t getDirectionFromAnalog(uint8_t x, uint8_t y)
{
	int8_t _x = x - 128;
	int8_t _y = y - 128;
	int8_t direction = (int8_t)(atan2(_y, _x) / M_PI * 8);
 
	if(direction <= 0)
		direction += 16;
 
	if(_x*_x + _y*_y > 64*64)
		return (uint8_t)direction;
	else
		return 0;		// joystick too close to centre
}

I'm having trouble figuring what to do with it. I'd need some help in cleaning the code so it'd work properly in Arduino and reading from 2 analog lines (any pin is fine, I can change if needed later) and also would it need #include math.h?

The function appears to take 2 analogue values (x and y) and convert them into a number between 1 and 16 (or zero if the joystick is centred). Try a test script to generate x and y values from two potentiometers or an analogue joystick if you have one.

the sketch is returning error:

error: return-statement with a value, in function returning 'void'

I think it was meant to be used as a library and not in main sketch? I never tried to create a library before.

the sketch is returning error:

What sketch?

AWOL:

the sketch is returning error:

What sketch?

This is what I have so far. I wanted to do serial print first just to test but until I can fix the return issue, serial is disabled to avoid spamming.

// returns the direction from 1..16, or 0 if the joystick is not being used
  #include <math.h>

  int x=0;
  int y=0;

void setup() {
  Serial.begin(9600);
}
void loop()  {
uint8_t getDirectionFromAnalog(uint8_t x, uint8_t y);
{
	int8_t _x = x - 128;
	int8_t _y = y - 128;
	int8_t direction = (int8_t)(atan2(_y, _x) / M_PI * 8);
 
	if(direction <= 0)
		direction += 16;
 
	if(_x*_x + _y*_y > 64*64){
		return (uint8_t)direction;
                //Serial.print ();
}
	else
{
		return 0;		// joystick too close to centre
}
}
}

edit: typo that passed spell check but changed the meaning of one sentence a bit

void loop()  {
uint8_t getDirectionFromAnalog(uint8_t x, uint8_t y);
{
	int8_t _x = x - 128;
	int8_t _y = y - 128;
	int8_t direction = (int8_t)(atan2(_y, _x) / M_PI * 8);
 
	if(direction <= 0)
		direction += 16;
 
	if(_x*_x + _y*_y > 64*64){
		return (uint8_t)direction;
                //Serial.print ();
}

You can't embed a function inside another function.

..but you can embed function prototypes, as was done (mistakenly) here.

#include <math.h>

int x=0;
int y=0;

void setup() 
{
  Serial.begin(9600);
}
void loop()  
{
  //need to do something with getDirectionFromAnalog here
}
uint8_t getDirectionFromAnalog(uint8_t x, uint8_t y)
{
  int8_t _x = x - 128;
  int8_t _y = y - 128;
  int8_t direction = (int8_t)(atan2(_y, _x) / M_PI * 8);

  if(direction <= 0)
    direction += 16;

  if(_x*_x + _y*_y > 64*64){
    return (uint8_t)direction;
    //Serial.print ();
  }
  else
  {
    return 0;		// joystick too close to centre
  }
}

That seems to work. Now I need to connect the analog controller and read the value into getDirectionFromAnalog