Advertisement
cisco404

kode program untuk mengontrol motor servo

May 27th, 2024
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | Source Code | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo myservo;  // create servo object to control a servo
  4.  
  5. // -------------------------------------------
  6. // kode program untuk mengontrol motor servo
  7. // www.ardukode.blogspot.com
  8. // -------------------------------------------
  9.  
  10. int potpin = 0;  // analog pin used to connect the potentiometer
  11. int val;    // variable to read the value from the analog pin
  12.  
  13. void setup() {
  14.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  15. }
  16.  
  17. void loop() {
  18.   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  19.   val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  20.   myservo.write(val);                  // sets the servo position according to the scaled value
  21.   delay(15);                           // waits for the servo to get there
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement