Flow sensor and two relays!

Hi guys
My project is simple ( but not for me)

I have an Arduino uno ,one FLUIDS / LIQUIDS FLOW SENSOR 5-15VDC 15mA and a relay board with 4 relays. only using two of the relays.
The reason for my question is as follows it keeps counting up. l/h.

I want the board to switch the relay 1 on when it detects water flow. when it no longer detects water flow the relay 1 mast stay engaged. once the water sensor detect water flow again relay 2 must switch in and relay 1 off. once it detects water flow again relay1 must switch in and relay 2 off.
here is my code:
</>
void setup() {

// put your setup code here, to run once:

pinMode(11,OUTPUT);

pinMode(12,OUTPUT);

pinMode(2, INPUT);
Serial.begin(9600);
attachInterrupt(0, pulse, RISING); // Setup Interrupt

}

void loop() {
// put your main code here, to run repeatedly:
flow = .00225 * pulse_freq;
Serial.print(flow, DEC);
Serial.println("L");
delay(500);

if((flow>0.5) && (count=0)) // You might have to play with this value
{

       digitalWrite(11,LOW);

       digitalWrite(12,HIGH);

       count++;

 }

else if(flow<2.5) // You might have to play with this value
 {

     digitalWrite(11,HIGH);

     digitalWrite(12,HIGH);

 }

else if((flow>0.5) && (count=1))

 { 

        digitalWrite(11,HIGH);

        digitalWrite(12,LOW);

        count=0;


  }

}

void pulse () // Interrupt function

{
pulse_freq++;
}
</>
what am i doing wrong?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

clicking this icon adds stuff to your post. paste your code in the highlighted area (you can edit your original post)

looks like there some gizmo that generates a pulse when water flows, and your ISR is counting pulses. wouldn't the pulse-frequency be the # of pulses / time period, not simply the # of interrupts which will constantly increase

sounds like the onset of water flowing toggles the two relays on/off with only one on at a time.

if this is true, needs to recognize the transition from no flow to flow. two booleans can be used to track current and previous flow to detect the onset of flow and toggle the relays

ok i am new to it all how would one do it!

i don't have your hardware and just used an analog input to represent flow. note difference in threshold to recognize flow

following demonstrates recognizing flow, the onset of flow and when to toggle relays

delay is just to minimize prints, but you could use it to measure freq

bool  flow;
bool  flowLst;

unsigned freq;

// -----------------------------------------------------------------------------
void loop ()
{
    freq = analogRead (A0);
    Serial.print (freq);

    flowLst = flow;
    if (! flow && 350 < freq)
        flow = true;
    else if (flow && 300 > freq)
        flow = false;

    Serial.print ("  ");
    Serial.print (flowLst);
    Serial.print ("  ");
    Serial.println (flow);
    if (flowLst != flow)  {
        if (flow)
            Serial.println (" flow start - toggle relays");
        else
            Serial.println (" flow stop");
    }

    delay (500);
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);
}

tryed it does nothing

do you have a pot on AO?

it demonstrates how to detect the the "onset" of flow. you need to incorporate the ides into your code

The reason why nothing happened is:

You can't just copy & paste gcjr-codes.

You have to adapt the code to your hardware

If you don't want to depend on the help for each and every tiny detail how to code it. It is about time to learn some basics.

and how to use this forum

Although the variable jumps around between 281 to 301 although no water is flowing

I See!

Should A0
not be pin 2 in my script!

no, pin 2 is an interrupt that presumably triggers on a pulse

i believe you code needs to also track the current and last "counts" that are incremented by the ISR (the ISR doe not count frequency) and the code needs to compute the frequency by dividing the the difference between the current and last counts and the time between computations.

the counts need to unsigned so the math works when the count wraps


the code you posted in incomplete, several of the variables are not defined (e.g. flow, count, pulse_freq)

would somebody help me and write some code please! pretty please!

Somehow, I'm thinking that a logic of
if flow goes up from zero to above zero alternate two relays
if flow goes up from zero to above zero alternate two relays again

is too simple for what it is used for.
If you miss one change from --- flow goes up from zero to above zero
everything will work the opposite way of as it should work.

Can you describe in detail what you want to do in the end?
What happends if relay1 is switched ON?
What happends if relay2 is switched ON?

I want to switch over a supply from one tank to another

Ok next round of ping-pong:
Does this mean
if tank_1 is empty switch over to tank_2

if tank_2 is empty switch over to tank_1?

no no each time it detects flow it must go from tank one and then it stops flowing then the next time it must flow in the other tank hope it makes sense!

tank_1 is empty relay1 is ON enabling flowing into tank_1
at some point tank_1 is completely filled ==> flow stops

flow starts again switch relay1 OFF switch relay2 ON

Though as tank1 is completely full and this made the flow stop how should ever the flow go above zero?

So what do you think: how many rounds of very short postings do want to play

until you give a detailed overview about your system saying very specific if your tanks are milk-tanks, ketchup-tanks or whatever ?
what makes the fluid flow ?
what makes the flow go down to zero?
etc. etc. etc.?




A few pics but i do no how to code it

  1. first time the water runs into tank 1 once it senses flow
  2. second time it senses flow it must switch over and flow to the other tank

Untitled