Go Down

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

#### peter_29

##### Nov 14, 2014, 08:22 am
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,

#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);
Serial.begin(19200);
}

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

#### JimboZA

#1
##### Nov 14, 2014, 08:31 am
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.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
`  duration += pulseIn(f_input_pin, HIGH, MainPeriod*900);`