Pulse counter counting backwards

HI ,
I have a pulse counter that is counting the pulses from a 6hz square wave. The counter reached just over 30,000 I then noticed therer was a minus sign had appeared and the counter was now counting back.
Can anyone see anything in the code below that would cause this to happen?

#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6,5,4,3,2);
int a=0;
int e =11;

void setup() {
// put your setup code here, to run once:
lcd.print("Solenoid Pulses");
pinMode (e,INPUT);

void loop() {
// put your main code here, to run repeatedly:
int button = digitalRead(e);

if (button==HIGH){
a ++;

delay (100);


counter2.ino (439 Bytes)

You could probably have followed the How to use this forum and posted you program in code tags. Which I would then see the counter uses a signed integer which has a range of -32768 to 32767. FYI 32767+1 = -32768.

You have not described the project, so your options are to use an unsigned integer will will count 0 -> 65535 -> 0 -> 65535, or a unsigned long which counts much high before rolling over. All of these have the same issue. The resolution is for you to determine how to monitor and manage the rollover.

The project is to count pulses of a 6hz signal for approx 1 million cycles to test a solenoid

The project is to count pulses of a 6hz signal for approx 1 million cycles to test a solenoid

Then you need to use a data type that can count to a million or more. Neither signed nor unsigned int will work.

A data type? Could you possibly elaborate?

Data type, the variable type; unsigned int, in t byte, float, long, etc.

What does this line do:

int a=0;

Take a read through the variables section of the Arduino Reference for data types

Thanks, Unsigned long works fine. I really just needed it to count over night I can reset it when I come in in the morning. The count got to 239463 over night.

Good news.
You can let it run a loootttt longer now!

Am I right in thinking it can count up to 2 ^32?