Standard Servo Motor with Pushbutton and Potentiometer
Control the direction and range of motion on a "standard", noncontinuous, servo motor with a push button.
Add a potentiometer for more control.
Ingredients:
1 pushbutton
1 potentiometer
1 standard servo motor (will not work with continuous servo motor)
1 arduino board
1 breadboard
1 100K resistor
jumper wire
int servoPin = 2; // Control pin for servo motor (digital port)
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 0; // Amount to pulse the servo
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
int inButton = 3; // choose the input pin for a pushbutton (digital port)
int valButton = 0; // variable for reading the pin status
int dtime = 50;
int potpin = 1; // choose the pin for the potentiometer (analog port)
int valpot = 500;
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pinMode(inButton, INPUT);
pulse = minPulse; // Set the motor position value to the minimum
Serial.begin(9600);
}
void loop() {
valpot = analogRead(potpin) + 500; // read the value from the sensor
Serial.println(valpot);
valButton = digitalRead(inButton); // read input value
if (valButton == HIGH){
int dtime = 150;
// pulse = 2300; // changing this number will change the position it will start at
pulse = valpot;
}
else{
pulse = 500; // changing this number will change the other position it will go to
int dtime = 0;
}
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
delay(dtime);
}
}
Comments (0)
You don't have permission to comment on this page.