Advertisement
absite

stepper2

Sep 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. //librerie
  2. #include <Arduino.h>
  3. #include "A4988.h"
  4. #include <LiquidCrystal.h>
  5.  
  6. //definizione pin
  7. int DIR = 2;
  8. #define STEP 3
  9. #define ENABLE 13
  10. #define MS1 A5
  11. #define MS2 A4
  12. #define MS3 A3
  13. int button = A0;
  14.  
  15. //definizione variabili
  16. #define MOTOR_STEPS 200 //step motore
  17. int MICROSTEPS = 1; //velocità
  18. int giri = 1; //giri*minuto
  19. int vel = 1;
  20. int value = 0;
  21.  
  22. //definizione array
  23. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  24. A4988 stepper(MOTOR_STEPS, DIR, STEP, ENABLE, MS1, MS2, MS3);
  25.  
  26.  
  27. void setup() {
  28.  
  29. //impostazioni pinMode
  30. pinMode(STEP, OUTPUT);
  31. pinMode(DIR, OUTPUT);
  32.  
  33. //output LCD
  34. lcd.begin(16, 2);
  35. lcd.print("giri/min:");
  36. lcd.setCursor(0, 1);
  37. lcd.print("velocita:");
  38.  
  39. Serial.begin(9600);
  40.  
  41. }
  42.  
  43. void loop() {
  44.  
  45. //lettura pulsanti
  46. int sensorValue = analogRead(button);
  47.  
  48. //incremento/decremento velocità
  49. if (sensorValue == 0) {
  50. vel = vel + 1;
  51. delay (500);
  52. }
  53. if (sensorValue == 504) {
  54. vel = vel - 1;
  55. delay (500);
  56. }
  57.  
  58. if (vel == 1) {
  59. MICROSTEPS = 1;
  60. }
  61. if (vel == 2) {
  62. MICROSTEPS = 2;
  63. }
  64. if (vel == 3) {
  65. MICROSTEPS = 3;
  66. }
  67. if (vel == 4) {
  68. MICROSTEPS = 4;
  69. }
  70. if (vel >= 5) {
  71. vel = 1;
  72. }
  73. if (vel <= 0) {
  74. vel = 1;
  75. }
  76.  
  77. //incremento/decremento giri*min
  78. if (sensorValue == 143) {
  79. giri = giri + 1;
  80. delay (500);
  81. }
  82.  
  83. if (sensorValue == 331) {
  84. giri = giri - 1;
  85. delay (500);
  86. }
  87.  
  88.  
  89. //movimento motore
  90. //direzione antioraria con giri negativi
  91. if (giri < 0) {
  92. stepper.rotate(-360);
  93. }
  94. //direzione oraria con giri positivi
  95. else {
  96. stepper.rotate(360);
  97. }
  98.  
  99. //stampa impostazioni a LCD
  100. lcd.setCursor(9, 0);
  101. lcd.print(giri);
  102. lcd.setCursor(9, 1);
  103. lcd.print(MICROSTEPS);
  104.  
  105. //stop con giri = 0
  106. if (giri == 0) {
  107. stepper.disable();
  108. }
  109. //stop con pulsante
  110. else if (value == 1) {
  111. stepper.disable();
  112. lcd.setCursor(12, 0);
  113. lcd.print("STOP");
  114. }
  115. //moviemnto motore
  116. else {
  117. stepper.begin(giri, MICROSTEPS);
  118. stepper.enable();
  119. }
  120.  
  121. //STOP con pulsante select
  122. if (sensorValue == 740) {
  123. value = value + 1;
  124. delay (500);
  125. }
  126.  
  127. if (value == 2) {
  128. value = 0;
  129. lcd.setCursor(12, 0);
  130. lcd.print(" ");
  131. }
  132.  
  133. Serial.println(sensorValue);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement