Skip to content

Instantly share code, notes, and snippets.

@Swap-File
Last active March 22, 2024 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swap-File/f77639f6d774324f3b17462b9ce35999 to your computer and use it in GitHub Desktop.
Save Swap-File/f77639f6d774324f3b17462b9ce35999 to your computer and use it in GitHub Desktop.
#include <Servo.h>
Servo myservo;
int pos = 0;
#define DELAY_TIME_MS 30
void setup() {
myservo.attach(9);
}
void loop() {
myservo.attach(9);
for (pos = 1; pos <= 179; pos += 1) { // goes from 1 degrees to 179 degrees (don't go all the way from 0 to 180 on cheap servos)
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(DELAY_TIME_MS);
}
myservo.detach(); //detatch to prevent vibration on cheap servos during delay
delay(DELAY_TIME_MS * 180);
myservo.attach(9);
for (pos = 179; pos >= 1; pos -= 1) { // goes from 179 degrees to 1 degrees (don't go all the way from 0 to 180 on cheap servos)
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(DELAY_TIME_MS);
}
myservo.detach(); //detatch to prevent vibration on cheap servos during delay
delay(DELAY_TIME_MS * 180);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment