daily pastebin goal
37%
SHARE
TWEET

stepper driver

LeonardoLW Mar 13th, 2018 (edited) 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2.  
  3. #define SPEED 800
  4.  
  5. const int dir = 8, step = 9, ms1 = 10, ms2 = 11, ms3 = 12;
  6. const int SetBotton = 13, IncBotton = 14, DecBotton = 15;
  7. const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
  8.  
  9. float position = 0, minMove = 45;
  10.  
  11. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  12.  
  13. void setup() {
  14.  
  15.  // put your setup code here, to run once:
  16.  pinMode(SetBotton, INPUT);
  17.  pinMode(IncBotton, INPUT);
  18.  pinMode(DecBotton, INPUT);
  19.  
  20.  pinMode(dir, OUTPUT);
  21.  pinMode(step, OUTPUT);
  22.  pinMode(ms1, OUTPUT);
  23.  pinMode(ms2, OUTPUT);
  24.  pinMode(ms3, OUTPUT);
  25.  
  26.  digitalWrite(ms1, LOW);
  27.  digitalWrite(ms2, HIGH);
  28.  digitalWrite(ms3, LOW);
  29.  
  30.  lcd.begin(16, 2);
  31.  lcd.print("PROTORAPID      ");
  32.  lcd.setCursor(0, 2);
  33.  lcd.print("Modulo 1801     ");
  34.  delay(2000);
  35.  lcd.clear();
  36.  lcd.print("POSIZIONE:      ");
  37.  lcd.setCursor(0, 2);
  38.  lcd.print(position, 2);
  39.  lcd.setCursor(11, 2);
  40.  lcd.print("gradi");
  41. }
  42.  
  43. void loop() {
  44.  
  45.   // put your main code here, to run repeatedly:
  46.   if(digitalRead(IncBotton) == HIGH) {
  47.     delay(150);
  48.     stepperMove(dir, step, int(minMove / 0.45), 1);
  49.     if(position != 360 && position + minMove <= 360) {
  50.       position += minMove;
  51.     }
  52.     else if(position + minMove > 360) {
  53.       position = abs(360 - (position + minMove));
  54.     }
  55.     else{
  56.       position = 0;
  57.       position += minMove;
  58.       }
  59.     lcd.setCursor(0, 2);
  60.     lcd.print("           gradi");
  61.     lcd.setCursor(0, 2);
  62.     lcd.print(position, 2);
  63.     }
  64.    
  65.   else if(digitalRead(SetBotton) == HIGH) {
  66.     lcd.clear();
  67.     lcd.setCursor(0, 0);
  68.     lcd.print("      Menu      ");
  69.     delay(1000);
  70.     lcd.clear();
  71.     lcd.print("SPOSTAMENTO MIN:");
  72.     lcd.setCursor(0, 2);
  73.     lcd.print(minMove, 2);
  74.     lcd.setCursor(11, 2);
  75.     lcd.print("gradi");
  76.     while(1) {
  77.       if(digitalRead(IncBotton) == HIGH) {
  78.         delay(150);
  79.         minMove += 0.45;
  80.         lcd.setCursor(0, 2);
  81.         lcd.print("           gradi");
  82.         lcd.setCursor(0, 2);
  83.         lcd.print(minMove, 2);
  84.       }
  85.       else if(digitalRead(SetBotton) == HIGH) {
  86.         delay(300);
  87.         break;
  88.       }
  89.       else if(digitalRead(DecBotton) == HIGH) {
  90.         delay(150);
  91.         if(minMove - 0.45 >= 0.45) {
  92.           minMove -= 0.45;
  93.         }
  94.         lcd.setCursor(0, 2);
  95.         lcd.print("           gradi");
  96.         lcd.setCursor(0, 2);
  97.         lcd.print(minMove, 2);
  98.       }
  99.     }
  100.     lcd.clear();
  101.     lcd.setCursor(0, 0);
  102.     lcd.print("POSIZIONE:      ");
  103.     lcd.setCursor(0, 2);
  104.     lcd.print(position, 2);
  105.     lcd.setCursor(11, 2);
  106.     lcd.print("gradi");
  107.     }
  108.    
  109.   else if(digitalRead(DecBotton) == HIGH) {
  110.     delay(150);
  111.     stepperMove(dir, step, int(minMove / 0.45), 0);
  112.     if(position != 0 && position - minMove >= 0) {
  113.       position -= minMove;
  114.     }
  115.     else if(position - minMove < 0) {
  116.       position = 360 + (position - minMove);
  117.       }
  118.     else{
  119.       position = 360;
  120.       position -= minMove;
  121.       }
  122.     lcd.setCursor(0, 2);
  123.     lcd.print("           gradi");
  124.     lcd.setCursor(0, 2);
  125.     lcd.print(position, 2);
  126.     }
  127. }
  128.  
  129. void stepperMove(byte dirPin, byte stepPin, unsigned int step, bool direction) {
  130.  
  131.   if(direction == 0) {
  132.     digitalWrite(dirPin, HIGH);
  133.     }else{
  134.       digitalWrite(dirPin, LOW);
  135.       }
  136.      
  137.   for(int counter = 0; counter != step; counter++) {
  138.     digitalWrite(stepPin, HIGH);
  139.     delayMicroseconds(SPEED);
  140.     digitalWrite(stepPin, LOW);
  141.     delayMicroseconds(SPEED);
  142.     }
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top