Limit when using count++ function

Hi all,

I've been using an Arduino to monitor an experiment and using the count++ function to count the number of cycles. I noticed that when the experiment reaches around 37,000 counts it stops increasing and starts decreasing back down to zero.

I need it to keep counting up and I don't see anything in the code that could be preventing that.

Do you guys know if there is an upper limit on counts when using count++ or if there is a better counting function?

Thanks,
Will

I’m gonna go out on a limb and guess that the problem is with your code. Since you didn’t show us that code, that’s about all I can say.

Always post your code!

What datatype is count?

Are you sure it's 37000 not 32767? 32767 is the largest value that can be stored in an int datatype - if you increment it, it will wrap around to -32768. In this case, the solution is simple - use a larger datatype for count: A long goes up to ~2.1 billion. And if you're only ever intending to deal with positive numbers, you can declare it unsigned - unsigned int goes to 65535, unsigned long to 4.2 billion)

#include "HX711.h"

const int loadPin = 2;
const float minLoad = 30;
int valveOne = 13;
int valveTwo = 8;
int count = 0;

HX711 scale(A3, A2);

void setup() {
  Serial.begin(38400);
  Serial.println("Load and Counter Values");

  scale.set_scale(-2250.f); 
  scale.tare();  

  Serial.println("Readings:");
  pinMode(valveOne, OUTPUT);
  pinMode(valveTwo, OUTPUT);
  delay(2000);
}

void loop() {
  digitalWrite(valveTwo, HIGH); 
  delay(500);
  scale.tare();
  digitalWrite(valveTwo, LOW); 
  digitalWrite(valveOne, HIGH); 
  delay(750); 
  int loadVal = digitalRead(loadPin);
  Serial.print("Reading: ");
  Serial.print(scale.get_units(1), 3); 
  Serial.print(" lbF");
  Serial.print("   Cycle ");
  Serial.println(count);
  delay(10);
  if (scale.get_units(1) < 450) delay(259200000);
  count++;
  digitalWrite(valveOne, LOW);

}

Sorry! Here is the code. The data base thing sounds like it might be correct. It could be that 32,000 number. I forgot to write down the exact number.

What, EXACTLY, are you counting? If you can't answer that, why are you counting anything?

We're counting cycles. So every time the program runs.

will2019:
We're counting cycles. So every time the program runs.

Why? Of what possible use is the number of times that loop() iterated?