Go Down

### Topic: T1 T0 pulse counting (Read 5704 times)previous topic - next topic

#### piotrekcrash

##### Jun 01, 2012, 01:12 pm
Hello!
I'm new to the forum.
I need to count pulses T1 and T0 in the following sposów.

Within one seconds would like to count the sum of the pulse length T0.

Within one seconds the same amount I would like to count the number of pulses on T1.

Did you meet someone with something similar in the Arduino? For example, using: freqcounterlibrary.zip

Regards

#### robtillaart

#1
##### Jun 02, 2012, 05:39 pm

To count the sum of pulse length T0 you must first learn how to determine the length of one pulse. There are several discussions about measuring pulselengths.

On the reference pages of arduino.cc you need to check the pulsein command, and in the core files you can see how this is done. From that you can start to build your own application.

Maybe you can tell more what you want to accomplish with your measurements.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### piotrekcrash

#2
##### Jun 02, 2012, 08:02 pm
I am building a car computer.
needs to measure the amount of time to open a gasoline injector in a given unit of time, for example, one second.

In the same second number of pulses needed to measure the distance traveled sensor.

From these two values ??can be calculated instantaneous combustion of gasoline, etc..

Both pulses are pulses of GND.

These data sent by bluetooth and  I counted everything else on the other device. (smartphone)

if someone can help me please write.

#### piotrekcrash

#3
##### Jun 09, 2012, 01:19 pmLast Edit: Jun 09, 2012, 01:22 pm by piotrekcrash Reason: 1
Quote
#include "TimerOne.h"

unsigned long injector = 0;
unsigned long injector_counter = 0;
unsigned long pulse_time_counter = 0;
unsigned long time = 0;
unsigned long speed_sensor = 0;

void setup()
{
Serial.begin(115200);
Serial.println("Start!");
Timer1.initialize(1000000);         // initialize timer1, and set a 1 second
Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
}

void loop()
{
injector = pulseIn(2, HIGH);
speed_sensor = pulseIn(3, HIGH);
if (injector != 0)
{
pulse_time_counter = pulse_time_counter + injector;
injector_counter++;
Serial.print("Single Pulse Time :") ; Serial.println(injector);
}
}

void callback()
{
Serial.print("Pulses Count :") ; Serial.println(injector_counter);
injector_counter = 0;
Serial.print("Pulses Time  :") ; Serial.println(pulse_time_counter); Serial.println();
pulse_time_counter = 0;
Serial.println(time);
}

How long is my loop Loop? One second and then goes to the function: callback?
is important to my loop Loop lasted exactly one second.

#### robtillaart

#4
##### Jun 09, 2012, 01:33 pm
1) dont do print statements in an interrupt as ISR's should be as short as possible.
2) declare all vars used in the ISR as volatile unsigned long.

Your code in loop does not know when the values will be reset to 0 so the output can be inconsistent at best.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### piotrekcrash

#5
##### Jun 09, 2012, 08:02 pm
Code: [Select]
`#include <TimerOne.h>#include <Streaming.h>volatile unsigned long injector = 0;volatile unsigned long injector_counter = 0;volatile unsigned long pulse_time_counter = 0;volatile unsigned long speed_sensor = 0;volatile unsigned long measurement_time = 0;volatile unsigned long speed_sensor_counter = 0;int printTime = 1;  unsigned long ltime, time;    //time variablesunsigned long lastTime; int check = 0;void setup() {  Serial.begin(115200);  pinMode(13, OUTPUT);      Timer1.initialize(1000000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)  Timer1.attachInterrupt( timerIsr ); // attach the service routine here } void loop(){  if (check == 1) {  Serial << injector_counter << endl;  pulse_time_counter = injector_counter = speed_sensor_counter = measurement_time = 0;  }  check = 0;    injector = pulseIn(2, HIGH); // speed_sensor = pulseIn(3, HIGH);    if (injector != 0)        {        pulse_time_counter = pulse_time_counter + injector;        injector_counter++;        }    if (speed_sensor != 0)        {        speed_sensor_counter++;        }} void timerIsr(){    check = 1;    digitalWrite( 13, digitalRead( 13 ) ^ 1 );}`

As it looks now?

Is there a way to measure whether the Loop function equal to abide with me one second?

#### robtillaart

#6
##### Jun 09, 2012, 09:17 pm
Quote
Is there a way to measure whether the Loop function equal to abide with me one second?