Advertisement
safwan092

Untitled

Apr 16th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_PWMServoDriver.h>
  3.  
  4. int controlPins[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  5. int pos = 0;
  6.  
  7. Adafruit_PWMServoDriver board1 = Adafruit_PWMServoDriver(0x40);
  8.  
  9. #define SERVOMIN 125 // this is the 'minimum' pulse length count (out of 4096)
  10. #define SERVOMAX 575 // this is the 'maximum' pulse length count (out of 4096)
  11. int servoNumber = 0;
  12.  
  13. void setup() {
  14. Serial.begin(9600);
  15. Serial.println("32 channel Servo test!");
  16.  
  17. board1.begin();
  18. board1.setPWMFreq(60);
  19. //yield();
  20. }
  21.  
  22. void loop() {
  23. for (int i = 0; i < 12; i++) {
  24. loopMotor(controlPins[i]);
  25. delay(1000);
  26. }
  27. }
  28.  
  29. int angleToPulse(int ang) {
  30. int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX); // map angle of 0 to 180 to Servo min and Servo max
  31. Serial.print("Angle: "); Serial.print(ang);
  32. Serial.print(" pulse: "); Serial.println(pulse);
  33. return pulse;
  34. }
  35.  
  36.  
  37. void loopMotor(int motorPinNumber) {
  38. for (pos = 0; pos <= 180; pos += 1) {
  39. board1.setPWM(motorPinNumber, 0, angleToPulse(pos) );
  40. delay(15);
  41. }
  42. for (pos = 180; pos >= 0; pos -= 1) {
  43. board1.setPWM(motorPinNumber, 0, angleToPulse(pos) );
  44. delay(15);
  45. }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement