Counter help

Our project is recording how many zaps has occurred for an insect zapper. Like whenever it zaps, it will be counted. My concern is that, this program can already count however, there are times that with only 1 zap it counts counts it twice or thrice. Id like it if counts for a certain period of time only once and if also there is a very little gap between the zaps, it will be recorded as one. can anyone help me please. thanks

// digital pin 2 has a ZapperInput attached to it. Give it a name:
int ZapperInput = 2; 
int ZapperState = 0;
int ZapperStateCounter = 0;


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the ZapperInput's pin an input:
  pinMode(ZapperInput, INPUT);
}

// the loop routine runs over and over again forever:

void loop() {
//   read the input pin:
 int ZapperState = digitalRead(ZapperInput);
  delay(125);        // delay in between reads for stability

   if (ZapperState == HIGH){
      ZapperStateCounter++;
      
   Serial.println(ZapperStateCounter);
  }}

Put the delay inside the if statement. Adjust to taste.

Presumably the double count is due to the bug hitting two wires at once, and being seen as two events although one bug?

If you make two events count as one bug, how will you cater for two bugs getting zapped within your delay?

boolrules:
Put the delay inside the if statement. Adjust to taste.

I already did but still the same output. :frowning:

wilfredmedlin:
Presumably the double count is due to the bug hitting two wires at once, and being seen as two events although one bug?

If you make two events count as one bug, how will you cater for two bugs getting zapped within your delay?

yes i also though of that problem :frowning: can you suggest how to solve this problem? I cant think of any so I just thought first of solving the delay problem first before moving on to that one.

Id like it if counts for a certain period of time only once and if also there is a very little gap between the zaps, it will be recorded as one.

I suggest that you don't use delay(). Instead use millis() for timing. Save the millis() value when a zap occurs then, when the next one occurs save the millis() value again. If the time between zaps is below your threshold then ignore the first one and count 1 zap, otherwise count 2 zaps.

It is this sort of application that the loop() function was designed for.

If the time between zaps is below your threshold then ignore the first one and count 1 zap, otherwise count 2 zaps.

It's conceptually easier, I think, to just ignore the second event if it occurs too soon after the first event.

this is basically like the Debounce issue, isn't it ?

just see how the Example sketch uses millis() to deal with it.

PaulS:
It's conceptually easier, I think, to just ignore the second event if it occurs too soon after the first event.

Agreed, but the second event could be regarded as the first event of a new set of events. It depends on the actual requirements.

UKHeliBob:
Agreed, but the second event could be regarded as the first event of a new set of events. It depends on the actual requirements.

im starting with the basics about millis and i would like to run this code first but the plotter doesnt show anything.

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 500;  //the value is a number of milliseconds
int button = 2;    //using the built in LED


void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(button, INPUT);
  startMillis = millis();  //initial start time
}

void loop()
{

 int buttonState = digitalRead(button);
 
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalRead(buttonState);  //if so, change the state of the LED.  Uses a neat trick to change the state
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}

but the plotter doesnt show anything.

Which plotter would this be ?

   digitalRead(buttonState);  //if so, change the state of the LED.  Uses a neat trick to change the state

Whoops !

I suspect that you meant

digitalWrite(aLedPin, !digitalRead(aLedPin));

Where aLedPin has been declared and the pin has been made an output.

but the plotter doesnt show anything.

You don't Serial.print() anything for the plotter to plot, so that's not exactly news.

PaulS:
You don't Serial.print() anything for the plotter to plot, so that's not exactly news.

omg im sorry, but now i tried and still the graph stops before 100 in the plotter :frowning:

UKHeliBob:
Which plotter would this be ?

   digitalRead(buttonState);  //if so, change the state of the LED.  Uses a neat trick to change the state

Whoops !

I suspect that you meant

digitalWrite(aLedPin, !digitalRead(aLedPin));

Where aLedPin has been declared and the pin has been made an output.

I just forgot about the serial.print(). and it displays now. however the plot stops before 100.

i just modified the digital serial read example in arduino and turned the delay into millis.

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
       // delay in between reads for stability
}

sorry for being stupid. i now realize my mistake

i just modified the digital serial read example in arduino and turned the delay into millis.

I see no use of millis()