Counter to my code (Dead Man Switch)

Hi

I need to add a counter to my project.
(I posted about different parts of it in the last few weeks and this is sort of a new question)

My project is controling relay with buttons and sensors values conditions and those parts of the code work well.

I need to add a counter to my code and I want to see it counting on my lcd display.

I want it to count until it reach 360 and then "stateRELAY1 = LOW"

The counter need to start when a value reach 20. This value will reach 20 only for a few seconds BUT the counter need to continue counting even if the value is back below 20.

I tried with an "if" but since the "if" exist only for a few second my counter stop before it reach 360.

This is the part of the code that I tried and like I said it does count but only for the time
that the flowRate is bigger than 20.

  void counter(){
 
    if (flowRate > 20)
    {
    a ++;
    lcd_2.setCursor(11, 1);
    lcd_2.print(a);
    }

  }

When this will work I will want the following:

When flowRate is bigger than 20, start counting and lcd.print it until it reached 360 and when it's done, put stateRELAY1 to LOW and reset counter to 0 until flowRate is bigger than 20 again.

All this without stoping the rest of my code working.

My goal is to use this as a simple Dead Man Switch but I need to see the counting on the LCD display and because the flow sensor will normally see a Flow within every 360 counts, my RELAY1 will stay ON unless the flow doesn't work.

I hope my explanation are enough for the question. I didn't want to post all the code that is very long since my problem is only about counting.

I'm still learning coding and stuff with Arduino and sometime it's hard to know how to asked the good question to get good answers.

Many Thanks.

Create a Boolean variable called remember20, or whatever you like. Initialize it to false. Then:

if(value > 20) remember20 = true;

Now use remember20 to enable the counter.

Hi DougP

It look so easy when you say it :slight_smile: but can you take me by the hand a little.
I'm really new with this.

I learn a lot in two months but the way I do it is taking an existing code and
modify it to fit my needs.

So when I look at your suggestion I can see a little but I don't know yet how to
do it.

Thanks

What makes the counter count?

when the value of my Flowsensor reach 20, the counter should begin to count.

hddforensic:
when the value of my Flowsensor reach 20, the counter should begin to count.

So, if flowsensor >= 20 AND count signal* == true then increment counter.

  • count signal should be on only for one scan when the count impulse goes true. You may have add some change of state code to insure this.

Well, if I don't find a full example of this in a code somewhere, I won't be able to do it
because it's far from my actual knowledge.

hddforensic:
Well, if I don't find a full example of this in a code somewhere, I won't be able to do it
because it's far from my actual knowledge.

If you just want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

But studying and learning is what brings your actual knowledge closer to your requirements.

...R

I have no problem about paying. The best way for me to learn right now is by example or with a teacher. I check around here in Montreal for Arduino "coding" courses and the only thing I found was 3 days introduction and this is really not enough for what I'm doing.

I didn't know about the Gigs and Collaborations sections in the forum, I will go to see. Yesterday I posted my project on a site called Guru.com to hire someone to help me. I will see what kind of answers I get.

Ok I found it.

Well I received some help but it’s finally working.

Except for one thing.

My millis is not equal to 1 second. (I will put that question in another post)

void LcdCounter(){


    CounterMillis = millis();
    if (CounterMillis - CounterMillis1 >= interval2) {

    if (a < 40 && purge == 0 && stateRELAY1 == HIGH) // value of 40 for testing purpose.              
    {                                                                           // The final goal will be 360 seconds
    a ++; 
    lcd_2.setCursor(11, 1);
    lcd_2.print(a);
    Serial.println(CounterMillis); // for debug millis
    Serial.println(CounterMillis1); // for debug millis
    }
    
    else
    {
    if (purge == 1){
    a = 0;
    purge = 0;
    lcd_2.setCursor(11, 1);
    lcd_2.print(F("     "));
    }

    if (a >= 40) // value of 40 for testing purpose. 
                      // The final goal will be 360 seconds
    {
    stateRELAY1 = LOW;
    stateRELAY2 = LOW;
    stateRELAY3 = LOW;
    lcd_2.setCursor(11, 1);
    lcd_2.print(F("Purge"));
    }
    }
    CounterMillis1 = CounterMillis;
    }
}

hddforensic:
Except for one thing.

My millis is not equal to 1 second. (I will put that question in another post)

Don't start another Thread. It is much easier to help if all the info about your project is in one place.

Post your complete program.

...R