Raqeeb_Alnakib

Full code scripts for Lesson #3 : Servo motor with Arduino

Mar 5th, 2020
4,499
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //   Support us on : https://global-prog.com
  3.  
  4.  
  5. #include <Servo.h>
  6. Servo myservo;       // create servo object to control a servo
  7. int pos=0;          // variable to store the servo position
  8.  
  9. void setup() {
  10.  
  11.   myservo.attach(3);  // attaches the servo object on pin 3
  12.  
  13. }
  14.  
  15. void loop(){
  16.  
  17.   for(pos=0;pos<=180;pos++){  //goes from 0 degrees to 180 degrees in steps of 1 degree
  18.     myservo.write(pos);      // tell servo to go to position in variable 'pos'
  19.      delay(25);           // waits 25ms for the servo to reach the position
  20.   }
  21.  
  22.   for(pos=180;pos>=0;pos--){ // goes from 180 degrees to 0 degrees in steps of 1 degree
  23.     myservo.write(pos);  // tell servo to go to position in variable 'pos'
  24.     delay(25);        // waits 25ms for the servo to reach the position
  25. }
  26.  
  27. }
RAW Paste Data