I wanted to write fast impulse counting code for Arduino, to run at 100khz. I wanted to count fast square impulses from generator. I can’t find anything on the internet.
You can use interrupts. Read the documentation here
Example code:
const byte interruptPin = 2;
int count = 0;
void setup() {
Serial.begin(115200);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), pulse, RISING );
}
void loop() {
if(count % 100000 < 10000)
{
Serial.println(count);
}
}
void pulse() {
count++;
}
Note: with such a fast input signal, speed is an issue. I'm not even sure the above code is fast enough, but at least you know which direction to go.