I have a Nema 17 stepper motor 42BYGHW603
a4899 driver
1kg loadcell and Hx711
My Problem is when the stepper motor is supposedly stop it jitters and also while it runs its skipping steps. I don't know what to do, and I am stuck.
#include <Arduino.h>
// LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Load cell
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
// Stepper Motor
#include <MobaTools.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int taree = 6;
//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
unsigned long t = 0;
// Stepper Motor
// Defines pins numbers
const byte stepPin = 2;
const byte dirPin = 3;
const int stepsPerRev = 1000; // Steps per revolution - may need to be adjusted
MoToStepper stepper( stepsPerRev, STEPDIR ); // create a stepper instance
// Buttons
const int startPin = 8;
const int addOnePin = 9;
const int minusOnePin = 10;
const int debounceDelay = 250; // Debounce delay for button presses
int counter = 0; // Counter variable
void setup() {
Serial.begin(57600);
Serial.println();
Serial.println("Starting...");
// Stepper Motor
// Sets the two pins as Outputs
stepper.attach( stepPin, dirPin );
stepper.setSpeed( 300 ); // 30 rev/min (if stepsPerRev is set correctly)
stepper.setRampLen( stepsPerRev / 2); // Ramp length is 1/2 revolution
// LCD load cell Output pullup
pinMode (taree, INPUT_PULLUP);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print(" 1KG MAX LOAD ");
LoadCell.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
float calibrationValue; // calibration value (see example file "Calibration.ino")
calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
Serial.println("Startup is complete");
}
delay(3000);
lcd.clear();
}
void loop() {
lcd.clear();
static boolean newDataReady = 0;
const int serialPrintInterval = 2000; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
float i = LoadCell.getData();
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > t + serialPrintInterval) {
Serial.print("Load_cell output val: ");
Serial.println(i);
newDataReady = 0;
t = millis();
}
}
// receive command from serial terminal, send 't' to initiate tare operation:
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay();
}
// check if last tare operation is complete:
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
}
// Stepper Motor
// start
if (digitalRead(startPin) == LOW) {
if(i <= counter){
digitalWrite(startPin, LOW);
stepper.rotate(1); // start turning, 1=forward, -1=backwards
}
}
if (i >= counter){
digitalWrite(startPin, HIGH);
stepper.stop();
counter = 0;
}
// print to lcd
if (digitalRead(addOnePin) == LOW) {
addOne(); // Add One button is pressed
lcd.clear();
lcd.setCursor(1, 1); // set cursor to secon row
lcd.print(counter, 1); // print out the retrieved value to the second row
lcd.print("g "); // grams
delay(debounceDelay); // Debounce delay to avoid multiple additions with one press
}
if (digitalRead(minusOnePin) == LOW) {
minusOne();// Minus One button is pressed
lcd.clear();
lcd.setCursor(1, 1); // set cursor to secon row
lcd.print(counter, 1); // print out the retrieved value to the second row
lcd.print("g "); // grams
delay(debounceDelay); // Debounce delay to avoid multiple additions with one press
} else{
lcd.setCursor(1, 1); // set cursor to secon row
lcd.print(i, 1); // print out the retrieved value to the second row
lcd.print("g ");
}
if (i>=1000) {
i=0;
lcd.setCursor(0, 0); // set cursor to secon row
lcd.print(" Over Loaded ");
delay(200);
}
if (digitalRead (taree) == LOW) {
lcd.setCursor(0, 1); // set cursor to secon row
lcd.print(" Taring... ");
LoadCell.start(1000);
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
void addOne() {
counter++; // Increment the counter by one
printCounter();
}
void minusOne() {
--counter; // Decrement the counter by one
printCounter();
}
void printCounter() {
Serial.print("Counter: ");
Serial.println(counter);
}