Advertisement
Maderdash

Ub

Jan 29th, 2022
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <AccelStepper.h>                                     //STEPPER MOTOR
  2. #include <Keypad.h>                                           //KEYPAD
  3. #include <LiquidCrystal.h>                                    //LCD
  4.  
  5. #define motorInterfaceType 1                                  // Define motor interface type
  6. #define X_pin            A0    // Pin A0 connected to joystick x axis
  7. #define Joy_switch       52    // Pin 4 connected to joystick switch
  8. #define HALL_SENSOR      8
  9.  
  10. Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);// Create keypad object
  11. AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);  // Creates an instance
  12. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  13.  
  14. long initial_homing     = -1;                                     // - is CCW and + is CW ; Set it to go CCW
  15. float linearv           = 0;
  16. int speedSelect         = 0;
  17. int flag                = 0;
  18. int val                 = 0;
  19. const int dirPin        = 23;                                 // Define pin connections
  20. const int stepPin       = 25;
  21. const byte ROWS =         4;                                  // Constants for row and column sizes
  22. const byte COLS =         4;
  23. byte rowPins[ROWS] = {39, 41, 43, 45};                       // Connections to Arduino
  24. byte colPins[COLS] = {47, 49, 51, 53};
  25. boolean printflag       = true;
  26. char hexaKeys[ROWS][COLS] =                                   // Array to represent keys on keypad
  27. {
  28.   {'1', '2', '3', 'A'},
  29.   {'4', '5', '6', 'B'},
  30.   {'7', '8', '9', 'C'},
  31.   {':', '0', ';', 'D'}
  32. };
  33.  
  34. void Joystick() {
  35.   if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
  36.     speedSelect++;
  37.     delay(20);  // delay for deboucing
  38.   }
  39.   switch (speedSelect) {  // check current value of step_speed and change it
  40.     case 1:
  41.       myStepper.setSpeed(1);  // fast speed
  42.       break;
  43.     case 3:
  44.       myStepper.setSpeed(3);  // medium speed
  45.       break;
  46.     case 10:
  47.       myStepper.setSpeed(10); // slow speed
  48.       break;
  49.   }
  50.   if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
  51.     myStepper.move(-1);
  52.   }
  53.   if (analogRead(X_pin) < 312) {  // If joystick is moved right
  54.     myStepper.move(1);
  55.   }
  56. }
  57.  
  58. void lcdDisplay(int output, bool clr) {
  59.   if (clr) {
  60.     lcd.clear();
  61.     lcd.setCursor(0, 0);
  62.     lcd.print(value);
  63.     int current = myStepper.currentPosition();
  64.     lcd.setCursor(1, 0);
  65.     lcd.print(current);
  66.   } else {
  67.     lcd.print(output);
  68.     int current = myStepper.currentPosition();
  69.     //lcd.clear(0,16);  need to clear only top row;
  70.     lcd.setCursor(1, 0);
  71.     lcd.print(current);
  72.   }
  73. }
  74. int getKeypadIntegerMulti() {
  75.   int value = 0;                      // the number accumulator
  76.   int keyvalue = 0;                       // the key pressed at current moment
  77.   int isnum = 0;
  78.   keyvalue = customKeypad.getKey();
  79.   if (keyvalue) {
  80.     if (keyvalue == ';') {
  81.       isnum = 0;
  82.       keyvalue = 0;
  83.       value = 0;
  84.       lcdDisplay()
  85.       homefunction(0, true);
  86.     } elif (keyvalue == ':') {
  87.       isnum = 0;
  88.       keyvalue = 0;
  89.       value = 0;
  90.       lcdDisplay(0, true)
  91.     }
  92.     isnum = (keyvalue >= '0' && keyvalue <= ';');     // is it a digit?
  93.     if (isnum) {
  94.       value = value * 10 + keyvalue - '0';            // accumulate the input number
  95.       lcdDisplay(keyvalue, false);
  96.     }
  97.   }
  98. }
  99.  
  100. void homefunction() {
  101.  
  102.  
  103.   // Move motor until home position reached
  104.   while (digitalRead(HALL_SENSOR) == 1) {
  105.     myStepper.moveTo(initial_homing);                         // Set the position to move to
  106.     initial_homing--;                                         // Decrease by 1 for next move if needed
  107.     myStepper.run();                                         // Start moving the stepper
  108.     delay(5);
  109.   }
  110.  
  111.   myStepper.setCurrentPosition(0);                        // Set the current position to zero for now
  112.   myStepper.setMaxSpeed(100);                             // Set max speed of Stepper
  113.   myStepper.setAcceleration(100);                         // Set Acceleration of Stepper
  114.   initial_homing = 1;                                     // Now set it to go CW
  115. }
  116.  
  117. void setup() {
  118.   // put your setup code here, to run once:
  119.   Serial.begin(115200);
  120.   lcd.begin(16, 2);
  121.   myStepper.setMaxSpeed(1000);
  122.   myStepper.setAcceleration(50);
  123.   myStepper.moveTo(2000);
  124.  
  125.  
  126.   // Setup the Hall Effect and Joystick switch as an Input
  127.   pinMode(HALL_SENSOR, INPUT);
  128.   pinMode(X_pin, INPUT);
  129.   pinMode(Joy_switch, INPUT_PULLUP);
  130.  
  131.   // Home the motor
  132.   homefunction();
  133.  
  134.  
  135. }
  136.  
  137. void loop() {
  138.   // put your main code here, to run repeatedly:
  139.   while (linearv == 0)                                  // While don´t value
  140.   {
  141.     Serial.println("Running");
  142.     val = getKeypadIntegerMulti();                      // Look keypad
  143.     linearv = (val / 10 / 15.7295 * 60);                // Calc vel. value
  144.   }
  145.   if (flag == 0)                                        // Sequence 0  Shown value on LCD
  146.   {
  147.     Serial.println("Flag 0");
  148.     lcd.clear();                                        // Clear LCD display and print character
  149.     lcd.setCursor(0, 0);
  150.     lcd.print(linearv);
  151.     myStepper.setSpeed(linearv);
  152.     flag = 1;                                           // Allow sequence 1
  153.   }
  154.   if (myStepper.distanceToGo() == 0) {                   // Change direction once the motor reaches target position
  155.     Serial.println("DistanceToGo = 0");
  156.     flag++;                                             // Incremente sequence count
  157.     if (flag == 2) {                                     // If sequence 1 complete
  158.  
  159.       Serial.println("Flag = 2");
  160.       myStepper.moveTo(-myStepper.currentPosition());
  161.     }
  162.     if (flag == 3) {                                    // If sequence 2 complete
  163.       Serial.println("Flag = 3");
  164.       flag = 0;                                         // Allow start
  165.       linearv = 0;                                      // Zero vel. value
  166.       lcd.clear();                                      // Clear LCD display and print character
  167.       myStepper.moveTo(-myStepper.currentPosition());   // Return
  168.     }
  169.   }
  170.   myStepper.runToPosition();                            // Move the motor one step
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement