water flowmeter activating relay, //help

Hello there!

im trying to making a flow alarm, when there is flow a relay is activated
i found a code on the net i customized

i need help to get the relay output in the script

and i am new to this,
hope a kind person what to help me out :=)

const int RelayPin = 13;       // pin that the Relay is attached to



volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
  flow_frequency++;
}
void setup()
{

  // initialize the LED pin as an output:
  pinMode(RelayPin, OUTPUT);
  
  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
  Serial.begin(9600);
  attachInterrupt(0, flow, RISING); // Setup Interrupt
  sei(); // Enable interrupts
  currentTime = millis();
  cloopTime = currentTime;
}
void loop ()
{
  currentTime = millis();
  // Every second, calculate and print litres/hour
  if (currentTime >= (cloopTime + 1000))
  {
    cloopTime = currentTime; // Updates cloopTime
    // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
    l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
    flow_frequency = 0; // Reset Counter
    Serial.print(l_hour, DEC); // Print litres/hour
    Serial.println(" L/hour");
  }

}

+1 Karma for using code tags in your first post.

You ask for help but you do not say what you need help with. Does the code compile? Can you upload it? Does it run? What happens? What did you want to happen?

Also post a link to the flow sensor, the relay and tell us what type of Arduino you are using. Use the "link" icon so your links can be clicked.

sorry i was to fast, by the way i need help to place the output for the relay in the Script

i got the script to work my self :slight_smile:

const int RelayPin = 13;       // pin that the Relay is attached to
const int threshold = 400;


volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
  flow_frequency++;
}
void setup()
{

  // initialize the LED pin as an output:
  pinMode(RelayPin, OUTPUT);
  
  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
  Serial.begin(9600);
  attachInterrupt(0, flow, RISING); // Setup Interrupt
  sei(); // Enable interrupts
  currentTime = millis();
  cloopTime = currentTime;
}
void loop ()
{
  currentTime = millis();
  // Every second, calculate and print litres/hour
  if (currentTime >= (cloopTime + 1000))
  {
    cloopTime = currentTime; // Updates cloopTime
    // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
    l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
    flow_frequency = 0; // Reset Counter
    Serial.print(l_hour, DEC); // Print litres/hour
    Serial.println(" L/hour");
  }
   if (l_hour > threshold) {
    digitalWrite(RelayPin, HIGH);
    Serial.println("flow");
  } else {
    digitalWrite(RelayPin, LOW);
  }

}

They are enabled by default, so no need for this  sei(); // Enable interrupts

If you want to avoid rollover issues in ~50 days, don't write your test against millis() this way  if (currentTime >= (cloopTime + 1000)) as cloopTime + 1000 will overflow at some point. You should prefer the version with a subtractionif (currentTime - cloopTime >= 1000)

You might need to disable interruptions for a very short while, the time to copy flow_frequency into a local cached variable. If you don't do this, as interrupts keep running, you might end up with wrong calculation because the 2 bytes of your int will have changed whilst you do the math.

how do i disable interruptions ?

cli() if you want to match the sei() call you had… but those are AVR specific. if you want portability you should use noInterrupts() and interrupts() to activate them again

noInterrupts();
int flow_frequency_copy = flow_frequency ;
flow_frequency = 0;
interrupts();
// then do the math with the copy.

you could even record the time when you set flow_frequency to zero and not use a fixed frequency of 1000 to calculate exact duration, you maths would be more precise.