Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <CheapStepper.h>
  2. #include <Servo.h>
  3.  
  4. CheapStepper stepper (8, 9, 10, 11);
  5. Servo myservo;
  6.  
  7. // here we declare our stepper using default pins:
  8. // arduino pin <--> pins on ULN2003 board:
  9. // 8 <--> IN1
  10. // 9 <--> IN2
  11. // 10 <--> IN3
  12. // 11 <--> IN4
  13.  
  14. // let's create a boolean variable to save the direction of our rotation
  15. //int verticleAngle = 75;
  16. boolean moveClockwise = true;
  17. boolean moveAntiClockwise = false;
  18. int servoAngle = 30 ;
  19. const int maxAngle = 75;
  20. const int minAngle = 30;
  21.  
  22. void setup() {
  23.  
  24.  
  25.  
  26. // let's just set up a serial connection and test print to the console
  27. Serial.begin(9600);
  28. myservo.attach(6);
  29. }
  30.  
  31. void loop() {
  32.  
  33. // let's move a full rotation (4096 mini-steps)
  34. // we'll go step-by-step using the step() function
  35. // myservo.write(45);
  36. // for(int i = 0;i < 1024;i++){
  37. // stepper.step(moveClockwise);
  38. // }
  39. // myservo.write(90);
  40. // for(int i = 0;i < 1024;i++){
  41. // stepper.step(moveAntiClockwise);
  42. // }
  43. int topLeft = analogRead(A2);
  44. int topRight = analogRead(A3);
  45. int botLeft = analogRead(A0);
  46. int botRight = analogRead(A1);
  47.  
  48. int valueReadedMean = (topLeft + topRight + botLeft + botRight) / 4;
  49. int threshold = valueReadedMean + 50;
  50.  
  51. if ((topLeft > threshold) | (topRight > threshold) | (botLeft > threshold) | (botRight > threshold)) {
  52. if (((topLeft > threshold) | (topRight > threshold)) & servoAngle > minAngle) {
  53. servoAngle -= 5;
  54. myservo.write(servoAngle);
  55. }
  56.  
  57. else if (((botLeft > threshold) | (botRight > threshold)) & servoAngle < maxAngle ) {
  58. servoAngle += 5;
  59. myservo.write(servoAngle);
  60. }
  61.  
  62. if ((topLeft > threshold) | (botLeft > threshold)) {
  63. for (int i = 0; i < 256; i++) {
  64. stepper.step(moveAntiClockwise);
  65. }
  66. }
  67.  
  68. else if ((topRight > threshold) | (botRight > threshold)) {
  69. for (int i = 0; i < 256; i++) {
  70. stepper.step(moveClockwise);
  71. }
  72. }
  73.  
  74. Serial.print("topLeft: ");
  75. Serial.println(topLeft);
  76.  
  77. Serial.print("topRight: ");
  78. Serial.println(topRight);
  79.  
  80. Serial.print("botLeft: ");
  81. Serial.println(botLeft);
  82.  
  83. Serial.print("botRight: ");
  84. Serial.println(botRight);
  85.  
  86. Serial.println(servoAngle);
  87. Serial.println("=====================");
  88.  
  89. }
  90. }
  91.  
  92.  
  93.  
  94.  
  95. // if(topLeft >= 150){
  96. // if (servoAngle == 75){
  97. // servoAngle = 30;
  98. // myservo.write(30);
  99. // }
  100. // // VVV this only works if we do it UP DOWN LEFT RIGHT
  101. // // servoAngle+=5;
  102. // // myservo.write(verticleAngle);
  103. // for(int i=0;i<256;i++){
  104. // stepper.step(moveAntiClockwise);
  105. // }
  106. // }
  107. // else if(topRight >= 150){
  108. // if (servoAngle == 75){
  109. // servoAngle = 30;
  110. // myservo.write(30);
  111. // }
  112. // // servoAngle+=5;
  113. // // myservo.write(verticleAngle);
  114. // for(int i=0;i<256;i++){
  115. // stepper.step(moveClockwise);
  116. // }
  117. // }
  118. // else if(botLeft >= 150){
  119. // if (servoAngle == 30){
  120. // servoAngle = 75;
  121. // myservo.write(75);
  122. // }
  123. // // servoAngle-=5
  124. // // myservo.write(verticleAngle);
  125. // for(int i=0;i<256;i++){
  126. // stepper.step(moveAntiClockwise);
  127. // }
  128. // }
  129. // else if(botRight >= 150){
  130. // if (servoAngle == 30){
  131. // servoAngle = 75;
  132. // myservo.write(75);
  133. // }
  134. // // servoAngle-=5
  135. // // myservo.write(verticleAngle);
  136. // for(int i=0;i<256;i++){
  137. // stepper.step(moveClockwise);
  138. // }
  139. // }
  140. // //
  141. // // delay(200);
  142. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement