Advertisement
Orion5001

Stepper Code v0.2

Oct 21st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 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. void setup() {
  16. lcd.begin(20, 4);
  17. lcd.setCursor(0, 0);
  18. lcd.print("Welcome to");
  19. lcd.setCursor(0, 1);
  20. lcd.print("Team Plastic");
  21. lcd.setCursor(0, 2);
  22. lcd.print("String Theory's");
  23. lcd.setCursor(0, 3);
  24. lcd.print("Plastic Extruder!");
  25.  
  26. pinMode (PUL, OUTPUT);
  27. pinMode (DIR, OUTPUT);
  28. pinMode (ENA, OUTPUT);
  29.  
  30. pinMode (pot_in, INPUT);
  31. pinMode (rocker, INPUT);
  32.  
  33. int minimum_speed_delay = 50;
  34. int maximum_speed_delay = 1;
  35. int acceleration_delay = 2;
  36. int count = 0;
  37. int current_speed_delay = 100;
  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 = 0;
  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. }
  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 = ;
  78.  
  79. lcd.setCursor(3, 2);
  80. lcd.print(int(current_speed_delay));
  81. }
  82. }
  83. }
  84. else if (digitalRead(rocker) == LOW) { //Turning motor off
  85. if (count > 0) { //Deceleration to 0
  86. lcd.clear();
  87. lcd.setCursor(0, 1);
  88. lcd.print("Motor is");
  89. lcd.setCursor(0, 2);
  90. lcd.print("decelerating");
  91. while (current_speed_delay < 100) {
  92. digitalWrite(DIR,LOW);
  93. digitalWrite(ENA,HIGH);
  94. digitalWrite(PUL,HIGH);
  95. delayMicroseconds(current_speed_delay);
  96. digitalWrite(PUL,LOW);
  97. delayMicroseconds(current_speed_delay);
  98. int current_speed_delay = current_speed_delay + acceleration_delay;
  99. int count = 0;
  100. }
  101. }
  102. else if (count == 0) { //Motor speed = 0
  103. lcd.clear();
  104. lcd.setCursor(3, 1);
  105. lcd.print("Motor is off");
  106. while (digitalRead(rocker) == LOW) {
  107. delay(10);
  108. }
  109. }
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement