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);
}