Advertisement
Guest User

Untitled

a guest
Oct 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. //STATIC INFO
  2. #define START_TRYK 13
  3. #define JOG_PLUS 9
  4. #define JOG_MINUS 12
  5. #define ENDESTOP_PLUS 11
  6. #define ENDESTOP_MINUS 10
  7. #define PIN_STEP 2
  8. #define PIN_DIR 5
  9. #define PIN_EN 8
  10. #define PIN_LED 7
  11. #define ACCELERATE_Q 3
  12. #define MAX_SPEED 7000
  13. #define BLINK_INTERVAL 100000
  14. #define INTERVAL 1000
  15. //GLOBAL VARIABLES
  16. bool running = 0;
  17. int frq = 0;
  18. int currentFrq = 0;
  19. int retning = 0;
  20. bool nyStart = 1;
  21.  
  22. void setup()
  23. {
  24. pinMode(START_TRYK, INPUT);
  25. pinMode(JOG_PLUS, INPUT);
  26. pinMode(JOG_MINUS, INPUT);
  27. pinMode(ENDESTOP_PLUS, INPUT);
  28. pinMode(ENDESTOP_MINUS, INPUT);
  29. pinMode(PIN_STEP, OUTPUT);
  30. pinMode(PIN_DIR, OUTPUT);
  31. pinMode(PIN_EN, OUTPUT);
  32. pinMode(PIN_LED, OUTPUT);
  33. nyStart = 1;
  34. }
  35.  
  36. void loop()
  37. {
  38. if(digitalRead(ENDESTOP_PLUS) == LOW) { retning = 2; }
  39. if(digitalRead(ENDESTOP_MINUS) == LOW) { retning = 1; }
  40. if(digitalRead(START_TRYK) == LOW && retning != 0) {running = 1;}
  41. //ALMINDELIG DRIFT
  42. while(running == 1 & digitalRead(START_TRYK) == HIGH) //Almindelig drift
  43. {
  44. while(retning == 1) //retning plus
  45. {
  46. currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
  47. if(digitalRead(ENDESTOP_PLUS) == LOW) { retning = 2; }
  48. if(digitalRead(START_TRYK) == LOW) { running = 0; retning = 0; break; }
  49. if(digitalRead(PIN_DIR) == HIGH && nyStart == 0)
  50. {
  51. while(frq > 31) //Decellerer
  52. {
  53. frq = (frq - ACCELERATE_Q);
  54. tone(PIN_STEP, frq);
  55. nyStart = 0;
  56. }
  57. }
  58. digitalWrite(PIN_DIR, LOW);
  59. if(nyStart == 1) { frq = 31; }
  60. while(frq < currentFrq) //accelerate
  61. {
  62. frq = (frq + ACCELERATE_Q);
  63. tone(PIN_STEP, frq);
  64. nyStart = 0;
  65. }
  66. tone(PIN_STEP, currentFrq);
  67. }
  68. while(retning == 2) //retning minus
  69. {
  70. currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
  71. if(digitalRead(ENDESTOP_MINUS) == LOW) { retning = 1; }
  72. if(digitalRead(START_TRYK) == LOW) { running = 0; retning = 0; break; }
  73. if(digitalRead(PIN_DIR) == LOW && nyStart == 0)
  74. {
  75. while(frq > 31) //Decellerer
  76. {
  77. frq = (frq - ACCELERATE_Q);
  78. tone(PIN_STEP, frq);
  79. nyStart = 0;
  80. }
  81. }
  82. digitalWrite(PIN_DIR, HIGH);
  83. if(nyStart == 1) { frq = 31; }
  84. while(frq < currentFrq) //accelerate
  85. {
  86. frq = (frq + ACCELERATE_Q);
  87. tone(PIN_STEP, frq);
  88. nyStart = 0;
  89. }
  90. tone(PIN_STEP, currentFrq);
  91.  
  92. }
  93. if(digitalRead(START_TRYK) == LOW) { running = 0; retning = 0; }
  94. }
  95.  
  96. //JOG DRIFT
  97. while(digitalRead(JOG_PLUS) == LOW)
  98. {
  99. digitalWrite(PIN_DIR, LOW);
  100. currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
  101. while(frq < currentFrq) //accelerate
  102. {
  103. frq = (frq + ACCELERATE_Q);
  104. tone(PIN_STEP, frq);
  105. }
  106. tone(PIN_STEP, currentFrq);
  107. }
  108. while(digitalRead(JOG_MINUS) == LOW)
  109. {
  110. digitalWrite(PIN_DIR, HIGH);
  111. currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
  112. while(frq < currentFrq) //accelerate
  113. {
  114. frq = (frq + ACCELERATE_Q);
  115. tone(PIN_STEP, frq);
  116. }
  117. tone(PIN_STEP, currentFrq);
  118. }
  119. //Reset til nul
  120. if(currentFrq >= 31)
  121. {
  122. while(currentFrq >= 31)
  123. {
  124. currentFrq = (currentFrq - ACCELERATE_Q);
  125. tone(PIN_STEP, currentFrq);
  126. }
  127. noTone(PIN_STEP);
  128. frq = 0;
  129. }
  130. nyStart = 1;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement