See the code in action on Tinkercad
#include <Servo.h>
Servo escapeKeyBox;
uint8_t analogPin = 0; // Set pin for servo
uint8_t servoPin = 9; // Set pin for servo
uint16_t val;
void setup() {
}
void loop() {
// Reading the AnalogPin for the servo position
val = analogRead(analogPin);
// 0-1023 pin value 0-180 Degree
val = map(val, 0, 1023, 0, 179);
servoAction(val);
}
void servoAction(int possition){
escapeKeyBox.attach(servoPin); // attaches the servo on pin 9 to the servo object
delay(15); // waits 15ms for the servo to connect
escapeKeyBox.write(possition); // tell servo to go to position in variable 'possition'
delay(30); // waits 30ms for the servo to reach the position
escapeKeyBox.detach(); // detaches the servo (saves power and removes gitter)
}
