turning dc motor on/off with joystick module switch

im trying to make a dc motor move dependent on if i press the switch built into the joystick or not. heres the code:

#include <Servo.h>

// Servo 1
Servo servo1;
const int servo1Pin = 9;
int startPos1 = 90;

// Servo 2
Servo servo2;
const int servo2Pin = 5;
int startPos2 = 90;

// Joystick

int x_akse = A0;
int y_akse = A1;
int x_Pos;
int y_Pos;
int SW_pin = 2;

//DC motor
int Motor = 10;

void setup() {
// put your setup code here, to run once:
servo1.attach (servo1Pin);
servo2.attach (servo2Pin);
pinMode (x_akse, INPUT);
pinMode (y_akse, INPUT);
servo1.write (startPos1);
servo2.write (startPos2);

pinMode (SW_pin, INPUT);
digitalWrite (SW_pin, HIGH);

pinMode (Motor, OUTPUT);
digitalWrite (Motor, LOW);

}

void loop() {
// put your main code here, to run repeatedly:
x_Pos = analogRead (x_akse);
y_Pos = analogRead (y_akse);

// x akse loop
if (x_Pos < 300){
if (startPos1 < 10);
{
}
}
else { //condition that if the value for the horizontal position is less than 300, then the first servo will move towards the right.

startPos1 = startPos1 + 1; //x gå mot venstre
servo1.write (startPos1);
delay (5);
}

if (x_Pos > 700){
if (startPos1 > 180);
{
}
}
else{
startPos1 = startPos1 - 1; //x gå mot høgre
servo1.write (startPos1);
delay (5);
}
// y akse loop
if (y_Pos < 300){
if (startPos2 < 10);
{
}
}
else{
startPos2 = startPos2 - 1; //y gå mot opp
servo2.write (startPos2);
delay (5);
}

if (y_Pos > 700) {
if (startPos2 > 180);
{
}
}
else{
startPos2 = startPos2 + 1; // y gå ned
servo2.write (startPos2);
delay(5);
} //This is where i need help:
SW_pin = digitalRead (2);

if (digitalRead (SW_pin = HIGH)){ // when the switch is HIGH/not pressed, the motor is low
digitalWrite (SW_pin, HIGH);
digitalWrite (Motor, LOW);

}
else{
digitalWrite (SW_pin, LOW);
digitalWrite (Motor, HIGH); // when the switch is LOW/pressed, the motor is high/turned on…what have i done wrong? thank you
}

}

And here is your code Auto Formatted in the IDE and posted here in code tags as advised in Read this before posting a programming question

Do you see how much easier it is to read and copy for examination in an editor ?

#include <Servo.h>

// Servo 1
Servo servo1;
const int servo1Pin = 9;
int startPos1 = 90;

// Servo 2
Servo servo2;
const int servo2Pin = 5;
int startPos2 = 90;

// Joystick

int x_akse = A0;
int y_akse = A1;
int x_Pos;
int y_Pos;
int SW_pin = 2;

//DC motor
int Motor = 10;

void setup()
{
  // put your setup code here, to run once:
  servo1.attach (servo1Pin);
  servo2.attach (servo2Pin);
  pinMode (x_akse, INPUT);
  pinMode (y_akse, INPUT);
  servo1.write (startPos1);
  servo2.write (startPos2);
  pinMode (SW_pin, INPUT);
  digitalWrite (SW_pin, HIGH);
  pinMode (Motor, OUTPUT);
  digitalWrite (Motor, LOW);
}

void loop()
{
  // put your main code here, to run repeatedly:
  x_Pos = analogRead (x_akse);
  y_Pos = analogRead (y_akse);
  // x akse loop
  if (x_Pos < 300)
  {
    if (startPos1 < 10);
    {
    }
  }
  else                           //condition that if the value for the horizontal position is less than 300, then the first servo will move towards the right.
  {
    startPos1 = startPos1 + 1; //x gå mot venstre
    servo1.write (startPos1);
    delay (5);
  }
  if (x_Pos > 700)
  {
    if (startPos1 > 180);
    {
    }
  }
  else
  {
    startPos1 = startPos1 - 1; //x gå mot høgre
    servo1.write (startPos1);
    delay (5);
  }
  // y akse loop
  if (y_Pos < 300)
  {
    if (startPos2 < 10);
    {
    }
  }
  else
  {
    startPos2 = startPos2 - 1; //y gå mot opp
    servo2.write (startPos2);
    delay (5);
  }
  if (y_Pos > 700)
  {
    if (startPos2 > 180);
    {
    }
  }
  else
  {
    startPos2 = startPos2 + 1; // y gå ned
    servo2.write (startPos2);
    delay(5);
  }                                                //This is where i need help:
  SW_pin = digitalRead (2);
  if (digitalRead (SW_pin = HIGH))       // when the switch is HIGH/not pressed, the motor is low
  {
    digitalWrite (SW_pin, HIGH);
    digitalWrite (Motor, LOW);
  }
  else
  {
    digitalWrite (SW_pin, LOW);
    digitalWrite (Motor, HIGH);      // when the switch is LOW/pressed, the motor is high/turned on...what have i done wrong? thank you
  }
}

As to your problem

int SW_pin = 2;

Presumably the switch is attached to pin 2

Later in the code you do

  SW_pin = digitalRead (2);
  if (digitalRead (SW_pin = HIGH))       // when the switch is HIGH/not pressed, the motor is low

First you change the value of SW_PIN then use the wrong syntax for digitalRead() using the new value and finish off by assigning yet another value to SW_PIN instead of comparing it using ==

After that I stopped looking

I'm sorry for not Auto Formatting in IDE, and also apologies for being a bit bad at programming. I'm new to this, and have clearly not understood fully how it works. Thank you for helping with some of it :slight_smile:

Declare 2 variables, on named switchPin and another named switchState
Use digitalRead() to read the state of switchPin and put the state returned in switchState. Then you can test the value of switchState to determine whether the switch is open or closed. If you set the switch pin mode to INPUT_PULLUP and wire it to go to GND when closed then it will always be in a known state and its state will be LOW when closed