Advertisement
cisco404

mengontrol kecepatan motor DC Drone

May 2nd, 2024
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | Source Code | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo motor1; // Objek Servo untuk motor 1
  4. Servo motor2; // Objek Servo untuk motor 2
  5. Servo motor3; // Objek Servo untuk motor 3
  6. Servo motor4; // Objek Servo untuk motor 4
  7.  
  8. // -------------------------------------------
  9. // Program Mengontrol Kecepatan Motor DC Drone
  10. // www.ardukode.blogspot.com
  11. // -------------------------------------------
  12.  
  13. int throttle = 0; // Nilai throttle dari joystick atau remote control
  14. int pitch = 0; // Nilai pitch dari joystick atau remote control
  15. int roll = 0; // Nilai roll dari joystick atau remote control
  16. int yaw = 0; // Nilai yaw dari joystick atau remote control
  17.  
  18. void setup() {
  19.   motor1.attach(9); // Hubungkan motor 1 ke pin 9
  20.   motor2.attach(10); // Hubungkan motor 2 ke pin 10
  21.   motor3.attach(11); // Hubungkan motor 3 ke pin 11
  22.   motor4.attach(12); // Hubungkan motor 4 ke pin 12
  23. }
  24.  
  25. void loop() {
  26.   // Baca nilai throttle, pitch, roll, dan yaw dari joystick atau remote control
  27.  
  28.   // Hitung kecepatan motor berdasarkan nilai throttle, pitch, roll, dan yaw
  29.   int speed1 = throttle + pitch + roll + yaw;
  30.   int speed2 = throttle - pitch + roll - yaw;
  31.   int speed3 = throttle + pitch - roll - yaw;
  32.   int speed4 = throttle - pitch - roll + yaw;
  33.  
  34.   // Batasi kecepatan motor
  35.   speed1 = constrain(speed1, 0, 255);
  36.   speed2 = constrain(speed2, 0, 255);
  37.   speed3 = constrain(speed3, 0, 255);
  38.   speed4 = constrain(speed4, 0, 255);
  39.  
  40.   // Kontrol kecepatan motor
  41.   motor1.write(speed1);
  42.   motor2.write(speed2);
  43.   motor3.write(speed3);
  44.   motor4.write(speed4);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement