Help on running stepper motor with keypad input

Hello again!

Its me the noobies trying to work on my project, would like to ask your help and suggestion for my codes dear Arduino Master ;-;
I'm currently working on a project, involving load cell+HX711, 2 buttons, Keypad, LCD+I2C Backpack module and Nema Stepper +TB6560. Im currently on my final stage of my coding progress which is compiling all things together. Everything works fine, the LCD display, keypad input, button, and the load cell unless the stepper.

So i had this 2 different mode which require me to move the stepper motor in two different motion. The first one, i need to move the stepper based on the button input. It will moves as long as the button pressed. Btw this part works fine. And the second mode I need to move the stepper based on the input that i get from keypad. So far, i have make it work based on the desired distance but apparently i cannot move it based on the define velocity and acceleration.

Ive tried a couple things which are increasing the millis (), the baudrate, moving the
stepper.setMaxSpeed(vel); stepper.setAcceleration(accel);

But it still does not work, Anyone can help me in giving suggestion what should i do? Im out of ideas since im not that experienced in writing Arduino codes :')

Thank you so much for passing by in this thread btw :smiley:

//Defining Libraries
#include <AccelStepper.h> //Library for stepper
#include <Wire.h> //Library for I2C
#include <LiquidCrystal_I2C.h> //Library for I2C
#include <Keypad.h> //Library for keypad
#include "HX711.h" //Library for load cell

//Defining Variable for Stepper Motor
#define dirPin 52 //Pin CW+ on TB6560
#define stepPin 53 //Pin CLK+ on TB6560
#define motorInterfaceType 1
#define stepsPerRevolution 1600 
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

//Defining Variable for Load Cell
#define DOUT  22 //Pin DT on HX711
#define CLK  23 //Pin CLK on HX711
float calibration_factor = -233500; //Calibration factor of load cell
HX711 scale(DOUT, CLK);

//Defining pinout for keypad
const int ROW_NUM = 4; //defining four rows in keypad
const int COLUMN_NUM = 4; //defining four columns in keypad
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
String inputString; //receive input from keypad 
long inputInt; //Storing integer from keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);  

//Defining pinout for LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);

//Defining pinout for buttons
const int button1 = 32; //'UP' button
const int button2 = 33; //'DOWN' button
int buttonState = 0; //variable for reading the pushbutton status
int buttonState2 = 0; //variable for reading the pushbutton status

//Storing Variable
int key = 0;
int velocity = 0;
int acceleration = 0;
int displacement = 0;
float vel;
float accel;
float disp;

void setup() {
  
  Serial.begin(115200);
  
  //Stepper setup
//  stepper.setMaxSpeed(1000);
//  stepper.setAcceleration(100);

  //Load cell setup
  scale.set_scale(-233500);  //Calibration Factor obtained from first sketch
  scale.tare();
   
  //Keypad setup
  inputString.reserve(10); // maximum digit input

  //LCD setup
  lcd.backlight();
  lcd.init(); 
  
}

