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