Advertisement
Orion5001

Stepper Code v0.5

Oct 22nd, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. //
  2. //
  3. //
  4.  
  5. #include <Wire.h>
  6. #include <LiquidCrystal_I2C.h>
  7. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
  8.  
  9. #define pot_in A3
  10. #define rocker 9
  11. #define PUL 7
  12. #define DIR 6
  13. #define ENA 5
  14.  
  15. float minimum_speed_delay = 1000;
  16. float maximum_speed_delay = 1;
  17. float acceleration_delay = 20;
  18. int count = 0;
  19. float current_speed_delay = 10000;
  20.  
  21. void setup() {
  22. lcd.begin(20, 4);
  23. lcd.setCursor(0, 0);
  24. lcd.print("Welcome to");
  25. lcd.setCursor(0, 1);
  26. lcd.print("Team Plastic");
  27. lcd.setCursor(0, 2);
  28. lcd.print("String Theory's");
  29. lcd.setCursor(0, 3);
  30. lcd.print("Plastic Extruder!");
  31.  
  32. pinMode (PUL, OUTPUT);
  33. pinMode (DIR, OUTPUT);
  34. pinMode (ENA, OUTPUT);
  35.  
  36. pinMode (pot_in, INPUT);
  37. pinMode (rocker, INPUT);
  38.  
  39. delay(5000);
  40. }
  41.  
  42. void loop() {
  43. if (digitalRead(rocker) == HIGH) {
  44. if (count == 0) { //Initial acceleration to minimum speed
  45. lcd.clear();
  46. lcd.setCursor(0, 1);
  47. lcd.print("Motor is");
  48. lcd.setCursor(0, 2);
  49. lcd.print("accelerating");
  50. while (current_speed_delay > minimum_speed_delay) {
  51. digitalWrite(DIR, LOW);
  52. digitalWrite(ENA, HIGH);
  53. digitalWrite(PUL, HIGH);
  54. delayMicroseconds(current_speed_delay);
  55. digitalWrite(PUL, LOW);
  56. delayMicroseconds(current_speed_delay);
  57. current_speed_delay = current_speed_delay - acceleration_delay;
  58. count = count + 1;
  59. }
  60. }
  61. else if (count > 0) { //Motor speed = multiple of potiometer reading
  62. lcd.clear();
  63. lcd.setCursor(0, 0);
  64. lcd.print("Motor is operating");
  65. lcd.setCursor(0, 2);
  66. lcd.print("at");
  67. lcd.setCursor(8, 2);
  68. lcd.print("RPMs");
  69. while (digitalRead(rocker) == HIGH) {
  70. digitalWrite(DIR, LOW);
  71. digitalWrite(ENA, HIGH);
  72. digitalWrite(PUL, HIGH);
  73. delayMicroseconds(current_speed_delay);
  74. digitalWrite(PUL, LOW);
  75. delayMicroseconds(current_speed_delay);
  76. int potent = analogRead(pot_in);
  77. current_speed_delay = constrain(potent, maximum_speed_delay, minimum_speed_delay);
  78. count = count + 1;
  79.  
  80. lcd.setCursor(3, 2);
  81. lcd.print(int(current_speed_delay));
  82. }
  83. }
  84. }
  85. else if (digitalRead(rocker) == LOW) { //Turning motor off
  86. if (count > 0) { //Deceleration to 0
  87. lcd.clear();
  88. lcd.setCursor(0, 1);
  89. lcd.print("Motor is");
  90. lcd.setCursor(0, 2);
  91. lcd.print("decelerating");
  92. while (current_speed_delay < 100) {
  93. digitalWrite(DIR, LOW);
  94. digitalWrite(ENA, HIGH);
  95. digitalWrite(PUL, HIGH);
  96. delayMicroseconds(current_speed_delay);
  97. digitalWrite(PUL, LOW);
  98. delayMicroseconds(current_speed_delay);
  99. current_speed_delay = current_speed_delay + acceleration_delay;
  100. count = count + 1;
  101. }
  102. count = 0;
  103. }
  104. else if (count == 0) { //Motor speed = 0
  105. lcd.clear();
  106. lcd.setCursor(3, 1);
  107. lcd.print("Motor is off");
  108. while (digitalRead(rocker) == LOW) {
  109. delay(10);
  110. }
  111. }
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement