Hi there,
I am having trouble creating weight ranges in order to control a stepper motor. The idea is that if the weight falls in the first weight range, the stepper motor will turn X amount of steps and if the weight falls I the second weight range, the stepper motor will turn Y amount of steps. If the weight is outside both weight ranges, no steps will be made.
I am using a load cell and HX711 module as the sensor.
At the moment, the stepper motor does respond to a weight but will turn even if the weights fall outside of the weight ranges.
I have attached all my code below, any suggestions?
// load cell
#include "HX711.h"
#define DOUT 6
#define CLK 7
HX711 scale(DOUT, CLK);
float calibration_factor = 387600;
// hedgehog weight limits
int sensorMin = 0.350; // 350g
int sensorMax = 0.750; // 750g
int sensorMid = 0.450; // 450g
int sensValLow = constrain(scale.get_units(), sensorMin, sensorMid);
int sensValHigh = constrain(scale.get_units(), sensorMid, sensorMax);
const int delayBetweenFeeds = 6e+7; //1 minute
//stepper
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9); //dirPin = 8, stepPin = 9
int previous = 0;
//SD card
#include <SPI.h> //load SD card library
#include <SD.h> //load SPI library
//do i need the following?:
//Sd2Card card;
//SdVolume volume;
//SdFile root;
const int chipSelect = 10;
File hedgehogData; //Data object you will write your sesnor data to
#include <Adafruit_SleepyDog.h>
//#include <JeeLib.h> //include library for low power functions
//ISR(WDT_vect) {Sleepy:watchdogEvent/();} //setup for low power waiting
void setup() {
Serial.begin(9600);
//set up load cell
Serial.println("Press T to tare");
scale.set_scale(calibration_factor); //Calibration Factor obtained from first sketch
scale.tare();
pinMode(10, OUTPUT);
SD.begin(chipSelect);
}
void loop() {
//use load cell to weigh
Serial.print("Weight: ");
Serial.print(scale.get_units(), 3); //2 decimal points
Serial.println(" kg");
delay(1000);
//control dispensor with load cell readings and stepper
if (sensValLow){ //((scale.get_units()>sensorMin) && (scale.get_units()>sensorMid){
hedgehogData = SD.open("Weight.txt", FILE_WRITE);
Serial.print("Weight: ");
Serial.print(scale.get_units(), 3);
Serial.println(" kg");
myStepper.setSpeed(30);
Serial.println("stepperclockwise");
myStepper.step(400);
hedgehogData.println(scale.get_units(), 3);
hedgehogData.close();
Sleepy::loseSomeTime(10000); //instead of delay (10000)
}
else if (sensValHigh){ //((scale.get_units()>sensorMid)&&(scale.get_units()<sensorMax)){
hedgehogData = SD.open("Weight.txt", FILE_WRITE);
Serial.print("Weight: ");
Serial.print(scale.get_units(), 3);
Serial.println(" kg");
delay(100);
myStepper.setSpeed(30);
Serial.println("stepperclockwise");
myStepper.step(600);
delay(1000);
}
else {
myStepper.step(0);
}
}