Hi,
can some one just give me a little pointer, here is what im trying to do.
An auger feeds onto a load cell and when the weight is reached it stops.
as the weight increases the speed slows down as shown in the code.
im just trying to get it to work as follows.
arduino is powered up.
scale is detected and settings loaded from eeprom (this works)
start button pressed.
serial print start once.
auger function runs untill weight reached.
auger stops
below is my current code (i will remove delay and change for millis)
#include <Wire.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
NAU7802 myScale; // Create an instance
SoftwareSerial mySerial (4, 5); //i2c for scale
int inPin = 12; // start button/serial command from master
int led = 13; // to see if button press is registered
int reading; //current reading push button
int previous = LOW; // previous reading puch button
//EEPROM
#define LOCATION_CALIBRATION_FACTOR 0
#define LOCATION_ZERO_OFFSET 10
bool settingsDetected = false;
//Average
#define AVG_SIZE 3
float avgWeights[AVG_SIZE];
byte avgWeightSopt = 0;
//Time Values
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long interval = 6000; // stable time for dispaly
const long interval1 = 30; //jitter time steady weight
const long dumpdelay = 300; //delay for emptying on target weight
long time = 0; // time since switch pressed
long debounce = 200; // debounce time
// More Bits
int Auger = 3; // pin motor driver pwm attached to
int Augerreverse = 4; // reverse pin on motor driver
void setup() {
pinMode(inPin, INPUT);
pinMode(led, OUTPUT);
mySerial.begin(9600);
Serial.begin(57600);
Serial.println("Qwiic Scale");
Serial.println("WAITING FOR START");
pinMode(Auger, OUTPUT);
startscale();
}
void loop() {
int startbutton = digitalRead(12);
//int statbuttoncounter = 0;
int laststate = LOW;
int counter = 0;
if (startbutton != laststate) {
if (laststate == HIGH) {
counter++; // once press to start one to stop the auger function counter increment here
Serial.println("on"); // Should happen when button pressed once
}
delay (500); // I WILL use millis here i promise
laststate = HIGH; // sets last to high for next if statement
}
if (counter % 2 == 0 && laststate == LOW) { // using this to see if button has been pressed once or twice?
Serial.println("Waiting");// waiting for button to be pressed
} else {
auger();
}
}
void startscale(void)
{
Wire.begin();
Wire.setClock(400000); //Qwiic Scale is capable of running at 400kHz if desired
if (myScale.begin() == false)
{
Serial.println("Scale not detected. Please check wiring. Freezing...");
while (1);
}
Serial.println("Scale detected!");
readSystemSettings(); //Load zeroOffset and calibrationFactor from EEPROM
myScale.setSampleRate(NAU7802_SPS_320); //Increase to max sample rate
myScale.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
Serial.print("Zero offset: ");
Serial.println(myScale.getZeroOffset());
Serial.print("Calibration factor: ");
Serial.println(myScale.getCalibrationFactor());
}
void calibrateScale(void)
{
Serial.println();
Serial.println();
Serial.println(F("Scale calibration"));
Serial.println(F("Setup scale with no weight on it. Press a key when ready."));
while (Serial.available()) Serial.read(); //Clear anything in RX buffer
while (Serial.available() == 0) delay(10); //Wait for user to press key
myScale.calculateZeroOffset(64); //Zero or Tare the scale. Average over 64 readings.
Serial.print(F("New zero offset: "));
Serial.println(myScale.getZeroOffset());
Serial.println(F("Place known weight on scale. Press a key when weight is in place and stable."));
while (Serial.available()) Serial.read(); //Clear anything in RX buffer
while (Serial.available() == 0) delay(10); //Wait for user to press key
Serial.print(F("Please enter the weight, without units, currently sitting on the scale (for example '4.25'): "));
while (Serial.available()) Serial.read(); //Clear anything in RX buffer
while (Serial.available() == 0) delay(10); //Wait for user to press key
//Read user input
float weightOnScale = Serial.parseFloat();
Serial.println();
myScale.calculateCalibrationFactor(weightOnScale, 64); //Tell the library how much weight is currently on it
Serial.print(F("New cal factor: "));
Serial.println(myScale.getCalibrationFactor(), 2);
Serial.print(F("New Scale Reading: "));
Serial.println(myScale.getWeight(), 2);
recordSystemSettings(); //Commit these values to EEPROM
}
//Record the current system settings to EEPROM
void recordSystemSettings(void)
{
//Get various values from the library and commit them to NVM
EEPROM.put(LOCATION_CALIBRATION_FACTOR, myScale.getCalibrationFactor());
EEPROM.put(LOCATION_ZERO_OFFSET, myScale.getZeroOffset());
}
//Reads the current system settings from EEPROM
//If anything looks weird, reset setting to default value
void readSystemSettings(void)
{
float settingCalibrationFactor; //Value used to convert the load cell reading to lbs or kg
long settingZeroOffset; //Zero value that is found when scale is tared
//Look up the calibration factor
EEPROM.get(LOCATION_CALIBRATION_FACTOR, settingCalibrationFactor);
if (settingCalibrationFactor == 0xFFFFFFFF)
{
settingCalibrationFactor = 0; //Default to 0
EEPROM.put(LOCATION_CALIBRATION_FACTOR, settingCalibrationFactor);
}
//Look up the zero tare point
EEPROM.get(LOCATION_ZERO_OFFSET, settingZeroOffset);
if (settingZeroOffset == 0xFFFFFFFF)
{
settingZeroOffset = 1000L; //Default to 1000 so we don't get inf
EEPROM.put(LOCATION_ZERO_OFFSET, settingZeroOffset);
}
//Pass these values to the library
myScale.setCalibrationFactor(settingCalibrationFactor);
myScale.setZeroOffset(settingZeroOffset);
settingsDetected = true; //Assume for the moment that there are good cal values
if (settingCalibrationFactor < 0.1 || settingZeroOffset == 1000)
settingsDetected = false; //Defaults detected. Prompt user to cal scale.
}
void readscale(void)
{
long currentReading = myScale.getReading();
float currentWeight = myScale.getWeight();
// Serial.print("\tWeight; ");
// Serial.print(currentWeight, 2);
// Serial.println();
int Weight = currentWeight; // do i need this??
if (settingsDetected == false)
Serial.print("\tScale not calibrated. Press 'c'.");
}
void auger(void) {
float currentWeight = myScale.getWeight();
unsigned long currentMillis = millis();
int laststate = 0;
{
if (currentWeight < 400) {
Serial.println(" Speed 1");
analogWrite(Auger, 255);
}
else if ((currentWeight >= 401) && (currentWeight <= 680)) {
Serial.println(" Speed 2");
analogWrite(Auger, 200);
}
else if ((currentWeight >= 681) && (currentWeight <= 700)) {
Serial.println(" Speed 3");
analogWrite(Auger, 120);
}
else if (currentWeight >= 700) {
Serial.print(" STOP");
analogWrite(Auger, 0);
}
if (currentWeight > 700 && currentMillis - previousMillis > interval1) {
Serial.println(" Deposit");
deposit();
}
}
}
void deposit(void)
{
Serial.print("DONE");
//rest needs to be written
}
When this code runs it serial prints waiting over and over, it does not print start at all and when the button is pressed it prints speed 1 (From auger())
as i say i would like it to do nothing or say waiting untill the button is pressed and the it print start once and then run auger constantly untill it reaches its target.
im sure its just the order of my if statements but i have been trying for a day or so now and just seem to be going in circles.
thanks in advance
andy