Particle DEV problem with Particle.variable

Hi,

I’m trying to us particle variable function.
However, it never transfer the data into my monitor. It just keep refreshing.

When I click refresh, it showed this image.

The following is the code I have. I found this online and add the particle variable function in it.
The data showed out fine through serial.

Thanks guys!

*The particle desktop IDE version is 1.18. I checked the update, it said it’s the latest.

    int flowPin = D1;    //This is the input pin on the Arduino
    double flowRate = 0;    //This is the value we intend to calculate.
    volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.

        void setup() {
          // put your setup code here, to run once:
          pinMode(flowPin, INPUT);           //Sets the pin as an input
          attachInterrupt(D1, Flow, RISING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
          Serial.begin(9600);  //Start Serial

          Particle.variable("Flow Rate", &flowRate, DOUBLE);
        }
        void loop() {
          // put your main code here, to run repeatedly:
          count = 0;      // Reset the counter so we start counting from 0 again
          interrupts();   //Enables interrupts on the Arduino
          delay (1000);   //Wait 1 second
          noInterrupts(); //Disable the interrupts on the Arduino

          //Start the math
          flowRate = (count * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL
          //flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
          //flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute

          Serial.println(flowRate);         //Print the variable flowRate to Serial
        }

        void Flow()
        {
           count++; //Every time this function is called, increment "count" by 1

        }

Disabling interrupts each way round loop() isn’t a good idea since between loop() iterations lots of other stuff - involving interrupts too - needs to be done.

The modern syntax is a bit simpler

  Particle.variable("Flow Rate", flowRate);

(I’d not use blanks in the name tho’)

1 Like

@wuchouk, you might want to try this:

        unsigned long sampleTime = 0;
        volatile int count = 0;

        void loop() {
          int count_snapshot;

          if (millis() - sampleTime >= 1000) {   // Skip if 1000ms has not elapsed          
            noInterrupts(); //Disable interrupts to copy and reset count
            count_snapshot = count;
            count = 0;      // Reset the counter so we start counting from 0 again
            interrupts();   //Enables interrupts on the Arduino
            //Start the math
            flowRate = (count * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL
            //flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
            //flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute

            Serial.println(flowRate);         //Print the variable flowRate to Serial
            sampleTime = millis();     // Reset sample timer for next interval
          }
       }

1 Like

Hi,

Sorry for the late reply. Can you elaborate more if I disable interrupts in the loop? what will happen?
I’m pretty new to the c++ programming.

And I found out what went wrong. it is Particle.variable(“Flow Rate”, &flowRate, double);
I missed a &.

Thanks

Hi @peekay123,

Sorry for late reply.
What is the difference to move noInterrupts() before interrupts()?

I don’t really understand the differences.

Thanks

A lot of the system functions and the controller itself relies on interrupts getting through as planned - if you block them for extended periods the system will become unstable.

Hence the new syntax which takes that burden off you :wink:

 Particle.variable("Flow Rate", flowRate);