Advertisement
Orion5001

Stepper Code v0.4

Oct 22nd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 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. int minimum_speed_delay = 50;
  16. int maximum_speed_delay = 1;
  17. int acceleration_delay = 2;
  18. int count = 0;
  19. int current_speed_delay = 100;
  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. int current_speed_delay = 100;
  51. while (current_speed_delay > minimum_speed_delay) {
  52. digitalWrite(DIR, LOW);
  53. digitalWrite(ENA, HIGH);
  54. digitalWrite(PUL, HIGH);
  55. delayMicroseconds(current_speed_delay);
  56. digitalWrite(PUL, LOW);
  57. delayMicroseconds(current_speed_delay);
  58. int current_speed_delay = current_speed_delay - acceleration_delay;
  59. int count = count + 1;
  60. }
  61. }
  62. else if (count > 0) { //Motor speed = multiple of potiometer reading
  63. lcd.clear();
  64. lcd.setCursor(0, 0);
  65. lcd.print("Motor is operating");
  66. lcd.setCursor(0, 2);
  67. lcd.print("at");
  68. lcd.setCursor(8, 2);
  69. lcd.print("RPMs");
  70. while (digitalRead(rocker) == HIGH) {
  71. digitalWrite(DIR, LOW);
  72. digitalWrite(ENA, HIGH);
  73. digitalWrite(PUL, HIGH);
  74. delayMicroseconds(current_speed_delay);
  75. digitalWrite(PUL, LOW);
  76. delayMicroseconds(current_speed_delay);
  77. int potent = analogRead(pot_in);
  78. current_speed_delay = constrain(potent, maximum_speed_delay, minimum_speed_delay);
  79. int count = count + 1;
  80.  
  81. lcd.setCursor(3, 2);
  82. lcd.print(int(current_speed_delay));
  83. }
  84. }
  85. }
  86. else if (digitalRead(rocker) == LOW) { //Turning motor off
  87. if (count > 0) { //Deceleration to 0
  88. lcd.clear();
  89. lcd.setCursor(0, 1);
  90. lcd.print("Motor is");
  91. lcd.setCursor(0, 2);
  92. lcd.print("decelerating");
  93. while (current_speed_delay < 100) {
  94. digitalWrite(DIR, LOW);
  95. digitalWrite(ENA, HIGH);
  96. digitalWrite(PUL, HIGH);
  97. delayMicroseconds(current_speed_delay);
  98. digitalWrite(PUL, LOW);
  99. delayMicroseconds(current_speed_delay);
  100. int current_speed_delay = current_speed_delay + acceleration_delay;
  101. int count = count + 1;
  102. }
  103. int count = 0;
  104. }
  105. else if (count == 0) { //Motor speed = 0
  106. lcd.clear();
  107. lcd.setCursor(3, 1);
  108. lcd.print("Motor is off");
  109. while (digitalRead(rocker) == LOW) {
  110. delay(10);
  111. }
  112. }
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement