Hi I want to build a crane that uses two motors and a joystick, but the motor speed is too fast that I can't pick anything up and put it down. How do I slow down the motor speed, for example increase by 1 degree each time. Also, how do I save the state so when I let go of the button it will not go to the original state from where it started.
#include <Servo.h>
Servo myservo;
Servo myservo2;
int joystickX = A2;
int joystickY = A3;
int X;
int Y;
int pos = 0;
int lastSwitchState = 0;
int lastystate = 0;
int lastxstate = 0;
void setup() {
Serial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
pinMode(2, INPUT);
pinMode(3, INPUT);
// put your setup code here, to run once:
for (int joystickX = 0; joystickX < 180; joystickX++);
delay(100);
for (int joystickY = 0; joystickY < 180; joystickY++);
delay(100);
}
void loop() {
int buttonState = digitalRead(joystickX);
if (buttonState != lastxstate) {
}
// if the state has changed, increment the counter
// if the current state is HIGH then the button went from off to on:
myservo.write(X);
X = analogRead(joystickX);
X = map(X, 0, 1023, 0, 180);
for (int X = 0; X < 180; X = X+1);
if (X != lastxstate){
X+1;
}
delay(100);
Serial.print(Y);
Y = analogRead(joystickY);
Y = map(Y, 0, 1023, 0, 180);
myservo2.write(Y);
delay(100);
analogWrite(3, Y / 6);
lastxstate = buttonState;
lastystate = buttonState;
}