Advertisement
Guest User

Untitled

a guest
Jan 7th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. const byte rs = 0; //pin for LCD
  3. const byte en = 1; //pin for LCD
  4. const byte d4 = 3; //pin for LCD
  5. const byte d5 = 4; //pin for LCD
  6. const byte d6 = 5; //pin for LCD
  7. const byte d7 = 6; //pin for LCD
  8. const byte dirPin = 9; //5v output pin for motor driver direction
  9. const byte stepPin = 10; //5v output pin for motor driver pulse
  10. const byte switchPin = 2; //input pullup pin for homing limit switch
  11. const int strokeSteps = 2444; //number of steps for full stroke, 1600 steps=72mm
  12. int speedPot; //integer for potentiometer read value 0 to 1023, analog read for speed pot
  13. int delPot; //integer for potentiometer read value 0 to 1023, anaolog read for stroke delay pot
  14. int stepDel = 90; //delay time between each motor step during operation, lower value=faster steps
  15. unsigned long strokeDel; //delay time between actuator strokes
  16. const int homeDel = 1000; //delay time between each motor step during homing sequence
  17. const int homingSteps = 400; //steps taken in direction away from limit switch after switch has been activated
  18. const byte maxSpeed = 90; //max speed expressed as the microseconds per pulse divided by 2
  19. byte speedPerc; //percent maximum speed of stepper motor, varies based on pot, used to define stepDel
  20. int oldStepDel = 90; //the value of stepDel as it is displayed on LCD
  21. int oldStrokeDel = 100; //the value of strokeDel as it is displayed on LCD
  22. unsigned long endPush = 0; //timer set at the of push stroke
  23. unsigned long endPull = 1; //timer set at end of pull stroke
  24.  
  25. LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //setup lcd object using variable defined pins
  26.  
  27.  
  28. void setup() {
  29. pinMode(dirPin, OUTPUT);
  30. pinMode(stepPin, OUTPUT);
  31. pinMode(switchPin, INPUT_PULLUP);
  32.  
  33. lcd.begin(16, 2); //start lcd with 16 columns and 2 rows
  34.  
  35. //indicate start of homing sequence on LCD
  36. lcd.setCursor(0, 0);
  37. lcd.print("Homing...");
  38.  
  39.  
  40. //set direction towards limit switch for homing
  41. digitalWrite(dirPin, LOW);
  42.  
  43. //while limit switch is not pressed (HIGH state due to input_pullup), step motor towards the limit switch for homing
  44. while (digitalRead(switchPin)) {
  45. digitalWrite(stepPin, HIGH);
  46. digitalWrite(stepPin, LOW);
  47. delayMicroseconds(homeDel);
  48. }
  49.  
  50. //pause after limit switch is hit
  51. delay(500);
  52.  
  53. lcd.setCursor(0, 0);
  54. lcd.print("Centering...");
  55.  
  56. //set direction away from limit switch
  57. digitalWrite(dirPin, HIGH);
  58.  
  59. //step motor away from limit switch, distance set by homingSteps
  60. for (int x = 0; x < homingSteps; x++) {
  61. digitalWrite(stepPin, HIGH);
  62. digitalWrite(stepPin, LOW);
  63. delayMicroseconds(homeDel);
  64. }
  65.  
  66. //pause before starting operation
  67. delay(500);
  68.  
  69. attachInterrupt(digitalPinToInterrupt(switchPin), crash, FALLING); //creates interrupt for limit switch, stops motion in case of collision with limit switch
  70. }
  71.  
  72. void loop() {
  73.  
  74. speedPot = analogRead(A0); //read value from potentiometer for speed
  75. delPot = analogRead(A1); //read value from potentiometer for stroke delay
  76. speedPerc = (5 * map(speedPot, 0, 1023, 2, 20)); //map pot value to a percentage between 30% and 100% rounded to nearest 5%
  77. stepDel = (100 * maxSpeed / (speedPerc)); //sets step delay time based on speedPerc, 100%=125 microsecond delay. 50%=250 microsecond delay
  78. strokeDel = (50 * map(delPot, 0, 1023, 1, 20)); //sets stroke delay between 50ms and 1000ms to nearest 50
  79.  
  80. //if strokeDel or stepDel values have changed, refresh LCD
  81. if (strokeDel != oldStrokeDel || stepDel != oldStepDel) {
  82. //writes speed and stroke delay time to lcd
  83. lcd.clear();
  84. lcd.setCursor(0, 0); //set lcd cursor start of first line
  85. lcd.print("Speed (%): "); //print "Speed %:" on lcd
  86. lcd.print(speedPerc); //print speedPerc value after "Speed %:"
  87. lcd.setCursor(0, 1); //set lcd cursor start of second line
  88. lcd.print("Delay (ms): ");
  89. lcd.print(strokeDel); //print stroke delay time in ms
  90. oldStrokeDel = strokeDel; //update old values
  91. oldStepDel = stepDel; //update old values
  92. }
  93.  
  94. //if there has been more time than strokeDel after then end of the pull, and the motor pulled more recently than it pushed, then push the platform away from limit switch
  95. //value for endPull **MUST** start off >endPush in order to trigger a push before pull
  96. if (millis() - endPull >= strokeDel && endPull > endPush) {
  97.  
  98. //set direction away from limit switch for push
  99. digitalWrite(dirPin, HIGH);
  100.  
  101. //accelerate motor for 2% of full stroke length
  102. for (int x = 0; x < (0.02 * strokeSteps); x++) {
  103. digitalWrite(stepPin, HIGH);
  104. digitalWrite(stepPin, LOW);
  105. delayMicroseconds((7 * ((0.01 * strokeSteps) - (x / 2))) + stepDel);
  106. }
  107. //perform 96% of full stroke at full speed
  108. for (int x = 0; x < (0.96 * strokeSteps); x++) {
  109. digitalWrite(stepPin, HIGH);
  110. digitalWrite(stepPin, LOW);
  111. delayMicroseconds(stepDel);
  112. }
  113. //decelerate for 2% of stroke length
  114. for (int x = (0.2 * strokeSteps); x > 0; x--) {
  115. digitalWrite(stepPin, HIGH);
  116. digitalWrite(stepPin, LOW);
  117. delayMicroseconds((7 * ((0.01 * strokeSteps) - (x / 2))) + stepDel);
  118. }
  119.  
  120. endPush = millis(); //set timer for end time of push stroke
  121. }
  122.  
  123. //if there has been more time than strokeDel after then end of the push, and the motor pushed more recently than it pulled, then pull the platform away from limit switch
  124. if (millis() - endPush >= strokeDel && endPush > endPull) {
  125.  
  126. //set direction towards limit switch for pull
  127. digitalWrite(dirPin, LOW);
  128.  
  129. //accelerate motor for 2% of full stroke length
  130. for (int x = 1; x <= (0.02 * strokeSteps); x++) {
  131. digitalWrite(stepPin, HIGH);
  132. digitalWrite(stepPin, LOW);
  133. delayMicroseconds((7 * ((0.01 * strokeSteps) - (x / 2))) + stepDel); //delay starts at roughly 164 + stepDel, as x increments towards 2% of strokeSteps then the delay approaches stepDel
  134. }
  135. //perform 96% of full stroke at full speed
  136. for (int x = 0; x < (0.96 * strokeSteps); x++) {
  137. digitalWrite(stepPin, HIGH);
  138. digitalWrite(stepPin, LOW);
  139. delayMicroseconds(stepDel);
  140. }
  141. //decelerate for 2% of stroke length
  142. for (int x = (0.2 * strokeSteps); x >= 1; x--) {
  143. digitalWrite(stepPin, HIGH);
  144. digitalWrite(stepPin, LOW);
  145. delayMicroseconds((7 * ((0.01 * strokeSteps) - (x / 2))) + stepDel); //delay starts at stepDel, as x decrements from 2% of strokeSteps towards 1 then the delay approaches roughly 164 + stepDel
  146.  
  147. endPull = millis(); //set timer for end time of pull stroke
  148. }
  149. }
  150.  
  151.  
  152. void crash() {
  153.  
  154. //while limit switch is pressed, freeze program here
  155. while (digitalRead(switchPin) == LOW) {
  156. }
  157.  
  158. lcd.clear();
  159. lcd.setCursor(0, 0);
  160. lcd.print("System Crash");
  161. lcd.setCursor(0, 1);
  162. lcd.print("Please Restart");
  163. delay(500);
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement