Pages: [1]   Go Down
Author Topic: Best way to count the frequency and duty cycle in same time.  (Read 1023 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm a new guy here, recently I started making a new project with arduino, but I have some big issues. In general, I need to count 2 different PWM signals at the same time, but I can't make it work. I have written and tested one code:  
Code:
void setup() {
  Serial.begin(115200);
  FreqPeriod::begin();

}

void loop() {
  
  injector = pulseIn(11, HIGH);
  
  pp=FreqPeriod::getPeriod();
  if (pp ){

    lfrq= 16000400.0 / pp;
  }
  
    if (injector != 0)
            {

            pulse_time_counter += injector;
          
            }
            
          unsigned long currentMillis = millis();
       if(currentMillis - previousMillis > interval)
       {  
               Serial.println(pulse_time_counter);
              Serial.println(lfrq, 1);
       previousMillis = currentMillis;  
      
       Fuel_amount = 0.0104*(pulse_time_counter / 1000000.0);// there, "pulse_time_counter" I could change in duty cycle.
       Litrazas = (Litrazas + Fuel_amount);
      
       Distance = 100/(lfrq/5000.0);//5000!

       Moment = (Fuel_amount * Distance);
       speed_sensor_counter = 0;
       pulse_time_counter = 0;

       }
                  u8g.firstPage();  
          do {
               draw();
          } while( u8g.nextPage() );
}

However, this code is working incorrectly, because I have calculated that the "Moment" should be 26, instead it(arduino, ar kas? nu žodžiu įsirašyk pats vietoj "it" kas čia yra) counts only to 24. "Moment" data is sent to LCD, which works in a manner of u8g library. Maybe I should use Time interrups for "pulse_time_counter" in one 1s, rather than using PulseIn function? Instead of using "pulse time counter" I'm ok with interrupt calculating "duty cycle" expressed as a percentage.
« Last Edit: September 30, 2013, 09:51:59 am by Gytis » Logged

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

Please help me!!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
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) please use CTRL-T before posting, that makes your code far more readable.
2) please post the whole code, noone can compile snippets.
3) provide links to libraries used, not every one has the same as you.

Logged

Rob Tillaart

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

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

Code:
#include "U8glib.h"
 U8GLIB_ST7920_128X64 u8g(7,9,8, U8G_PIN_NONE);
 #include <DS1302.h>
 DS1302 rtc(2, 3, 4);
#include "FreqPeriod.h"

