suggestions for basic pulse counters

05 Jan 2013

I'm looking to to send live flowrate data to the terminal window from a simple watermill flowmeter, giving 2500 pulses per litre. I thought I had it with a modification of the swissflow sf800 program, but I need to calibrate the flowrate as the viscosity in question changes daily. I created a simple means of adjusting the conversion factor from the keyboard to account for a measure test volume. All appeared to be working, start up the pump and the numbers go up to the region of the 800ml/min wanted. Adjust the count to get it as close as possible (it fluctuates a little as you would expect), turn it off and it drops to 0. But when I halved the pump power (and got a 400ml jug full in a minute) it picked up from zero and went straight to the region of 800ml again, everything looked to be working, accept that it kept coming to around that same set level. Any suggestions why the following code might do that? And I was looking to try a simpler implentation than the SF800 code, but I can't quite see how to employ the multitude of counter options. This must be simple for an mbed, but I'm new to all this! It's got a separate ATAN2 function going BTW

#include "counter.h"
#include "mbed.h"

Serial pc(USBTX, USBRX);
counter SF800 (p29);
Timer t_a, t_b;
AnalogIn z(p20);
AnalogIn y(p19);
AnalogIn x(p18);

int main() {

    t_a.start();
    t_b.start();
    int a_init_pulses = 0;
    int b_init_pulses = 0;
    float flowconvert = 2500;
    float flowrate = 0;
    float inclin = 0;
    float inc_offset = 180;
    int time_window = 1000;

    bool a_b_flip_flop = true; // if true use window a
    
    pc.printf("\n\r*************\n\rMBED restarted\n\r*************\n");
    
    while (1) {
        wait(0.25);
        
        if (pc.readable()) {
            switch(pc.getc()) {
                case '2': flowconvert += 100; break; 
                case '1': flowconvert -= 100; break;
                //case '0': inc_offset += 0.1; break;
                //case '9': inc_offset -= 0.1; break;
                case 'i': inc_offset += (90 - inclin); break;
            }
        }
            
        //pc.printf("After switch\n");
        
        if (a_b_flip_flop) {
            flowrate = (float)(SF800.getPulses() - a_init_pulses)/(flowconvert * t_a.read_ms() / 60000);
        }
        else {
            flowrate = (float)(SF800.getPulses() - b_init_pulses)/(flowconvert * t_b.read_ms() / 60000);
        }

        if (t_a.read_ms() > time_window) {
            // switch to window B
        
            //pc.printf("\rswitching to window B = %i pulses detected in window A\n", SF800.getPulses() - a_init_pulses);
        
            t_a.reset();
            a_b_flip_flop = false;
            a_init_pulses =  SF800.getPulses();
        }
        
        if (t_a.read_ms() > time_window/2 && !a_b_flip_flop) {
            // switch to window A
        
            //pc.printf("\rswitching to window A = %i pulses detected in window B\n", SF800.getPulses() - b_init_pulses);
        
            t_b.reset();
            a_b_flip_flop = true;
            b_init_pulses =  SF800.getPulses();
        }
        
        inclin = fmod((atan2(((y.read()-0.5)*10), ((x.read()-0.5)*10)) * 180.0 / 3.141592654) + inc_offset, 360);
       
        pc.printf("\rflowrate = %.3f ml/min", flowrate);
        pc.printf("    Inclination  %.1f degrees", inclin);
        //pc.printf(" inc_offset %f", inc_offset);
        pc.printf(" flowconvert %f", flowconvert);
        //pc.printf(" X value %f", x.read());
        //pc.printf(" Y value %f", y.read());
        //pc.printf(" Z value %f", z.read());
        //pc.printf(" X acc %f", (x.read()-0.502)*10);
        //pc.printf(" Y acc %f", (y.read()-0.494)*10);
        //pc.printf(" Z acc %f", (z.read()-0.502)*10);
    }
}