Pages: [1]   Go Down
Author Topic: T1 T0 pulse counting  (Read 1928 times)
0 Members and 1 Guest are viewing this topic.
Poland
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Poland
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poland
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

* TimerOne-v9.zip (5.71 KB - downloaded 6 times.)
« Last Edit: June 09, 2012, 06:22:26 am by piotrekcrash » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Rob Tillaart

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

Poland
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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 variables
unsigned 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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

you should read the blink without delay example - http://arduino.cc/en/Tutorial/BlinkWithoutDelay -

it shows you how to use the millis() function to wait exactly for 1 second ±1 millisec.

Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: