int JoyStick_X = A1; //x
const int relayPin1 = 8; // Relay 1 control pin
const int relayPin2 = 7; // Relay 2 control pin
const int stopLeftButton = 2; // Stop forward button pin
const int stopRightButton = 3; // Stop reverse button pin
const int middleX = 520; // the center value of the joystickX
const int middleY = 520; // the center value of the joystickY
int JoyStick_Y = A2; //y
const int relayPin3 = 10; // Relay 3 control pin
const int relayPin4 = 9; // Relay 4 control pin
const int stopUpButton = 4; // Stop upward button pin
const int stopDownButton = 5; // Stop downward button pin
void setup()
{
pinMode(JoyStick_X, INPUT_PULLUP);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(stopLeftButton, INPUT_PULLUP);
pinMode(stopRightButton, INPUT_PULLUP);
Serial.begin(9600);
pinMode(JoyStick_Y, INPUT_PULLUP);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
pinMode(stopUpButton, INPUT_PULLUP);
pinMode(stopDownButton, INPUT_PULLUP);
}
void loop()
{
int x; // x the variable for the position of the jostick on the horizontal axe
x=analogRead(JoyStick_X);
Serial.println(x);
if ((x > middleX + 100) and (digitalRead(stopLeftButton)== LOW)) // moving te joystick to the left
{
//Left direction <<<<<
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, LOW);
}
else if ((x < middleX - 100) and (digitalRead(stopRightButton) == LOW)) // moving te joystick to the right
{
// Right direction >>>>>
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, HIGH);
}
//else no action
else {
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}
delay(100);
int y;
y=analogRead(JoyStick_Y);
Serial.println(y);
if ((y > middleX + 100) and (digitalRead(stopUpButton)== LOW))
{
//👆 Up direction
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, LOW);
}
else if ((y < middleY - 100) and (digitalRead(stopDownButton) == LOW))
{
//👇 Down direction
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, HIGH);
}
// else no action
else {
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
// delay(250);
}
delay(250);
}