LCD counter goes into minus digits after a while

Hi,

I am making a counter that adds 124 every second and displays the updated number on an LCD. However, at some point it starts to show negative numbers. What would be the cause of this?

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// constants won't change. Used here to set a pin number:
const int ledPin =  13;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)
int money = 0;


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
 
  lcd.begin(16, 2);
   lcd.print("$");
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  lcd.setCursor(2, 0);
  lcd.print(0);
  
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    
    money = money + 124;
    
    //Serial.println('
  • ' ' + money);
       
        lcd.setCursor(2, 0);
       
        lcd.print(money);}
        delay(1000);
       
       
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
     
    }
unsigned long money = 0;
int money = 0;

What range of values can this type of variable hold ?

However, at some point it starts to show negative numbers.

Approximately at what point?

The most significant bit of a signed integer is used for the +/- sign (0 = positive, 1=negative). So with 16-bits you can count to 32,767, (which is 0111 1111 1111 1111 in binary (spaces added for readability). If you count one more, you flip the most significant bit and you have 1000 0000 0000 0000. That's a negative number.

But, but it's NOT "negative zero" because C/C++ uses [u]Two's Complement[/u] for negative numbers. (There is no negative-zero with two's compliment.) So, it's -32,768. If you write a program with ints that "count's up forever" it will hit 32,767, then -32,768 and it will start counting down to zero where it will overflow, leaving you with 16 binary zeros where it starts-over.

….A long time ago I worked for a company that made a piece of equipment with a connection for a [u]chart recorder[/u] to record pressure (PSI). We had a customer telling us that the chart recorder would "go crazy" at a certain point. The front panel display was OK. Of course, it was a "normal" decimal display of PSI, and a corresponding analog voltage output to the chart recorder. We couldn't duplicate the problems in our factory testing. It was a binary overflow at some point suddenly slamming the chart-recorder-output to zero. But with the conversion from some binary reading (which of course nobody could see) to decimal PSI, and then to an analog voltage it wasn't obvious that we had a binary overflow at 255 or 32,767. And, they were using some custom scale/gain settings so we couldn't duplicate the problem and we were baffled. Finally, the programmer figured-out what was happening. (The programmer had probably seen this kind of thing before.)