DoubleWaterMeterPulseSensor on 1 Node v2.0



  • Please help me to compile this scetch for hot (HW_) and cold (CW_) water meter on 1 arduino.
    I use the original code from Water Meter Pulse Sensor, but I dont know how to fix "PULSE_FACTOR" - error

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    
    #include <MySensors.h>
    
    
    #define HW_DIGITAL_INPUT_SENSOR 2                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
    #define CW_DIGITAL_INPUT_SENSOR 3                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
    
    
    #define HW_PULSE_FACTOR 1000                       // Nummber of blinks per m3 of your meter (One rotation/liter)
    #define CW_PULSE_FACTOR 1000                       // Nummber of blinks per m3 of your meter (One rotation/liter)
    
    #define SLEEP_MODE false                        // flowvalue can only be reported when sleep mode is false.
    
    #define MAX_FLOW 40                             // Max flow (l/min) value to report. This filters outliers.
    
    #define CHILD_ID_HW 1                              // Id of the sensor child
    #define CHILD_ID_CW 2                              // Id of the sensor child
    
    
    unsigned long SEND_FREQUENCY =
        30000;           // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    
    MyMessage flowMsg1(CHILD_ID_HW,V_FLOW);
    MyMessage flowMsg2(CHILD_ID_CW,V_FLOW);
    
    MyMessage volumeMsg1(CHILD_ID_HW,V_VOLUME);
    MyMessage volumeMsg2(CHILD_ID_CW,V_VOLUME);
    
    MyMessage lastCounterMsg1(CHILD_ID_HW,V_VAR1);
    MyMessage lastCounterMsg2(CHILD_ID_CW,V_VAR1);
    
    double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
    
    
    volatile unsigned long pulseCount = 0;
    volatile unsigned long lastBlink = 0;
    volatile double flow = 0;
    bool pcReceived = false;
    unsigned long oldPulseCount = 0;
    unsigned long newBlink = 0;
    double oldflow = 0;
    double volume =0;
    double oldvolume =0;
    unsigned long lastSend =0;
    unsigned long lastPulse =0;
    
    void setup()
    {
        // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
        pinMode(HW_DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
         // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
        pinMode(CW_DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
    
        pulseCount = oldPulseCount = 0;
    
        // Fetch last known pulse count value from gw
        request(CHILD_ID_HW, V_VAR1);
         request(CHILD_ID_CW, V_VAR1);
    
        lastSend = lastPulse = millis();
    
        attachInterrupt(digitalPinToInterrupt(HW_DIGITAL_INPUT_SENSOR), onPulse, FALLING);
           attachInterrupt(digitalPinToInterrupt(CW_DIGITAL_INPUT_SENSOR), onPulse, FALLING);
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Water Meter", "1.1");
    
        // Register this device as Waterflow sensor
        present(CHILD_ID_HW, S_WATER);
        present(CHILD_ID_CW, S_WATER);
    }
    
    void loop()
    {
        unsigned long currentTime = millis();
    
        // Only send values at a maximum frequency or woken up from sleep
        if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
            lastSend=currentTime;
    
            if (!pcReceived) {
                //Last Pulsecount not yet received from controller, request it again
                request(CHILD_ID_HW, V_VAR1);
                request(CHILD_ID_CW, V_VAR1);
                return;
            }
    
            if (!SLEEP_MODE && flow != oldflow) {
                oldflow = flow;
    
                Serial.print("l/min:");
                Serial.println(flow);
    
                // Check that we dont get unresonable large flow value.
                // could hapen when long wraps or false interrupt triggered
                if (flow<((unsigned long)MAX_FLOW)) {
                    send(flowMsg1.set(flow, 2));                   // Send flow value to gw
                    send(flowMsg2.set(flow, 2));                   // Send flow value to gw
                }
            }
    
            // No Pulse count received in 2min
            if(currentTime - lastPulse > 120000) {
                flow = 0;
            }
    
            // Pulse count has changed
            if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
                oldPulseCount = pulseCount;
    
                Serial.print("pulsecount:");
                Serial.println(pulseCount);
    
                send(lastCounterMsg1.set(pulseCount));                  // Send  pulsecount value to gw in VAR1
                 send(lastCounterMsg2.set(pulseCount));                  // Send  pulsecount value to gw in VAR1
    
                double volume = ((double)pulseCount/((double)PULSE_FACTOR));
                if ((volume != oldvolume)||(!SLEEP_MODE)) {
                    oldvolume = volume;
    
                    Serial.print("volume:");
                    Serial.println(volume, 3);
    
                    send(volumeMsg1.set(volume, 3));               // Send volume value to gw
                    send(volumeMsg2.set(volume, 3));               // Send volume value to gw
                }
            }
        }
        if (SLEEP_MODE) {
            sleep(SEND_FREQUENCY);
        }
    }
    
    void receive(const MyMessage &message)
    {
        if (message.type==V_VAR1) {
            unsigned long gwPulseCount=message.getULong();
            pulseCount += gwPulseCount;
            flow=oldflow=0;
            Serial.print("Received last pulse count from gw:");
            Serial.println(pulseCount);
            pcReceived = true;
        }
    }
    
    void onPulse()
    {
        if (!SLEEP_MODE) {
            unsigned long newBlink = micros();
            unsigned long interval = newBlink-lastBlink;
    
            if (interval!=0) {
                lastPulse = millis();
                if (interval<500000L) {
                    // Sometimes we get interrupt on RISING,  500000 = 0.5sek debounce ( max 120 l/min)
                    return;
                }
                flow = (60000000.0 /interval) / ppl;
            }
            lastBlink = newBlink;
        }
        pulseCount++;
    }```
    
    _2WaterMeterPulseSensor:75: error: 'PULSE_FACTOR' was not declared in this scope
    
    C:\Users\Барков\Desktop\_2WaterMeterPulseSensor\_2WaterMeterPulseSensor.ino: In function 'void setup()':
    
    _2WaterMeterPulseSensor:105: error: 'onPulse' was not declared in this scope
    
    C:\Users\Барков\Desktop\_2WaterMeterPulseSensor\_2WaterMeterPulseSensor.ino: In function 'void loop()':
    
    _2WaterMeterPulseSensor:163: error: 'PULSE_FACTOR' was not declared in this scope
    
    exit status 1
    'PULSE_FACTOR' was not declared in this scope
    

  • Mod

    @tjay4x4 on a mobile device now so I can't verify but I think changing

    define HW_PULSE_FACTOR 1000                       // Nummber of blinks per m3 of your meter (One rotation/liter)
    #define CW_PULSE_FACTOR 1000                       // Nummber of blinks per m3 of your meter (One rotation/liter)
    

    to

    define PULSE_FACTOR 1000                       // Number of blinks per m3 of your meter (One rotation/liter)
    

    will be sufficient.



  • @mfalkvidd Thank you for reply!
    I'm trying this before, when I change it to

    define PULSE_FACTOR 1000
    
    
    

    I get this red line in the code;

     attachInterrupt(digitalPinToInterrupt(HW_DIGITAL_INPUT_SENSOR), onPulse, FALLING);
     attachInterrupt(digitalPinToInterrupt(CW_DIGITAL_INPUT_SENSOR), onPulse, FALLING);
    
    C:\Users\Барков\Desktop\_2WaterMeterPulseSensor\_2WaterMeterPulseSensor.ino: In function 'void setup()':
    
    _2WaterMeterPulseSensor:105: error: 'onPulse' was not declared in this scope
    
    exit status 1
    'onPulse' was not declared in this scope
    ```

Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 6
  • 1
  • 3
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts