hi, i'm having issues with my loadcell and my step motor. My load cell is not working optimally, it has spikes from nothing to 800 and when I want to turn on my step motor it does nothing. the load cell and the step motor used the same 5v.
#include "HX711.h"
#include <Wire.h>
#include <Pushbutton.h>
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
HX711 scale;
int reading;
int lastReading;
// REPLACE WITH YOUR CALIBRATION FACTOR
#define CALIBRATION_FACTOR -476.592
// Button
#define BUTTON_PIN 4
Pushbutton button(BUTTON_PIN);
// Stepper motor pins
const int int1 = 10;
const int int2 = 11;
const int int3 = 12;
const int int4 = 13;
const int counterClockwise = 8;
const int clockwise = 7;
int pole1[] = {0, 0, 0, 0, 0, 1, 1, 1, 0};
int pole2[] = {0, 0, 0, 1, 1, 1, 0, 0, 0};
int pole3[] = {0, 1, 1, 1, 0, 0, 0, 0, 0};
int pole4[] = {1, 1, 0, 0, 0, 0, 0, 1, 0};
int counter1 = 0;
int counter2 = 0;
int dir = 3;
int poleStep = 0;
void setup() {
Serial.begin(9600);
// Initialize the scale
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(CALIBRATION_FACTOR); // this value is obtained by calibrating the scale with known weights
scale.tare(); // reset the scale to 0
// Initialize stepper motor pins
pinMode(int1, OUTPUT);
pinMode(int2, OUTPUT);
pinMode(int3, OUTPUT);
pinMode(int4, OUTPUT);
pinMode(counterClockwise, INPUT_PULLUP);
pinMode(clockwise, INPUT_PULLUP);
}
void loop() {
// Handle button press for scale tare
if (button.isPressed()) {
Serial.print("tare...");
scale.tare();
}
// Read and print scale data
if (scale.wait_ready_timeout(200)) {
reading = round(scale.get_units());
Serial.print("gram ");
Serial.println(reading);
if (reading != lastReading) {
// Do something if the reading has changed
}
lastReading = reading;
} else {
Serial.println("HX711 not found.");
}
// Handle stepper motor control
int btn1State = digitalRead(counterClockwise);
int btn2State = digitalRead(clockwise);
if (btn1State == LOW) {
counter1++;
delay(500);
if (counter1 != 2) {
counter2 = 0;
dir = 1;
} else {
counter1 = 0;
dir = 3;
}
}
if (btn2State == LOW) {
counter2++;
delay(500);
if (counter2 != 2) {
counter1 = 0;
dir = 2;
} else {
counter2 = 0;
dir = 3;
}
}
if (dir == 1) {
poleStep++;
driverStepper(poleStep);
} else if (dir == 2) {
poleStep--;
driverStepper(poleStep);
} else {
driverStepper(8);
}
if (poleStep > 7) {
poleStep = 0;
} else if (poleStep < 0) {
poleStep = 7;
}
delay(1);
}
void driverStepper(int step) {
digitalWrite(int1, pole1[step]);
digitalWrite(int2, pole2[step]);
digitalWrite(int3, pole3[step]);
digitalWrite(int4, pole4[step]);
}