void loop() {

  //Initial mode
  while (key == 0){
      lcd.clear();
      lcd.setCursor(0, 0); 
      lcd.print("Select mode:");
      lcd.setCursor(0, 1); 
      lcd.print("1: Prep Mode; 2: Test Mode");  
      inputString = keypad.getKey();
      inputInt = inputString.toInt();
      key = inputInt;
      delay(100);
  }
  while (key == 1) {
      inputString = "";
      lcd.clear();
      lcd.setCursor(0, 0); 
      lcd.print("Entering prep mode..");
      lcd.setCursor(0, 1); 
      lcd.print("Press 'D' to move to mode B ");
      char input = keypad.getKey();
      buttonState = digitalRead(button1);
      buttonState2 = digitalRead(button2);

      if (buttonState == HIGH && buttonState2 == LOW) {
          digitalWrite(dirPin, HIGH);
          for (int i = 0; i < 10; i++) {
              digitalWrite(stepPin, HIGH);
              delayMicroseconds(500);
              digitalWrite(stepPin, LOW);
              delayMicroseconds(50);
          }
      }
      if (buttonState == LOW && buttonState2 == HIGH) {
          digitalWrite(dirPin, LOW);
          for (int i = 0; i < 10; i++) {
              digitalWrite(stepPin, HIGH);
              delayMicroseconds(500);
              digitalWrite(stepPin, LOW);
              delayMicroseconds(50);
          }
      }
      if (buttonState == LOW && buttonState2 == LOW) {
          digitalWrite(dirPin, LOW);
          digitalWrite(stepPin, LOW);
      }
      if (input == 'D'){
      key = key + 1;
      inputString = "";
      }
  }

  while (key == 2) {
      while (velocity == 0){
          lcd.clear();
          lcd.setCursor(0, 0); 
          lcd.print("Type desired velocity in mm/s:");
          delay (100);
          char key1 = keypad.getKey();
          
          if (key1 >= '0' && key1 <= '9') {     
              inputString += key1;               
          }
          else if (key1 == '#') {
              if (inputString.length() > 0) {
                  velocity = inputString.toInt();
                  inputString = "";
                  lcd.clear();
                  lcd.setCursor(0, 0); 
                  lcd.print("Desired Velocity: ");
                  lcd.print(velocity);
                  lcd.print(" mm/s");
                  delay (4000);
              }
          } 
          else if (key1 == '*') {
              inputString = "";
          }
      vel = 320.0099*velocity;
      }
      
      while (acceleration == 0){
          lcd.clear();
          lcd.setCursor(0, 0); 
          lcd.print("Desired Velocity: ");
          lcd.print(velocity);
          lcd.setCursor(0, 1); 
          lcd.print("Type desired acceleration in mm/s^2:");
          delay (100);
          char key2 = keypad.getKey();
          if (key2 >= '0' && key2 <= '9') {
              inputString += key2;
          }
          else if (key2 == '#') {
              if (inputString.length() > 0) {
                  acceleration = inputString.toInt();
                  inputString = "";
                  lcd.clear();
                  lcd.setCursor(0, 0); 
                  lcd.print("Desired Acceleration: ");
                  lcd.print(acceleration);
                  lcd.print(" mm/s^2");
                  delay (4000);
              }
          } 
          else if (key2 == '*') {
              inputString = "";
          }
      accel = 320.0099*acceleration;
      }
      
      while (displacement == 0){
          lcd.clear();
          lcd.setCursor(0, 0); 
          lcd.print("Desired accel: ");
          lcd.print(acceleration);
          lcd.setCursor(0, 1); 
          lcd.print("Type desired displacement in mm:");
          delay (100);
          char key3 = keypad.getKey();
          if (key3 >= '0' && key3 <= '9') {
              inputString += key3;
          }
          else if (key3 == '#') {
              if (inputString.length() > 0) {
                  displacement = inputString.toInt();
                  inputString = "";
                  lcd.clear();
                  lcd.setCursor(0, 0); 
                  lcd.print("Desired displacement: ");
                  lcd.print(displacement);
                  lcd.print(" mm");
              }
          } 
          else if (key3 == '*') {
              inputString = "";
          }
      disp = 320.0099*displacement;
      }
      
  scale.tare();
  stepper.setMaxSpeed(vel);
  stepper.setAcceleration(accel);
      
      while (displacement != 0) {
          lcd.clear();
          lcd.setCursor(0, 0); 
          lcd.print("Start testing...");
          stepper.setMaxSpeed(1000);
          stepper.setAcceleration(500);
          stepper.moveTo(-1000);
          stepper.run();
          static unsigned long timer = 0;
          unsigned long interval = 4000;
          if (millis() - timer >= interval){
              timer = millis();
              Serial.print(stepper.currentPosition()/-320.0099);
              Serial.print(",");
              Serial.print(scale.get_units(), 2);
              Serial.print(",");
              Serial.println();
          }
      }
  }
}

Do Yourself a favour and read the first topic telling how to get the best from this forum.
Post formatted code using code tags and post a real wiring diagram.

Oh hello!
Oops sorry i thought its gonna be too long to post it using the code tags, so I post the file instead. Thank you tho for telling me about that! I will check again the topic to make a proper thread

Thanks for the code!
Before analyzing the code I would like to read and check the wiring diagram.
In the time between You can think about using more Serial.print and serial monitor to verify that the execution of the code runs as You want.

Hello, yeap i just finished the schematic and uploaded the picture on the thread. Thank you so much for helping me out!!

Sorry but all thumbs down. Those Fritzings are useless. Power supplies are missing as well as all pin designations. No helper will buy the entire collection of circuits, counting pins comparing with a Fritzing.
Sorry.

uh sorry, what does it mean by Fritzing anyway?

What is the name of program/system making that drawing? Fritzing?
The trouble is that no pin designation is readable. That picture shows in what neighbourhood on the circuit the connection is made. Even having those circuits on hand it's almost a detective work to see where the connections are made.

The more easy You make it for helpers to read and understand the better help You get.
No helper sets an entire evening aside to penetrate a "Fritzing". The police investigating a murder might do it.....

Ah i'm sorry, wait lemme try to fix the schematic so that it will become clearer. I will upload it on the thread once it finished. Thank you tho for your critics, i didnt realized that it is unreadable ;-;

In the time between, try my tip about using strategic Serial.print to "gossip", tell how the execution proceeds inside the code.

As the hired guy I was thrown into large, totally unknown systems, in order to find bugs, improve system capabilities etc. The technic of Serial.prints has done a lot for me.

So..
I had problem with the value of velocity and acceleration that the stepper run with. How can i possibly print this? so basically i need to print that part right? Since the other part is working just fine

Okey.
Print out like "Velocity set to "; Print (velocity); Do the same for acceleration, at the locations in the code where this is done.
Then any mishaps in variable calculation would likely be seen.

I see, hmm i never try those. I'll try your suggestion, thank you btw!

Please do. Your system is respectably large as well as the code. You're the guy being the best in navigating the code.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.