Devices & Components
Arduino Uno Rev3
Servos (Tower Pro MG996R)
Project description
Code
servo_sweep
scratch
Controlling the angle of the sweep.
1/* Sweep 2 by BARRAGAN <http://barraganstudio.com> 3 This example code is in the public domain. 4 5 modified 8 Nov 2013 6 by Scott Fitzgerald 7 http://www.arduino.cc/en/Tutorial/Sweep 8*/ 9 10#include <Servo.h> 11 12Servo myservo; // create servo object to control a servo 13// twelve servo objects can be created on most boards 14 15int pos = 0; // variable to store the servo position 16int startpos = 30; // Starting degree set to 30 17int endpos = 150; // Ending degree set to 150 18 19void setup() { 20 myservo.attach(9); // attaches the servo on pin 9 to the servo object 21} 22 23void loop() { 24 for (pos = startpos; pos <= endpos; pos += 1) { // goes from 0 degrees to 180 degrees 25 // in steps of 1 degree 26 myservo.write(pos); // tell servo to go to position in variable 'pos' 27 delay(15); // waits 15ms for the servo to reach the position 28 } 29 for (pos = endpos; pos >= startpos; pos -= 1) { // goes from 180 degrees to 0 degrees 30 myservo.write(pos); // tell servo to go to position in variable 'pos' 31 delay(15); // waits 15ms for the servo to reach the position 32 } 33} 34
Comments
Only logged in users can leave comments