Controlling a 9g Servo Motor with joystick

I used a Dual axis Joystick To control a 9g Servo. The code works nicely but it randomly writes it's position to 0. How do I fix it?


#include <Servo.h>

int Y;
int X;

const int Y_pin = A0;
const int X_pin = A1;
int sw_pin = 9;

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos;    // variable to store the servo position

void setup() {
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object

  pinMode(X_pin,INPUT);
  pinMode(Y_pin,INPUT);
  pinMode(sw_pin,INPUT);

  myservo.write(0);

  Serial.begin(9600);
}

void Read_Xval() {
  X = analogRead(X_pin);
  return X;
}

void Read_Yval() {
  Y = analogRead(Y_pin);
  return Y;
}

void Move_Servo() {
  if (Y>650) {
    if (pos > 180; pos == 180) {
      pos = 180;
    }
    else {
      pos += 5;
    }
  }
  if (Y<450) {
    if (pos < 0; pos == 0) {
    }
    else {
      pos -= 5;
    }
  }
  myservo.write(pos);
  delay(50);
}

// ||||||||||||||||||||||||||| LOOP ||||||||||||||||||||
void loop() {
  Read_Xval();
  Read_Yval();
  Move_Servo();

  Serial.print("X: ");
  Serial.print(X);
  Serial.print(" Y: ");
  Serial.print(Y);
  Serial.print(" Pos: ");
  Serial.println(pos);

 delay(50);`
}

You should get a lot of compiler warnings with this code.

Aniway: you only set the postion to 0 after a reset, so make a Serial.println("RESET"); in your setup() and see if it occasionally reboots.

Ok, and these

if (pos > 180; pos == 180) {

do not what you think they do

Thanks To both of you Well In the new Code I haven't Used single capital Letters and Accordingly I have Print a Serial command on setup. And Code Works Very fine until You use it For around 15-30 Minutes. It starts hallucinating and The Serial Monitor Just Stops Working. or Prints Something like
X: 503 Y: 827 Pos: 155
"X��MQ5"*
X: 505 Y: 762 Pos: 5

#include <Servo.h>

int Y_J;
int X_J;

const int Y_pin = A0;
const int X_pin = A1;
int sw_pin = 9;

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos;    // variable to store the servo position

void setup() {
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object

  pinMode(X_pin,INPUT);
  pinMode(Y_pin,INPUT);
  pinMode(sw_pin,INPUT);

  myservo.write(0);

  Serial.begin(9600);
  Serial.println("RESET");
}

void Read_Xval() {
  X_J = analogRead(X_pin);
}

void Read_Yval() {
  Y_J = analogRead(Y_pin);
}

void Move_Servo() {
  if (Y_J>650) {
    if (pos > 180; pos == 180) {
      pos = 180;
    }
    else {
      pos += 5;
    }
  }
  if (Y_J<450) {
    if (pos < 0; pos == 0) {
      pos = 0;
    }
    else {
      pos -= 5;
    }
  }
  myservo.write(pos);
  delay(50);
}

// ||||||||||||||||||||||||||| LOOP ||||||||||||||||||||
void loop() {
  Read_Xval();
  Read_Yval();
  Move_Servo();

  Serial.print("X: ");
  Serial.print(X_J);
  Serial.print(" Y: ");
  Serial.print(Y_J);
  Serial.print(" Pos: ");
  Serial.println(pos);

  delay(50);
}

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