bangnaga

Arduino Multi Servo Serial

Oct 16th, 2020 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. // Arduino Kontrol 2 servo menggunakan serial terminal
  2. // Perintah 1 to 4
  3. // Power servo harus menggunakan power external jika menggunakan multi servo
  4.  
  5. #include <Servo.h>
  6. Servo myservo;
  7. Servo myservo2;
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(9600);
  12.   myservo.attach(9);
  13.   myservo2.attach(10);
  14.   myservo.write(100);
  15.   myservo2.write(100);
  16.   delay(1000);
  17. }
  18.  
  19. void loop() {
  20.   if (Serial.available())
  21.   {
  22.     int state = Serial.parseInt();
  23.     if (state == 1)
  24.     {
  25.       bukaservo1();
  26.     }
  27.    if (state == 2)
  28.     {
  29.      bukaservo2();
  30.     }
  31.     if (state == 3)
  32.     {
  33.       tutupservo1();
  34.     }
  35.    if (state == 4)
  36.     {
  37.       tutupservo2();
  38.     }
  39.   }
  40.  
  41. }
  42. // Akhir Void Loop
  43.  
  44.  
  45. void bukaservo1(){
  46.   myservo.write(100);
  47. }
  48. void bukaservo2(){
  49.   myservo2.write(100);
  50. }
  51.  
  52. void tutupservo1(){
  53.   myservo.write(180);
  54. }
  55.  
  56. void tutupservo2(){
  57.   myservo2.write(180);
  58. }
  59.  
  60.  
Advertisement
Add Comment
Please, Sign In to add comment