#include <Servo.h>
//Joystick pin definition and setup
#define resetPin 2
#define steeringPin A0
long pastTime;
long currentTime;
int current_position;
//Servo Motor setup
#define pulsePin 3
Servo M1;
void setup() {
// put your setup code here, to run once:
pinMode(resetPin, INPUT);
pinMode(steeringPin, INPUT);
M1.attach(pulsePin,544,2400);
current_position = 90;
M1.write(current_position);
pastTime = millis();
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(resetPin)==1){
M1.write(90);
current_position = 90;
pastTime = millis();
}
else{
if (analogRead(steeringPin)==0){
delay(50);
currentTime = millis();
current_position = current_position + 0.05*(currentTime - pastTime);
M1.write(current_position);
pastTime = currentTime;
}
else if (analogRead(steeringPin)==1023){
delay(50);
currentTime = millis();
current_position = current_position - 0.05*(currentTime - pastTime);
M1.write(current_position);
pastTime = currentTime;
}
else{
pastTime = millis();
}
}
}