double lfrq;
long int pp;
volatile unsigned long injector = 0;
long pulse_time_counter = 0;

    long previousMillis = 0;        // will store last time LED was updated
    long interval = 1010;
  
      volatile unsigned long speed_sensor = 0;
        volatile unsigned long speed_sensor1 = 0;
    static float Fuel_amount = 0;
    static float Distance = 0;
    static float Distance_1 = 0;
    static float Moment = 0;
    static float freq;
    static float Litrazas = 0;
    static float Kilometrazas = 0;

   long speed_sensor_counter = 0;

 void draw (void) {
  
    u8g.setFont(u8g_font_5x7);
    u8g.drawStr(73,10,"Time");
    u8g.setFont(u8g_font_fixed_v0);
    u8g.setPrintPos(96,10);
    u8g.print(rtc.getTimeStr(FORMAT_SHORT));
  
 if (Moment < 10.0)   {
        if (Litrazas < 10.0) {            
        // sitas L/km paisyti.good one
        u8g.setFont(u8g_font_freedoomr25n);
        u8g.setPrintPos(10,40);
        u8g.print(Moment, 1);
        u8g.setFont(u8g_font_6x10);
        u8g.drawStr(61,38,"L/");
        u8g.setFont(u8g_font_6x12);
        u8g.drawStr(72,38,"100km");
        //linija po skaiciais
        u8g.drawHLine(6,44,105);
        u8g.drawVLine(105,41,10);
        //economy
        u8g.setFont(u8g_font_5x7);
        u8g.drawStr(7,60,"Fuel_economy:");
        u8g.setFont(u8g_font_freedoomr10r);
        u8g.setPrintPos(75,62);
        u8g.print(Litrazas, 1);
        u8g.setFont(u8g_font_6x12);
        u8g.setPrintPos(97, 60);
        u8g.print("L");
        }
        else if (Litrazas > 10.0) {
        u8g.setFont(u8g_font_freedoomr25n);
        u8g.setPrintPos(10,40);
        u8g.print(Moment, 1);
        u8g.setFont(u8g_font_6x10);
        u8g.drawStr(61,38,"L/");
        u8g.setFont(u8g_font_6x12);
        u8g.drawStr(72,38,"100km");
        //linija po skaiciais
        u8g.drawHLine(6,44,105);
        u8g.drawVLine(105,41,10);
        //economy
        u8g.setFont(u8g_font_5x7);
        u8g.drawStr(7,60,"Fuel_economy:");
        u8g.setFont(u8g_font_freedoomr10r);
        u8g.setPrintPos(75,62);
        u8g.print(Litrazas, 1);
        u8g.setFont(u8g_font_6x12);
        u8g.setPrintPos(104, 60);
        u8g.print("L");
        }
    }  
      
    if (Moment > 10.0) {
          if (Litrazas < 10.0) {  
          u8g.setFont(u8g_font_freedoomr25n);
          u8g.setPrintPos(5, 40);
          u8g.print(Moment, 1);  
          u8g.setFont(u8g_font_6x10);
          u8g.drawStr(75,38,"L/");
          u8g.setFont(u8g_font_6x12);
          u8g.drawStr(86,38,"100km");
          //linija po skaiciais
          u8g.drawHLine(6,44,119);
          u8g.drawVLine(119,41,10);
          // Fuel
          u8g.setFont(u8g_font_5x7);
          u8g.drawStr(7,60,"Fuel_economy:");
          u8g.setFont(u8g_font_freedoomr10r);
          u8g.setPrintPos(75,62);
          u8g.print(Litrazas, 1);
          u8g.setFont(u8g_font_6x12);
          u8g.setPrintPos(97, 60);
          u8g.print("L");
          }
          else if (Litrazas > 10.0)
          {
          u8g.setFont(u8g_font_freedoomr25n);
          u8g.setPrintPos(5, 40);
          u8g.print(Moment, 1);  
          u8g.setFont(u8g_font_6x10);
          u8g.drawStr(75,38,"L/");
          u8g.setFont(u8g_font_6x12);
          u8g.drawStr(86,38,"100km");
          //linija po skaiciais
          u8g.drawHLine(6,44,119);
          u8g.drawVLine(119,41,10);  
          u8g.setFont(u8g_font_5x7);
          u8g.drawStr(73,10,"Time");
          u8g.setFont(u8g_font_fixed_v0);
          u8g.setPrintPos(96,10);
          u8g.print(rtc.getTimeStr(FORMAT_SHORT));
          u8g.setFont(u8g_font_5x7);
          u8g.drawStr(7,60,"Fuel_economy:");
          u8g.setFont(u8g_font_freedoomr10r);
          u8g.setPrintPos(75,62);
          u8g.print(Litrazas, 1);
          u8g.setFont(u8g_font_6x12);
          u8g.setPrintPos(104, 60);
          u8g.print("L");
          }
  }
 }



void setup() {
  Serial.begin(115200);
  FreqPeriod::begin();

}

void loop() {
  
  injector = pulseIn(11, HIGH);
  
  pp=FreqPeriod::getPeriod();
  if (pp ){

    lfrq= 16000400.0 / pp;
  }
  
    if (injector != 0)
            {

            pulse_time_counter += injector;
          
            }
            
          unsigned long currentMillis = millis();
       if(currentMillis - previousMillis > interval)
       {  

       previousMillis = currentMillis;  
      
       Fuel_amount = 0.0104*(pulse_time_counter / 1000000.0);//Cilindru skaicius is 0.0026=0,0104L

       Litrazas = (Litrazas + Fuel_amount);
      
       Distance = 100/(lfrq/5000.0);//5000!

       Moment = (Fuel_amount * Distance);
       speed_sensor_counter = 0;
       pulse_time_counter = 0;

       }
               u8g.firstPage();  
          do {
               draw();
          } while( u8g.nextPage() );
}

To test code, I use two generators in my university, each one giving 100Hz 50% pulse to arduino. Libraries: u8glib for lcd, for real time module I use DS1320 library and for frequency I'm using http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/. And finally, for pulse duty, I using pulseIn function. Can you sugesst for me, some library that could count pulse duty with arduino interrupt?
« Last Edit: September 30, 2013, 09:53:00 am by Gytis » Logged

Pages: [1]   Go Up
Jump to: