Go Down

Topic: counting pulses (Read 528 times) previous topic - next topic

thill86

Hi!

I am a newbie with programmin.

I would like to make fuel consumption meter to a boat.(carburetor, so I need to measure only one flow)

I have a starter kit with arduino uno, my flow sensor hasnt arrived yet.

Flow sensor gives approximitely 2500 pulses per litre.

I have SOME experience with c-lanquage, but the stucture on arduino IDE  with its void setup() and void loop() is kind of restrictive with programming.
At least to me :)

Can anyone give me some pointers and hints, or complete and tested code :P

adwsystems

This may not be your flow meter but it has an example you can you use for a basis on how to count pulses and count pulses per second.

Robin2

but the stucture on arduino IDE  with its void setup() and void loop() is kind of restrictive with programming.
At least to me :)
The purpose is to make things easier.  What have you in mind that makes you feel it is restrictive?

What is the maximum number of pulses per second that you need to deal with?

Do you want to count the pulses or measure the time interval between them? Either or both is possible.

One possibility is to use an interrupt to detect the pulses. Read about attachInterrupt() in the Reference section.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Paul__B

I would like to make fuel consumption meter to a boat. (carburettor, so I need to measure only one flow)

I have a starter kit with Arduino UNO, my flow sensor hasn't arrived yet.

Flow sensor gives approximately 2500 pulses per litre.
OK, so the flow is clearly far less than one litre per second, so you are unlikely to need to complicate matters with interrupts (unless you use an LCD  :smiley-eek: ).  Always preferable not to.

The details you  must supply are exactly what you want it to indicate - consumption rate, total consumption, or both, or what else?

Now, how do you want it to indicate?  Bar graph?  LCD display?  Analog meter?  Output to computer?  Something else?  Braille?  :smiley-lol:

I have some experience with C-language, but the structure on Arduino IDE  with its void setup() and void loop() is kind of restrictive with programming.
At least to me :)
Which means you really do not understand "real time" programming and presumably, were doing something entirely different on a PC.

Most microcontroller applications - and yours is a perfect example - are "turnkey".  They start up at power on - that is the "setup()" - and then continuously execute a process which is the "loop()".  It is a loop because it never stops.  Well, it stops when you turn the power off and you may have to figure out how to deal with that, mainly in saving accumulated data like an odometer.

These two functions are "void" because they never return any result to another function - setup() flows on into the loop() and loop() never stops, so it has no return status.

How's that?

Robin2

If you are interested in using interrupts to detect the pulses (I probably would) then this simple program may be of interest. I use it with a small DC motor with a simple optical detector that produces one pulse every revolution. The general concept can be applied to most situations where pulses need to be detected.

Code: [Select]

const byte fwdPin = 9;
const byte revPin = 10;
const byte potPin = A1;

int potVal;
int pwmVal;

unsigned long revMicros;
unsigned long prevRevMicros;
unsigned long revDuration;
unsigned long revCount;

unsigned long prevDisplayMillis;
unsigned long  displayInterval = 1000;

    // variables for the ISR
volatile unsigned long isrMicros;
volatile unsigned long isrCount;
volatile bool newIsrMicros = false;



void setup() {
    Serial.begin(115200);
    Serial.println("SimpleISRdemo.ino");
    pinMode (fwdPin, OUTPUT);
    pinMode (revPin, OUTPUT);

    isrCount = 0;
    attachInterrupt(0, revDetectorISR, RISING);
}

//==========

void loop() {
    getIsrData();
    if (millis() - prevDisplayMillis >= displayInterval) {
        prevDisplayMillis += displayInterval;
        showData();
        readPot();
        updateMotorSpeed();
    }
}

//===========

 void readPot() {
    potVal = analogRead(potPin);
}

//===========

void updateMotorSpeed() {
    pwmVal = potVal >> 2;

    digitalWrite(revPin,LOW);
    analogWrite(fwdPin, pwmVal);
 }

//===========

void getIsrData() {
    if (newIsrMicros == true) {
        prevRevMicros = revMicros; // save the previous value
        noInterrupts();
            revMicros = isrMicros;
            revCount = isrCount;
            newIsrMicros = false;
        interrupts();
        revDuration = revMicros - prevRevMicros;
    }
}

//===========

void showData() {
    Serial.println();
    Serial.println("===============");
    Serial.print("PWM Val "); Serial.println(pwmVal);
    Serial.print("  Rev Duration ");
    Serial.print(revDuration);
    Serial.print("  Rev Count ");
    Serial.print(revCount);
    Serial.println();
}

//===========

void revDetectorISR() {
    isrMicros = micros();
    isrCount ++;
    newIsrMicros = true;

}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Railroader

I used an impeller flow meter a long time ago. That sensor was highly nonlinear especially at low flow rates. Check the linearity of Your sensor.
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

Railroader

I used an impeller flow meter a long time ago. That sensor was highly nonlinear especially at low flow rates. Check the linearity of Your sensor.
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

UKHeliBob

Quote
the stucture on arduino IDE  with its void setup() and void loop() is kind of restrictive with programming.
Whilst I don't agree with that statement, you don't have to use the setup() and loop() functions if you don't want to.  You can write your own main() function, initialise the hardware and read the sensor(s).  It is, however, much easier to use setup() and loop() and have the Arduino environment add the code to do the difficult stuff for you

I assume that you realise that you can write your own functions and call them from setup() and loop()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up