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 ![]()
//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();
}
}
}
}
