Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. #include <Stepper.h>
  2. const int stepPin = 6; //PUL -Pulse
  3. const int dirPin = 7; //DIR -Direction
  4. const int enPin = 8; //ENA -Enable
  5.  
  6. //Global Variables
  7.  
  8. double trayPosition = 0; //Counts number of Motor Spins completed
  9. // double trayEnd = 10; //Motor spins needed for 'out' position
  10.  
  11. double pusherPosition = 0;
  12. int trayStepsRev = 200;
  13. int trayFast = 200; // Adjust for our motor later
  14. int traySlow = 50;
  15.  
  16. Stepper pushMotor(2048, 13, 14, 15, 16); //Initialize push motor
  17.  
  18. bool StartButton = digitalRead(3); //Start button
  19.  
  20. bool ResetButton = digitalRead(4); //Reset Button
  21.  
  22. bool E_STOP = digitalRead(5); //E_STOP Button
  23.  
  24. bool LimitSwitch = digitalRead(6); //Tray Limit Switch
  25. bool pusherLimitSwitch = digitalRead(7); //Pusher Limit Switch
  26.  
  27. bool trayHome, pusherHome;
  28.  
  29. unsigned long trayStartTime, trayStopTime, trayRunTime; //Initialize trayRunTime once time value is known
  30.  
  31. unsigned long pusherStartTime, pusherStopTime, pusherRunTime;
  32.  
  33. void pushIn();
  34. void pushOut();
  35. void trayIn();
  36. void trayOut();
  37. void reset();
  38. void emergency();
  39.  
  40.  
  41.  
  42. void setup ()
  43. {
  44. //Input Pins
  45. pinMode(3, INPUT); //Start button
  46.  
  47. pinMode(4, INPUT); //Reset Button
  48.  
  49. pinMode(5, INPUT); //E_STOP
  50.  
  51. pinMode(6, INPUT); //Tray Limit Switch
  52.  
  53. pinMode(7, INPUT); //Pusher Limit Switch
  54.  
  55. //Output Pins
  56. pinMode(stepPin, OUTPUT);
  57. pinMode(dirPin, OUTPUT);
  58. pinMode(enPin, OUTPUT);
  59. digitalWrite(enPin, LOW);
  60.  
  61. trayStartTime = 0;
  62. trayStopTime = 0;
  63. trayRunTime = 5000; //5 seconds of tray run time
  64.  
  65. pushMotor.setSpeed(60); //Set speed to 60 rpm
  66.  
  67. Serial.begin(9600);
  68.  
  69. }
  70.  
  71. void trayIn()
  72. {
  73. if (!pusherHome) {
  74. pushIn();
  75. }
  76. while (LimitSwitch != HIGH)
  77. {
  78.  
  79. while (ResetButton != HIGH || E_STOP != HIGH) {
  80.  
  81. if (ResetButton == HIGH)
  82. {
  83. reset();
  84. break;
  85. }
  86. if (E_STOP == HIGH) {
  87. emergency();
  88. break;
  89. }
  90. else if (trayPosition <= 20)
  91. {
  92. for (int x = 0; x < 20 * trayStepsRev; x++) {
  93. if (trayPosition == 0) {
  94. trayHome = true;
  95. break;
  96. }
  97. digitalWrite(dirPin, LOW); //Motor spinning in reverse dir
  98. digitalWrite(stepPin, HIGH);
  99. delayMicroseconds(traySlow);;
  100. trayPosition--;
  101. }
  102. }
  103.  
  104. else
  105. {
  106. if (trayPosition == 0 || LimitSwitch == HIGH) {
  107. trayHome = true;
  108. break;
  109. }
  110. digitalWrite(dirPin, LOW); //Motor spinning in reverse dir
  111. digitalWrite(stepPin, HIGH);
  112. delayMicroseconds(trayFast);;
  113. trayPosition--;
  114.  
  115. }
  116. }//end while loop
  117. }//end limit switch loop
  118. } //end trayIn
  119.  
  120.  
  121. void trayOut()
  122. { while (trayPosition != 120) {
  123. while (E_STOP != HIGH || ResetButton != HIGH) {
  124. if (E_STOP == HIGH) {
  125. emergency();
  126. break;
  127. }
  128. else if (ResetButton == HIGH) {
  129. reset();
  130. }
  131. else {
  132. digitalWrite(dirPin, HIGH); //Forward Movement
  133. digitalWrite(stepPin, HIGH);
  134. delayMicroseconds(trayFast);
  135. trayPosition++;
  136.  
  137. }
  138. }//end while
  139. delay(5000);
  140. pushOut();
  141.  
  142. } //end trayOut
  143.  
  144. void pushIn(){
  145. while (pusherLimitSwitch != HIGH)
  146. {
  147. while (ResetButton != HIGH || E_STOP != HIGH) {
  148. if (ResetButton == HIGH)
  149. {
  150. reset();
  151. }
  152. else if (E_STOP == HIGH) {
  153. emergency();
  154. }
  155. else if (pusherPosition == 5)
  156. {
  157. for (int x = 0; x < 5; x++) {
  158. pushMotor.step(-slowDown);
  159. pusherPosition--;
  160. }
  161. }
  162. else
  163. {
  164. if (pusherPosition == 0 || pusherLimitSwitch == HIGH) {
  165. pusherHome = true;
  166. break;
  167. }
  168. pushMotor.step(-stepsPerRev); //Moving backwards
  169. pusherPosition --;
  170.  
  171. }
  172. }//end while
  173. }
  174. }// end function
  175.  
  176.  
  177. void pushOut() {
  178. while (pusherPosition != 20) {
  179. while (E_STOP != HIGH || ResetButton != HIGH) {
  180. if (E_STOP == HIGH) {
  181. emergency();
  182. break;
  183. }
  184. else if (ResetButton == HIGH) {
  185. reset();
  186. }
  187. else {
  188. pushMotor.step(stepsPerRev); //forward movement
  189. pusherPosition++;
  190.  
  191. }
  192.  
  193. }
  194.  
  195. void reset()
  196. {
  197. pushMotor.step(0);
  198. trayMotor.step(0);
  199. if (!pusherHome) {
  200. pushIn();
  201. }
  202. if (!trayHome) {
  203. trayIn();
  204. }
  205. }
  206.  
  207. void emergency() {
  208. while (ResetButton != HIGH || StartButton != HIGH) {
  209. pushMotor.step(0);
  210. trayMotor.step(0);
  211.  
  212. if (ResetButton == HIGH) {
  213. reset();
  214. }
  215. else if (StartButton != HIGH) {
  216.  
  217. if (pusherHome != true || pusherPosition != 20)
  218. {
  219. pushOut();
  220. }
  221.  
  222. if (trayHome != true || trayPosition != 120) {
  223. if (digitalRead(dirPin) == LOW) //Means tray Motor was moving back
  224. {
  225. trayIn();
  226. }
  227. else {
  228. trayOut();
  229. }
  230. }
  231.  
  232. }
  233. }//end while
  234. }//end function
  235.  
  236.  
  237. void loop ()
  238. {
  239. if (trayPosition == 0) {
  240. trayHome = true;
  241. }
  242. if (pusherPosition == 0) {
  243. pusherHome = true;
  244. }
  245.  
  246. if (((StartButton == HIGH ) && (Home == HIGH)) || ((StartButton == HIGH) && (ResetButton == HIGH)) || ((Home == HIGH) && (ResetButton == HIGH)) || (StartButton == HIGH && Home == HIGH && ResetButton == HIGH) ) //Multiple button press
  247. {
  248. reset(); //Home everything
  249. }
  250.  
  251. if (Start == HIGH) {
  252. trayOut();
  253. }
  254. else if (Home == HIGH) {
  255. trayIn();
  256. }
  257.  
  258. }//end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement