Advertisement
CuriousScientist

Arduino code for Accelstepper with buttons

Apr 28th, 2020
1,686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
  3. //This code belongs to the following tutorial video: https://youtu.be/eVnZIuAVQEQ
  4.  
  5. #include <AccelStepper.h> //AccelStepper library
  6. AccelStepper stepper(1, 9, 8);// pulses Digital 9 (CLK); Direction Digital 8 (CCW)
  7.  
  8. //16x2 LCD
  9. #include <LiquidCrystal_I2C.h> //SDA = A4, SCL = A5
  10. LiquidCrystal_I2C lcd(0x27, 16, 2);
  11.  
  12.  
  13. //Defining pins
  14. const int RotaryCLK = 2; //CLK pin on the rotary encoder
  15. const int RotaryDT = 3; //DT pin on the rotary encoder
  16. const int ButtonCW = 4; //Button for clockwise rotation
  17. const int ButtonCCW = 5; //Button for counterclockwise rotation
  18.  
  19. //Defining variables
  20. int RotateCounter = 0; //initial position
  21. int MotorSpeed = 50; //some default value for steps/s
  22.  
  23. //Statuses
  24. int CLKNow;
  25. int CLKPrevious;
  26.  
  27. int DTNow;
  28. int DTPrevious;
  29.  
  30. // Time
  31. float TimeNow1;
  32. float TimeNow2;
  33.  
  34. void setup()
  35. {
  36.  
  37.   Serial.begin(9600);
  38.  
  39.     //------------------------------------------------------
  40.     lcd.init();                      // initialize the lcd
  41.     lcd.init();
  42.     lcd.backlight();
  43.     //------------------------------------------------------
  44.   lcd.setCursor(0,0); //Defining positon to write from first row,first column .
  45.   lcd.print("Button stepping with");
  46.   lcd.setCursor(0,1);
  47.   lcd.print("AccelStepper()"); //You can write 16 Characters per line .
  48.   delay(1000); //wait 1 sec
  49.   //------------------------------------------------------
  50.  
  51.    pinMode(2, INPUT_PULLUP); //we use the internal pullup resistor
  52.    pinMode(3, INPUT_PULLUP);
  53.    pinMode(4, INPUT); //CW button
  54.    pinMode(5, INPUT); //CCW button  
  55.  
  56.   //Store states
  57.   CLKPrevious = digitalRead(RotaryCLK);
  58.   DTPrevious = digitalRead(RotaryDT);
  59.    
  60.   attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE);  
  61.  
  62.   stepper.setMaxSpeed(1000); //SPEED = Steps / second
  63.   stepper.setAcceleration(5000); //ACCELERATION = Steps /(second)^2
  64.  
  65.   printLCD(); //Print the things on the LCD
  66.   TimeNow1 = millis(); //Start time
  67.  
  68. }
  69.  
  70.  
  71. void loop()
  72. {
  73.   //The motor only runs when one of the buttons are kept pressed
  74.  
  75.   CheckButtons(); //Checking the status of the buttons
  76.  
  77.   RunTheMotor(); //Running the motor
  78.  
  79.  
  80.   TimeNow2 = millis();
  81.  
  82.   if(TimeNow2 - TimeNow1 > 200) //if the time difference is more than 200 ms (increase the number to print to the LCD less often)
  83.   {
  84.     updateLCD(); //Printing LCD (This slows down the stepping significantly, so use it less frequently)
  85.     TimeNow1 = millis();
  86.   }
  87.  
  88. }
  89.  
  90.  
  91.  
  92. void updateLCD()
  93. {  
  94.   //this function only updates the necessary parts
  95.   lcd.setCursor(7,0);
  96.   lcd.print("   "); //first we clear the area with spaces (it is important when you show a 3-digit number after a 4-digit number)
  97.   lcd.setCursor(7,0);
  98.   lcd.print(MotorSpeed);    //Print the speed
  99.  
  100.   lcd.setCursor(10,1);
  101.   lcd.print("      ");
  102.   lcd.setCursor(10,1);
  103.   lcd.print(stepper.currentPosition());    //Print the number of steps done from the origin (i.e. the position)  
  104. }
  105.  
  106. void printLCD()
  107. {      
  108.     lcd.setCursor(0,0); // Defining position to write from first row, first column .
  109.     lcd.print("Speed: ");
  110.     lcd.setCursor(8,0);
  111.     lcd.print("        ");
  112.     lcd.setCursor(8,0);
  113.     lcd.print(MotorSpeed);    //Print the speed
  114.    
  115.     lcd.setCursor(0,1); // Defining position to write from second row, first column .
  116.     lcd.print("Position: ");
  117.     lcd.setCursor(10,1);
  118.     lcd.print("      ");
  119.     lcd.setCursor(10,1);
  120.     lcd.print(stepper.currentPosition());    //Print the number of pulses
  121.    
  122. }
  123.  
  124.  
  125.  
  126. void RunTheMotor() //function for the motor
  127. {    
  128.     stepper.enableOutputs(); //enable pins
  129.     stepper.moveTo(RotateCounter); //tell the stepper to move to the 'RotateCounter'steps (absolute) position
  130.    
  131.     while(stepper.distanceToGo() != 0)
  132.     {
  133.       stepper.setSpeed(MotorSpeed);      
  134.       stepper.runSpeedToPosition();
  135.      
  136.       //Serial.print("DistanceToGo: "); //for debugging
  137.       //Serial.println(stepper.distanceToGo());
  138.     }    
  139.     //Serial.println(MotorSpeed); //for debugging
  140. }
  141.  
  142. void CheckButtons()
  143. {
  144.   //Serial.println(digitalRead(ButtonCW)); //Just for debugging
  145.   if(digitalRead(ButtonCW) == HIGH) //if the button is pressed
  146.   {
  147.     RotateCounter++; //increase the value of the variable, this represents the absolute position of the stepper
  148.    
  149.    // Serial.print("ButtonCW: "); //for debugging
  150.    // Serial.println(stepper.distanceToGo());
  151.  
  152.     //you can add delay here which is also a type of debouncing. It is useful when you want to click the button
  153.     //once and increase the steps (value of RotateCounter) only by one.
  154.     //delay(100); //Simplest thing to be able to use this function to add just 1 step at a time to use delay    
  155.   }  
  156.   //Serial.println(digitalRead(ButtonCCW)); //Just for debugging
  157.   if(digitalRead(ButtonCCW) == HIGH)
  158.   {
  159.     RotateCounter--; //decrease the value of the variable, this represents the absolute position of the stepper
  160.    
  161.     //Serial.print("ButtonCCW: "); //for debugging
  162.    // Serial.println(stepper.distanceToGo());
  163.     //delay(100);
  164.   }
  165. }
  166.  
  167. void rotate()
  168. {
  169.   CLKNow = digitalRead(RotaryCLK); //Read the state of the CLK pin
  170.  
  171.   // If last and current state of CLK are different, then a pulse occurred  
  172.   if (CLKNow != CLKPrevious  && CLKNow == 1)
  173.   {
  174.     // If the DT state is different than the CLK state then
  175.     // the encoder is rotating CCW so increase
  176.     if (digitalRead(RotaryDT) != CLKNow)
  177.     {
  178.       MotorSpeed++;          
  179.     }
  180.     else
  181.     {
  182.       // Encoder is rotating CW so decrease      
  183.       MotorSpeed--;      
  184.     }
  185.     stepper.setSpeed(MotorSpeed); //as this functions is not started from the loop() it is maybe good to update the speed here          
  186.   }
  187.   CLKPrevious = CLKNow;  // Store last CLK state
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement