Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AccelStepper.h> //STEPPER MOTOR
- #include <Keypad.h> //KEYPAD
- #include <LiquidCrystal.h> //LCD
- #define motorInterfaceType 1 // Define motor interface type
- #define X_pin A0 // Pin A0 connected to joystick x axis
- #define Joy_switch 52 // Pin 4 connected to joystick switch
- #define HALL_SENSOR 8
- Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);// Create keypad object
- AccelStepper myStepper(motorInterfaceType, stepPin, dirPin); // Creates an instance
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- long initial_homing = -1; // - is CCW and + is CW ; Set it to go CCW
- float linearv = 0;
- int speedSelect = 0;
- int flag = 0;
- int val = 0;
- const int dirPin = 23; // Define pin connections
- const int stepPin = 25;
- const byte ROWS = 4; // Constants for row and column sizes
- const byte COLS = 4;
- byte rowPins[ROWS] = {39, 41, 43, 45}; // Connections to Arduino
- byte colPins[COLS] = {47, 49, 51, 53};
- boolean printflag = true;
- char hexaKeys[ROWS][COLS] = // Array to represent keys on keypad
- {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {':', '0', ';', 'D'}
- };
- void Joystick() {
- if (!digitalRead(Joy_switch)) { // If Joystick switch is clicked
- speedSelect++;
- delay(20); // delay for deboucing
- }
- switch (speedSelect) { // check current value of step_speed and change it
- case 1:
- myStepper.setSpeed(1); // fast speed
- break;
- case 3:
- myStepper.setSpeed(3); // medium speed
- break;
- case 10:
- myStepper.setSpeed(10); // slow speed
- break;
- }
- if (analogRead(X_pin) > 712) { // If joystick is moved Left
- myStepper.move(-1);
- }
- if (analogRead(X_pin) < 312) { // If joystick is moved right
- myStepper.move(1);
- }
- }
- void lcdDisplay(int output, bool clr) {
- if (clr) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(value);
- int current = myStepper.currentPosition();
- lcd.setCursor(1, 0);
- lcd.print(current);
- } else {
- lcd.print(output);
- int current = myStepper.currentPosition();
- //lcd.clear(0,16); need to clear only top row;
- lcd.setCursor(1, 0);
- lcd.print(current);
- }
- }
- int getKeypadIntegerMulti() {
- int value = 0; // the number accumulator
- int keyvalue = 0; // the key pressed at current moment
- int isnum = 0;
- keyvalue = customKeypad.getKey();
- if (keyvalue) {
- if (keyvalue == ';') {
- isnum = 0;
- keyvalue = 0;
- value = 0;
- lcdDisplay()
- homefunction(0, true);
- } elif (keyvalue == ':') {
- isnum = 0;
- keyvalue = 0;
- value = 0;
- lcdDisplay(0, true)
- }
- isnum = (keyvalue >= '0' && keyvalue <= ';'); // is it a digit?
- if (isnum) {
- value = value * 10 + keyvalue - '0'; // accumulate the input number
- lcdDisplay(keyvalue, false);
- }
- }
- }
- void homefunction() {
- // Move motor until home position reached
- while (digitalRead(HALL_SENSOR) == 1) {
- myStepper.moveTo(initial_homing); // Set the position to move to
- initial_homing--; // Decrease by 1 for next move if needed
- myStepper.run(); // Start moving the stepper
- delay(5);
- }
- myStepper.setCurrentPosition(0); // Set the current position to zero for now
- myStepper.setMaxSpeed(100); // Set max speed of Stepper
- myStepper.setAcceleration(100); // Set Acceleration of Stepper
- initial_homing = 1; // Now set it to go CW
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- lcd.begin(16, 2);
- myStepper.setMaxSpeed(1000);
- myStepper.setAcceleration(50);
- myStepper.moveTo(2000);
- // Setup the Hall Effect and Joystick switch as an Input
- pinMode(HALL_SENSOR, INPUT);
- pinMode(X_pin, INPUT);
- pinMode(Joy_switch, INPUT_PULLUP);
- // Home the motor
- homefunction();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- while (linearv == 0) // While donΒ΄t value
- {
- Serial.println("Running");
- val = getKeypadIntegerMulti(); // Look keypad
- linearv = (val / 10 / 15.7295 * 60); // Calc vel. value
- }
- if (flag == 0) // Sequence 0 Shown value on LCD
- {
- Serial.println("Flag 0");
- lcd.clear(); // Clear LCD display and print character
- lcd.setCursor(0, 0);
- lcd.print(linearv);
- myStepper.setSpeed(linearv);
- flag = 1; // Allow sequence 1
- }
- if (myStepper.distanceToGo() == 0) { // Change direction once the motor reaches target position
- Serial.println("DistanceToGo = 0");
- flag++; // Incremente sequence count
- if (flag == 2) { // If sequence 1 complete
- Serial.println("Flag = 2");
- myStepper.moveTo(-myStepper.currentPosition());
- }
- if (flag == 3) { // If sequence 2 complete
- Serial.println("Flag = 3");
- flag = 0; // Allow start
- linearv = 0; // Zero vel. value
- lcd.clear(); // Clear LCD display and print character
- myStepper.moveTo(-myStepper.currentPosition()); // Return
- }
- }
- myStepper.runToPosition(); // Move the motor one step
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement