counting pulses

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 :slight_smile:

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

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.

thill86:
but the stucture on arduino IDE with its void setup() and void loop() is kind of restrictive with programming.
At least to me :slight_smile:

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

thill86:
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 :astonished: ). 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? :grinning:

thill86:
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 :slight_smile:

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?

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.

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

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.

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.

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()