Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1.  
  2. #include <Servo.h>
  3.  
  4.  
  5. //Left button click records position
  6. //Right button click plays through recorded positions
  7. //Left button hold will chomp chomp the claw
  8. //Right button hold will restart recording from beginning
  9.  
  10. const int SERVOS = 4;
  11. int PIN[SERVOS] = {11,10,6,9}; //R,L,claw,base
  12. int ANGLE[SERVOS] = {90,90,90,90};
  13. int MIN[SERVOS] = {40,10,0,0};
  14. int MAX[SERVOS] = {165,165,165,165};
  15. int POTPIN[SERVOS] = {A5,A4,A3,A2};
  16. int POTZERO[SERVOS];
  17. int buttonL = A1, buttonR = A0;
  18. int buttonLheld = 0, buttonRheld = 0;
  19. int clickThresh = 4, holdThresh = 50;
  20. Servo servo[SERVOS];
  21. int potdeadzone = 200;
  22. int maxlrdiff = 100;
  23. int minlrdiff = -15;
  24. int maxAutoActions = 100;
  25. int autoAction[100][SERVOS];
  26. int actionIndex = 0;
  27. boolean learnMode = 1;
  28. boolean didmove = 0;
  29.  
  30. void setup()
  31. {
  32. pinMode(buttonL, INPUT_PULLUP); //to memorize
  33. pinMode(buttonR, INPUT_PULLUP); //to start actions
  34. for (int i = 0; i < SERVOS; i++){
  35. servo[i].attach(PIN[i]);
  36. servo[i].write(ANGLE[i]);
  37. pinMode(POTPIN[i],INPUT);
  38. POTZERO[i]=analogRead(POTPIN[i]);
  39. }
  40. }
  41.  
  42. void loop()
  43. {
  44. delay(20);
  45. if (!digitalRead(buttonL)) {
  46.  
  47. if (buttonLheld == holdThresh) {
  48. //L hold action
  49. servo[2].write(MIN[2]);
  50. delay(100);
  51. servo[2].write(MAX[2]);
  52. delay(100);
  53. servo[2].write(MIN[2]);
  54. delay(100);
  55. servo[2].write(ANGLE[2]);
  56. buttonLheld++;
  57. }
  58. if (buttonLheld < holdThresh) {
  59. buttonLheld++;
  60. }
  61. }else{
  62. if (buttonLheld > clickThresh && buttonLheld < holdThresh) {
  63. //L click action
  64.  
  65. if (learnMode == 1){
  66. if (actionIndex < maxAutoActions){
  67. for(int i = 0; i < SERVOS; i++){
  68. autoAction[actionIndex][i] = ANGLE[i];
  69. }
  70. actionIndex++;
  71. if (actionIndex == maxAutoActions) learnMode = 0;
  72. }
  73. }
  74. }
  75. buttonLheld=0;
  76. }
  77. if (!digitalRead(buttonR)) {
  78.  
  79. if (buttonRheld == holdThresh) {
  80. //R hold action
  81. actionIndex=0;
  82. learnMode = 1;
  83. maxAutoActions = 100;
  84. buttonRheld++;
  85. }
  86. if (buttonRheld < holdThresh) {
  87. buttonRheld++;
  88. }
  89. }else{
  90. if (buttonRheld > clickThresh && buttonRheld < holdThresh){
  91. if(learnMode == 1) {
  92. //R click action
  93. learnMode = 0;
  94. maxAutoActions = actionIndex-1 ;
  95. actionIndex = 0;
  96. }
  97. servo[2].write(5);
  98. delay(500);
  99. servo[2].write(ANGLE[2]);
  100. actionIndex++;
  101. }
  102. buttonRheld=0;
  103. }
  104.  
  105.  
  106. if (learnMode == 1){
  107. readpots();
  108. }else{
  109. playback();
  110. }
  111. }
  112.  
  113. void readpots(){
  114. for (int i = 0; i < SERVOS; i++){
  115. if (analogRead(POTPIN[i]) > POTZERO[i] + potdeadzone) ANGLE[i]=ANGLE[i]+1;
  116. if (analogRead(POTPIN[i]) < POTZERO[i] - potdeadzone) ANGLE[i]=ANGLE[i]-1;
  117. moveit(i);
  118. }
  119. }
  120.  
  121. void playback(){
  122. didmove=0;
  123. if (actionIndex > maxAutoActions) actionIndex = 0;
  124. for (int i = 0; i < SERVOS; i++){
  125. if ( ANGLE[i] > autoAction[actionIndex][i]) {
  126. ANGLE[i]--;
  127. didmove=1;
  128. }
  129. if ( ANGLE[i] < autoAction[actionIndex][i]) {
  130. ANGLE[i]++;
  131. didmove=1;
  132. }
  133. moveit(i);
  134. }
  135. if (didmove == 0) {
  136. actionIndex++;
  137. }
  138.  
  139. }
  140.  
  141. void moveit(int i){
  142. //servo 1 and 2 have a motion relationship and cannot exceed a certain distance between eachothers values
  143. //keep in mind they are inverted in orientation
  144. if (i == 0 || i == 1) {
  145.  
  146. if (ANGLE[1] > 180-ANGLE[0] + maxlrdiff) ANGLE[1] = 180-ANGLE[0] + maxlrdiff;
  147. if (ANGLE[1] < 180-ANGLE[0] +minlrdiff) ANGLE[1] = 180-ANGLE[0] +minlrdiff;
  148.  
  149. }
  150. //contrain the min and max servo values
  151. if (ANGLE[i] > MAX[i]) ANGLE[i] = MAX[i];
  152. if (ANGLE[i] < MIN[i]) ANGLE[i] = MIN[i];
  153. servo[i].write(ANGLE[i]);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement