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
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.
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.
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
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