Go Down

Topic: plis help ,I'm trying to make a pulse counter (Read 95 times) previous topic - next topic


I'm trying to make a pulse counter and I found several sketches, but I think they need some changes in order to function, I'm trying to read is a pulse of 5 V, and its duration counting 5 pulses turn on an LED, and I have a question, 1/2 can count pulse,

please and thank you

#define MainPeriod 100  // measurement frame, milliseconds
#define f_input_pin 2   // input pin for pulseIn

long previousMillis = 0;
unsigned long duration=0; // receive pulse width
long pulsecount=0;

void setup()
  pinMode(f_input_pin, INPUT);

void loop()
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= MainPeriod)
    previousMillis = currentMillis;   
    // write current time and F values to the serial port
    Serial.print(" "); // separator!
    float Freq=0.5e6/float(duration); // assume pulse duty cycle 0.5
    Serial.print(" ");
    Serial.print(" ");
  // instead of single measurement per cycle - accumulate and average
  duration += pulseIn(f_input_pin, HIGH, MainPeriod*900);


I think they need some changes in order to function
Well it either functions or not. If it doesn't function as you would like, you'll need to describe what is and isn't happening.
meArm build blog:     http://jimbozamearm.blogspot.com/

Please don't PM for technical advice. Firstly my advice is sometimes wrong, and second, in the forum you get a broader, more timely, range of responses.


You were doing so well until you threw this spanner in the works
Code: [Select]

  duration += pulseIn(f_input_pin, HIGH, MainPeriod*900);

You are now totally disrupting the flow of the loop and rendering your subtractive millis technique, useless.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131