-2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • could you define fast? – Frenchy Dec 30 '18 at 18:46
  • Fast means 100 k pulse per second – Marianna Kalwat Dec 30 '18 at 19:24
  • 100khz so 10 microseconds so you could use hardware timer but you cant do lot of operations because each operation will be slower than the counter, except if you test some ports (2micro second to read ou write value on port). i could give you an example if you want to use hardware timer – Frenchy Dec 30 '18 at 19:36
  • Give me working example, i will be very grateful ;) – Marianna Kalwat Dec 30 '18 at 19:53
  • ok i'll give you to morrow its late in France.. – Frenchy Dec 30 '18 at 19:55
  • 1
    Marianna: while readers here are generally willing to help someone who does not know where to start on a problem, it helps readers a great deal if question askers can make a start. The first step is to do some solid research with a search engine, and then to try pieces of found code, and to test whether they are suitable. Even if this enterprise does not result in working code, at least the resulting Stack Overflow question can show what has been tried. – halfer Dec 30 '18 at 21:28

1 Answers1

2

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.

J.D.
  • 4,511
  • 2
  • 7
  • 20