Count pulses at the digital input pin into a time period

I am trying to count the number of pulses on the digital pin within one second. A motor rotates and with each rotation a pulse is sent to the digital pin. These pulses must be counted within one second to know the number of rotation. unfortunately, the abort condition is not stopped after one second and I don't know where my error is. Does anyone have any idea? Here is my code

byte digitalPinPulse = 2;
int sensorPin;
byte pulseCount;

void setup() {
  // put your setup code here, to run once:
  pinMode(digitalPinPulse, INPUT);
}

void loop() {
  oldTime = millis();
  Time = millis();
  do {
    sensorPin = digitalRead(digitalPinPulse);
    if (sensorPin == HIGH)
    {
      pulseCount++;
      Serial.println( pulseCount);
    }
    Time = millis();
  }
  while ((Time - oldTime) < 1000);
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your code counts how many times it reads the input and finds that it is HIGH rather than how many times it has become HIGH. You need to count the number of state changes. Fix that first

See the StateChangeDetection example in the IDE

Hello
Post the schematic of your project.
What type of sensor for RPM detection is used?
In which range the RPM shall be measured?

No. You only need to count long enough to get a reliable reading. How long you need to count for depends on things such as the desired measurement resolution, responsiveness to changes in rpm and the range within which rpm is expected to fall.

I have changed it but that still does not solve my problem. After one second the do while loop should be aborted and it is not the case.


byte digitalPinPulse = 2;
int sensorPin = 0;
int lastSensoPin = 0;
byte pulseCount;

void setup() {
  // put your setup code here, to run once:
  pinMode(digitalPinPulse, INPUT);
}

void loop() {
  oldTime = millis();
  Time = millis();
  do {
    sensorPin = digitalRead(digitalPinPulse);
    if (sensorPin != lastSensoPin) {
      if (sensorPin == HIGH)
      {
        pulseCount++;
        Serial.println( pulseCount);
      }
      Time = millis();
    }
  }
  while ((Time - oldTime) < 1000);
}

Look at where you do this:

Does that happen at a suitable spot? (Hint: look at your if-statements)

i'd suggest using an interrupt to increment a count on a transition, RISING and using loop() to report the count periodically checking millis().

byte digitalPinPulse = 2;
byte pulseCount;

#define Period      1000
unsigned long msecLst;

void
Isr ()
{
    pulseCount++;
}

void setup () {
    Serial.begin (9600);

    pinMode (digitalPinPulse, INPUT);
    attachInterrupt (digitalPinToInterrupt (digitalPinPulse), Isr, RISING);
}

void loop () {
    unsigned long msec = millis();
    if (msec > msecLst)  {
        msecLst += Period;
        Serial.println (pulseCount);
    }
}

when you compile a program you notice that "Time" does not change. which I do not find normal. Therefore the difference (Time- oldTime) in the while remains the same. but I do not know how to change this

in itself it is not wrong and it even works. but if l would use this method on a plant then the plant is interrupted for one second to measure the number of turns. As a supplement perhaps, the number of turns in a second will help me later to calculate the water flow. This water flow is measured in a timer interrupt (every 4 s) and this measurement runs parallel to the normal operation of the plant.

ok. misunderstood what is being asked for

// report time between rising edges on sensor pin

byte pinSensor = 2;
byte state;

#define Period      1000
unsigned long msecLst;

void setup () {
    Serial.begin (9600);

    pinMode (pinSensor, INPUT);
    state = digitalRead (pinSensor);
}

void loop () {
    unsigned long msec = millis();
    byte          sensor = digitalRead (pinSensor);
    if (state != sensor)  {
        state = sensor;
        if (HIGH == sensor)  {
            Serial.println (msec - msecLst);
            msecLst = msec;
        }
    }
}

Wouah thank you for your reactivity. sorry maybe i'm expressing it wrong. here i don't mean the time between two rising edge, but the number of rising edge within one second

that's what i posted in #8, the count after each second
you'd need to keep track of the prior count and subtract it from count to determine the delta

I try with this method and it solve the problem. I don't need to count the number pulses during one second, but just the time between zwo pulses and I konw that this correspond to one pulse. Thank you for your help