I have a nice code that I found that works a standard servo one button 90 clockwise other button 90 anti clockwise.
Question as I have no room for a micro servo will a linear servo work the same ?
// We include the libraries.
#include <Servo.h> // Library for Servo commands. The library is present in the latest versions of the Arduino IDE.
// We declare the constants and variables.
Servo myservo; // We set the servomotor with the name myservo.
int Button_Sx; // We declare the variable for checking the state of the left button or anticlockwise movement.
int Button_Dx; // We declare the variable for checking the state of the right button or clockwise movement.
int Servo_Position = 0; // We assign the value of the servo starting position.
void setup () {
myservo.attach (3); // We set pin 3 as the pwm pin for controlling the servomotor.
pinMode (4, INPUT); // We set pin 4 connected to the left button as input.
pinMode (5, INPUT); // We set pin 5 connected to the right button as input.
myservo.write (Servo_Position); // Position the servomotor at every start in position 0.
}
// The program repeats itself in an infinite loop.
void loop () {
Button_Sx = digitalRead (4); // Reads the logical value of pin 4 and assigns this value to the variable of the state of the left button.
Button_Dx = digitalRead (5); // Reads the logical value of pin 5 and assigns this value to the variable of the status of the right button.
// Check that the buttons are not in the same logical state. (Executes the increment instructions if only one button is pressed).
if (Button_Sx! = Button_Dx) {
if (Button_Sx == HIGH) {
Servo_Position = --Servo_Position; // Decrements the value of the variable.
}
if (Button_Dx == HIGH) {
Servo_Position = ++ Servo_Position; // Increase the value of the variable.
}
myservo.write (Servo_Position); // Move the servomotor to the position corresponding to the value of the variable.
delay (10); // Delay in milliseconds to stabilize the system.
}
}
Linear servo .......