I'm making a project in which I'll be able to control a couple of servos using a joy-stick module. I'm starting on the first servo, using the Y-axis of the joystick to control it. When I try it out with this code, the servo jolts a little at the start, and then turns 180 degrees the FIRST time I move the joystick. When I try to repeat this before resetting the Arduino, the servo does nothing. Here's m code:
#include <Servo.h>
Servo myservo;
Servo myservo2;
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup() {
myservo.attach(3);
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200); //USE 115200 BAUD
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
ServoUpdate1();
if (analogRead(Y_pin) < 100){
myservo.write(90);
delay(1);
myservo.write(180);
delay(1);
myservo.write(270);
delay(1);
myservo.write(0);
}
delay(500);
}
Thanks in advance, you guys are great.