loop not working as part of other code.

Hi,

im working on a project where by a user presses a button and an auger dispenses a set amount of stuff untill its reached the target weight.

I have the code nearly finished but came to add a start switch but cannot get it to work with my existing code,

the first code is my code that is not working and the second code does work fine. why is this? Im assuming its a formatting error but not incorrect enough to not complie and upload.

thanks

#include <Wire.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"

NAU7802 myScale; // Create an instance
SoftwareSerial mySerial (10, 11); //i2c for scale

int inPin = 2; // start button/serial command from master
int outPin = 13; // to see if button press is registered
int state = HIGH; // current value
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;
const long interval = 6000; // stable time for dispaly
const long interval1 = 3000; //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(outPin, OUTPUT);
  mySerial.begin(9600);
  Serial.begin(57600);
  Serial.println("Qwiic Scale");
  pinMode(Auger, OUTPUT);
  //startscale();
}

void loop() 
{
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == LOW)
      //state = LOW;
      Serial.println("START");
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}



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 writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)
  {
    mySerial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }

  mySerial.write(0xff); //We need to write the 3 ending bits to the Nextion as well
  mySerial.write(0xff); //it will tell the Nextion that this is the end of what we want to send.
  mySerial.write(0xff);

}

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();
  {
    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");
}

working code

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  Serial.begin(57600);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == LOW)
     // state = HIGH;
      Serial.println("START");
      
    else
      state = LOW;

    time = millis();
  }

  digitalWrite(outPin, state);
  

  previous = reading;
}

"Not working" conveys no useful information to help you. What does the code do? How is that different from what you want the code to do?

First code...

   else
      state = HIGH;

Second code...

   else
      state = LOW;

Other than that, and the initialization of the scale in the first sketch, I can't see any differences.

Edit: Also why is...

state = HIGH;

..commented out in both sketches?

In the first sketch: state starts HIGH, and never gets set LOW.
In the second sketch: state starts HIGH, gets set LOW the first time the button is pressed, then is never set LOW again.

Hi

Thanks for your response pcbbc

Well they are commented out as for a reason i can no longer remember.

The second code works as I need and only prints "start" one when it is pressed.

I will have to change that line in the first code and see what happens.