Help me eliminating diagonal input when using joystick

Hi

I'm working on joystick to trigger relay module for activating three actuator cylinders. The Arduino code is based on the joystick coordinate.

In my code, the cylinder doesn't take any actions when the joystick is located on the middle (Println:"Norm"). When the joystick slides in RIGHT, LEFT, DOWN, UP direction, the cylinder moves in specific direction(Println: "Right", "Left"," Down"," Up") as I'd set up.

But then the problem is when the joystick slides on the diagonal direction, it triggers combination input. For example, when the joystick slides between right and down direction, the right and down movement working together(Println:"Right Down").

I wish I can eliminate the diagonal input or include the diagonal coordinate in the "Norm" input.
Since I'm not good at coding, I can't think of any solution to this problem.
Please help me if you can solve this problem.

Thank you.

here's my code:


#define joyX A1
#define joyY A0

int button=4;
int buttonState = 0;
int buttonState1 = 0;



void setup() {
  
 
  pinMode(button,INPUT);
  digitalWrite(button, HIGH);
  Serial.begin(9600);
  
  pinMode(8, OUTPUT);       
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  
}

void loop() {

  int xValue = analogRead(joyX);
  int yValue = analogRead(joyY);
    Serial.print(xValue);
    Serial.print("\t");
    Serial.println(yValue);

    buttonState = digitalRead(button);
    Serial.println(buttonState);

//RIGHT
  
    if  (yValue>550 )
  {
   Serial.println("RIGHT"); 
   digitalWrite(8,LOW);          
   digitalWrite(9,HIGH);  
   digitalWrite(10,LOW);           
   digitalWrite(11,HIGH);                    
  }
  

    if  (xValue<400 && xValue>600 && yValue>500 &&  yValue<300)
  {
   Serial.println("Norm"); 
   digitalWrite(8,LOW);          
   digitalWrite(9,LOW);                             
   digitalWrite(10,LOW);          
   digitalWrite(11,LOW);          
   digitalWrite(12,LOW);          
   digitalWrite(13,LOW);         
  } 


//Left

    if (yValue<6)
  {
   Serial.println("LEFT"); 
   digitalWrite(8,HIGH);          
   digitalWrite(9,LOW);  
   digitalWrite(10,HIGH);           
   digitalWrite(11,LOW);  
                           
  }
  

   if  (xValue<550 && xValue>500 && yValue>450 &&  yValue<460)
  {
   Serial.println("Norm"); 
   digitalWrite(8,LOW);          
   digitalWrite(9,LOW);                             
   digitalWrite(10,LOW);          
   digitalWrite(11,LOW);          
   digitalWrite(12,LOW);          
   digitalWrite(13,LOW);              
  } 

  //Up

    if (xValue>700)
  {
   Serial.println("UP"); 
   digitalWrite(12,LOW);          
   digitalWrite(13,HIGH);  
   digitalWrite(10,HIGH);           
   digitalWrite(11,LOW);  
                           
  }
  
    if  (xValue<550 && xValue>500 && yValue>450 &&  yValue<460)
  {
   Serial.println("Norm"); 
   digitalWrite(8,LOW);          
   digitalWrite(9,LOW);                             
   digitalWrite(10,LOW);          
   digitalWrite(11,LOW);          
   digitalWrite(12,LOW);          
   digitalWrite(13,LOW);         
  } 

  //Down

    if (xValue<300)
  {
   Serial.println("DOWN"); 
   digitalWrite(8,HIGH);          
   digitalWrite(9,LOW);  
   digitalWrite(12,HIGH);           
   digitalWrite(13,LOW);  
                           
  }
  

    if  (xValue<550 && xValue>400 && yValue>400 &&  yValue<460)
  {
   Serial.println("Norm"); 
   digitalWrite(8,LOW);          
   digitalWrite(9,LOW);                             
   digitalWrite(10,LOW);          
   digitalWrite(11,LOW);          
   digitalWrite(12,LOW);          
   digitalWrite(13,LOW);         
  } 

  if (buttonState == LOW)

  {

    Serial.println("Switch = High");
   digitalWrite(8,LOW);          
   digitalWrite(9,HIGH);                             
   digitalWrite(10,LOW);          
   digitalWrite(11,HIGH);          
   digitalWrite(12,LOW);          
   digitalWrite(13,HIGH);         
  }

  else{//digitalWrite(4, LOW);

    }
  Serial.println(buttonState1);



}

It's up to you to decide what such input shall mean. Why is it forbidden?

It may help if you provide more information about the purpose of your three actuators.

raise your thresholds and make decisions exclusive

shouldn't the thresholds be near the 20/80% values in each direction? instead of > 500, shouldn't it be 800?

in one case you use < 6. that's extreme. why not < 200?

every office i've had has a black/white board for diagramming things

You have to account for the other axis when determining what is "up", "down", "left" or "right"
currently you have them defined as this:

y >550 = right,
y<6 = left,
x>700 = up,
x<300 = down

should be something like this:

if (y>550 && x > 400  && x < 600) // right

in other words if y = right and x is somewhere in the middle then it must evaluate as "right"

Your "norm" condition should be the default position. That means if it is not up,down,right or left then it is "norm". To do that you can use the if/ else if / else structure.

if("up") {
do up stuff
}
else if ("down") {
do "down" stuff
}
else if ("left") {
do "left" stuff
}
else if ("right") {
do "right" stuff
}
else {
do "norm" stuff
}

do you really need to check that the opposite axis is near the middle or simply not exceeding one of its thresholds

the mutual exclusion may also help but shouldn't be necessary

    if (ThreshRight < x)
        ...
    else if (ThreshLeft > x)
        ...
    else if (ThreshUp < y)
        ...
    else if (ThreshDown > y)
        ...

When dealing with analog values I typically leave a "dead zone" as a form of hysteresis to prevent "toggling" of the output. It may not be needed, same with the "mutual exclusion" in both cases I feel it is more robust. YMMV

Simple solution:

if (abs(x) > abs(y))
  //horizontal
else
  //vertical

of course.
that's why there i suggested thresholds at 20/80% and not simply x > 512 and x < 512

The purpose of the actuator is to tilting a board on the top like a simulator.
Got it solved now

Thanks

I added "else if" and it works like a charm !

Thank You very much !

Thank you for the help !

It helped me a lot :slightly_smiling_face:

In dei mum

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.