Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. // Make sure you have the Adafruit servo driver library installed >>>>> https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
  2. // X-axis joystick pin: A1
  3. // Y-axis joystick pin: A0
  4. // Trim potentiometer pin: A2
  5. // Button pin: 2
  6.  
  7.  
  8. #include <Wire.h>
  9. #include <Adafruit_PWMServoDriver.h>
  10.  
  11. Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
  12.  
  13. #define SERVOMIN 140 // this is the 'minimum' pulse length count (out of 4096)
  14. #define SERVOMAX 520 // this is the 'maximum' pulse length count (out of 4096)
  15.  
  16. #define trimMin 100 //min value for pot
  17. #define trimMax 240 //max value for pot
  18.  
  19. // our servo # counter
  20. uint8_t servonum = 0;
  21.  
  22. int xval;
  23. int yval;
  24.  
  25. int lexpulse;
  26. int rexpulse;
  27.  
  28. int leypulse;
  29. int reypulse;
  30.  
  31. int uplidpulse;
  32. int lolidpulse;
  33. int altuplidpulse;
  34. int altlolidpulse;
  35.  
  36. int mouthPulse1, mouthPulse2;
  37.  
  38. int trimval;
  39.  
  40. int LED=6;
  41. int MMode=4;
  42. int mmodestatus=0;
  43.  
  44. const int analogInPin = A0;
  45. int sensorValue = 0;
  46. int outputValue = 0;
  47. int switchval = 0;
  48.  
  49. void setup() {
  50. Serial.begin(9600);
  51. Serial.println("8 channel Servo test!");
  52. pinMode(analogInPin, INPUT);
  53. pinMode(2, INPUT); //Blinzeln
  54. pinMode(MMode, INPUT); //Mund-Mode
  55. pinMode(LED, OUTPUT); //LED
  56.  
  57. pwm.begin();
  58.  
  59. pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
  60.  
  61. delay(10);
  62. }
  63.  
  64. // you can use this function if you'd like to set the pulse length in seconds
  65. // e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
  66. void setServoPulse(uint8_t n, double pulse) {
  67. double pulselength;
  68.  
  69. pulselength = 1000000; // 1,000,000 us per second
  70. pulselength /= 60; // 60 Hz
  71. Serial.print(pulselength); Serial.println(" us per period");
  72. pulselength /= 4096; // 12 bits of resolution
  73. Serial.print(pulselength); Serial.println(" us per bit");
  74. pulse *= 1000000; // convert to us
  75. pulse /= pulselength;
  76. Serial.println(pulse);
  77.  
  78. }
  79.  
  80. void loop() {
  81.  
  82.  
  83. mmodestatus=digitalRead(MMode);
  84.  
  85. if (mmodestatus == HIGH)
  86. {
  87. digitalWrite(LED, HIGH);
  88. }
  89. else
  90. {
  91. digitalWrite(LED, LOW);
  92. }
  93. xval = analogRead(A1);
  94. lexpulse = map(xval, 0,1023, 220, 440);
  95. rexpulse = lexpulse;
  96.  
  97. switchval = digitalRead(2);
  98.  
  99.  
  100. yval = analogRead(A0);
  101. leypulse = map(1023 - yval, 0,1023, 250, 500);
  102. reypulse = map(1023 - yval, 0,1023, 400, 280);
  103.  
  104. trimval = analogRead(A2);
  105. trimval = map(trimval, 616, 932, -40, 40);
  106. //trimval = map(trimval, 320, 580, -40, 40);
  107.  
  108. uplidpulse = map(yval, 0, 1023, 400, 280);
  109. uplidpulse -= (trimval-40);
  110. uplidpulse = constrain(uplidpulse, 280, 400);
  111. altuplidpulse = 680-uplidpulse;
  112.  
  113. lolidpulse = map(yval, 0, 1023, 410, 280);
  114. lolidpulse += (trimval/2);
  115. lolidpulse = constrain(lolidpulse, 280, 400);
  116. altlolidpulse = 680-lolidpulse;
  117.  
  118.  
  119. pwm.setPWM(0, 0, lexpulse);
  120.  
  121. if (mmodestatus == HIGH){ //Change this to low if the button is the wrong way round
  122. pwm.setPWM(1, 0, leypulse);
  123. }
  124. else{
  125. mouthPulse1 = map(yval, 0, 1024, mouthMinOpen, mouthMaxOpen); //Replace 'mouthMinOpen' and 'mouthMaxOpen'
  126. mouthPulse2 = map(yval, 0, 1024, mouthMaxOpen, mouthMinOpen); //with desired numbers for the mouth range.
  127. pwm.setPWM(6, 0, mouthPulse1);
  128. pwm.setPWM(7, 0, mouthPulse2);
  129. = }
  130.  
  131. if (switchval == HIGH) {
  132. pwm.setPWM(2, 0, 400);
  133. pwm.setPWM(3, 0, 240);
  134. pwm.setPWM(4, 0, 240);
  135. pwm.setPWM(5, 0, 400);
  136. }
  137. else if (switchval == LOW) {
  138. pwm.setPWM(2, 0, uplidpulse);
  139. pwm.setPWM(3, 0, lolidpulse);
  140. pwm.setPWM(4, 0, altuplidpulse);
  141. pwm.setPWM(5, 0, altlolidpulse);
  142. }
  143. }
  144.  
  145. Serial.println(trimval);
  146.  
  147. delay(5);
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement