I am fairly new to Arduino and recently I have been working on a crane project using 2 servo motors(non-continuous, max 180 degrees) and a joystick to control them. So far, I have been able to control the crane’s x-axis and y-axis motion, but cannot achieve the following…
When I move the joystick and suddenly let go, the crane moves back to its neutral/initial position.I know this is because the movement of the joystick, when let go, is measured in the code and writes the values that this movement gives out. I am not quite sure how to solve this problem.
I my crane to be able to record its position each time it moves, so that when I press down on the joystick’s momentary switch, the crane will move back to that its last position.
3.My crane’s x and y movements are very fast.I have tried increasing the delay in my code, but this only delays the time it writes the joystick’s values.
My current code is attached below.If anyone is able to help me I would greatly appreciate it
Servo Horz, Vert; // create servo object for controlling a servo
int ServoX = A0; // analog pin used to connect the X - axis of Joystick
int ServoY = A1; // analog pin used to connect the Y - axis of Joystick
int x, y; // variables to read the values from the analog pins
Horz.attach(9); // attaches the horizontal motion servo on pin 9 to the servo object
Vert.attach(10); // attaches the vertical motion servo on pin 10 to the servo object
//display all read values to serial monitor
/* Horizontal and Vertical control of 2 servos*/
x = ServoX; // reads the value of the Joystick’s X - axis (value between 0 and 1023)
y = ServoY; // reads the value of the Joystick’s Y - axis (value between 0 and 1023)
x = map(analogRead(ServoX), 0, 1023, 0, 179); // map analog values from 0 to 1023
y = map(analogRead(ServoY), 0, 1023, 179, 0); // map analog values from 0 to 1023
Horz.write(x); // sets the servo position according to the scaled value
delay(15); // waits for the servos to get there