Before getting any further, please define the length of your "pulse". If it is longer than the time for your loop to execute, you will count more than once because the read will still be high.
Paul
Well the speed required for standard mode is 96 pulses per second. slow mode is 72 pulses per second. The sensor def can output faster than that but not sure Arduino can keep up.. I also have Teensy which is faster maybe that could help?
Not sure how I could get that value, sorry about that - I did plug it in a cheapo digital oscciloscope and I see the pulses but not sure how to read for how long each pulses are.
The screen tells me each horizontal box is 10ms. I estimate one pulse is 17.5millisesonds in width, which matches the PW, pulse width, on the scope of 0,017seconds.
The conclusion is your program will read one pulse as being high for several passes through loop(). And will give a false count, right away.
The only nicely defined change on your pulse is when it goes low. May I suggest you investigate the use of interrupts on some pin set to read "low" and count one for each interrupt. Time for you to look at the many examples of using interrupts on your Arduino.
Paul
in order to have good help, it is very important to provide good information to those who help.
Which arduino are you using in this sketch? Apparently it's a mega Arduino.
What type of sensor is connected to pin (23?)?
If possible post a schematic of your project.
Just to answer your question the simplest way is to divide the pulse detected by 4 and save it on an int. This will automatically truncate the decimal value.
const int input = 23;
int pulse = 0;
int var = 0;
void setup() {
pinMode(input, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("No pulses yet...");
}
void loop() {
if (digitalRead(input) > var)
{
var = 1;
pulse++;
//MOD GERRY
int nibbleCount = pulse / 4;
Serial.print(nibbleCount);
Serial.print(" nibbles ");
Serial.print(pulse);
Serial.print(" pulse");
Serial.println(" detected.");
}
if (digitalRead(input) == 0) {
var = 0;
}
delay(1);
}