Help me tringgering relay module with joystick

Hi
I'm working on 3 actuator cylinder with with arduino.
The way I operating it is conneting all three actuator on a 8ch relay module and trigger the relay module with joystick switch.
I want the actuator to work while I dragging the joystick and stops when I release it.
I can drag the joystick in 4 direction (up, down, right, left) But even if I write all the direction for the code, it seems like only one direction (the last condition for the code) is working.
Here's my code: I'd put only "right" and "left" for the testing.

#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(6, OUTPUT);       
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, 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>500)
  {
   Serial.println("RIGHT"); 
   digitalWrite(6,LOW);          
   digitalWrite(7,HIGH);  
   digitalWrite(8,LOW);           
   digitalWrite(9,HIGH);                    
  }
  

else {
   digitalWrite(6,LOW);          
   digitalWrite(7,LOW);                             
   digitalWrite(8,LOW);          
   digitalWrite(9,LOW);          
   digitalWrite(10,LOW);          
   digitalWrite(11,LOW);         
  }


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



  
  Serial.println(buttonState1);


  delay(100);

}

Like I said earlier, only last condition for the code (Left direction) is working. When I check arduino serial monitor, it seems recognize the Right input but relay module is not working like the other one does.
Please help me if someone knows what's the problem here.

Thank you

Please read the forum guide in the sticky post at the top of the forum section. This will tell you how to post your code correctly, and other important matters. Then please edit your post above and correct the use of code tags.

The } before this comment appears to be the end of loop(). The code after the comment is not part of any function, so would cause a compiler error.

Thank you for the advice.

I edited my post :smiley:

Thanks, that's a little better. What other things did you read about in the forum guide that you did not include in your post?

What does the output look like now? Does it print "RIGHT" when yValue is greater than 500? Does it print "LEFT" when yValue is less than 3? If so, it is working as designed and if the relays are not doing what you intended then it might be a wiring problem.

Thank you for the answer sir.
It printed all "LEFT" and "RIGHT".
I solved this problem by modifying some codes.

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