Avoiding Delay()

I have been building an aquarium controller for a bit. All working but finally decided to get rid of the dreaded delay function for pump timings as it stops the whole program while a pump is running, not good.

I have been using a modified version of the "blink without delay" code see below. I have that working but when I introduce state and a counter I get a very strange on off effect with my pump for the interval time. So it works as a time but the pump goes on and off rapidly for the correct interval, anyone have any ideas why?

to explain code, buzz delay is a web button variable that when I click button on webpage posts value of 8000 to arduino web server and sends pin LOW or pump on, when set to 0 it turns pin HIGH or pump off.

I write the ledstate for digital write in the main loop, it just picks up the state change.

if (buzzDelay == 8000){

if(currentMillis - previousMillis > interval) { // save the last time you blinked the LED previousMillis = currentMillis;

if (ledState == HIGH) ledState = LOW;

else //digitalWrite(BUZZER_PIN, HIGH); Serial.print("pump off"); ledState = HIGH;

var = 0; buzzDelay = 0;

}

}

digitalWrite(ledPin, ledState);

Right. Can you repost with all your code, and put it into code tags? I suspect some curly brackets are missing but with your layout it is hard to tell.

And use the auto-format facility in the IDE to make it clear what is done by each "if" statement.

file attached, cheers.

aquaV2V2.pde (21.5 KB)

I don't want to download a pde file which I then have to try and locate, and find a program to open it.

Just copy-and-paste your code between code tags so we can see it in front of us on the page.

a PDE file opens in arduino IDE. Posting on an arduino forum I thought that may have been a given? sorry if I am mistaken?

The code is too big to post, exceeds limit on forum. I attach a txt.

See new attachment.

Cheers

aquav2.txt (22.6 KB)

The point of posting it as something you can see is that it makes it easier to discuss.

Your average college lecturer doesn't stand at the front of the class holding up a USB memory stick, talking about the program that is on it, does he? No. He writes it up on the black/white/purple board for you to see.

If it is too large, then make a couple of posts out of it. Or, at least, post (in a nicely formatted form) the function where the fault is, and any variable definitions and functions that function relies on.

If it is too large, then make a couple of posts out of it. Or, at least, post (in a nicely formatted form) the function where the fault is, and any variable definitions and functions that function relies on.

Sorry, I don't agree with with that, especially the second part. If it's too big, then attaching it is fine. Most of us know where our browsers put files.

Why not just use pastebin.com to paste the long code? I'm on an iPad and downloading files to view is a PITA.

http://pastebin.com/

It really is that simple.

ilium007: Why not just use pastebin.com to paste the long code? I'm on an iPad and downloading files to view is a PITA.

http://pastebin.com/

It really is that simple.

Fair point about tablet devices, and another reason I don't own one, but pastebin requires another online account to save files.

locost-se7en: a PDE file opens in arduino IDE. Posting on an arduino forum I thought that may have been a given? sorry if I am mistaken?

The code is too big to post, exceeds limit on forum. I attach a txt.

That's OK, if it's too big, it's too big.

Now looking at the relevant part:

  if (buzzDelay == 8000){

    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;   

      if (ledState == HIGH)
        ledState = LOW;



      else 
        //digitalWrite(BUZZER_PIN, HIGH);
      Serial.print("pump off");
      ledState = HIGH;
      digitalWrite(ledPin, ledState);

      var = 0;
      buzzDelay = 0;


    }

The way you are writing makes me think you believe more than one statement is being executed on the "else" branch, almost as if you had written:

      if (ledState == HIGH)
        ledState = LOW;
      else 
        {
        //digitalWrite(BUZZER_PIN, HIGH);
        Serial.print("pump off");
        ledState = HIGH;
        digitalWrite(ledPin, ledState);
        }

Am I right? You know you need the curly braces if you want to do more than one thing.

dxw00d:

ilium007: Why not just use pastebin.com to paste the long code? I'm on an iPad and downloading files to view is a PITA.

http://pastebin.com/

It really is that simple.

Fair point about tablet devices, and another reason I don't own one, but pastebin requires another online account to save files.

Well how do you read and reply to Arduino forums on the train to work XD

Well how do you read and reply to Arduino forums on the train to work

I don't. I read and reply to them using my android phone while sat on a park bench watching the river flow by. :P

locost-se7en:
So it works as a time but the pump goes on and off rapidly for the correct interval, anyone have any ideas why?

You pin assignments aren’t unique, so if ledpin=3 is intended to identify the pin controlling the pump then #define BUZZER_PIN6 3 seems to refer to the same pin. Since you do digitalWrite(BUZZER_PIN6, HIGH); every time you notice that buzzDelay6 == 0, I expect you have one bit of code turning it on because buzzDelay == 8000 and another bit promptly turning it off again.

Some general comments on the code, in case you’re interested.

Every for, if, else, do, while should be followed by a { on a line on its own and a matching } below it indented by the same amount. There are various schools of thought, and other styles that produce more condensed code, but this style gives you the clearest indication of how your control structure was intended to work and with code like this that is way more important than anything else.

If you need to apply the same logic to more than two of something, you should consider managing it using an array. By the time you’ve got up to seven of these LEDs/buzzers/pumps/whatever the heck they are, this should be screaming USE ARRAYS at you. I have no idea how many copy/paste errors you have now or will introduce in future, but this is a great breeding ground for bugs.

The main reason your code was too big to post was the music you embedded in the middle. This is irrelevant to your problem and could have been deleted to make your sketch more readable. (The other reason was that you had seven copies of most of your logic.) As a courtesy to the people trying to make sense of your sketch, it would be nice to delete all commented-out code and delete all unused functions. Anywhere you have two blank lines together, delete one of them. Repeat as necessary.

Thanks PeterH

I will strip down my code and re post as it is quite messy, sorry everybody.

I will try a cut down version of my code with only the pump / PIN assignment for pin 3 and see if that holds it up.

Cheers

Thanks Nick as well. I did not realize that about curly braces with if and else statements. yes I was trying to get more than one thing executed. Good catch.

Nick, you are a Legend. It was the curly brackets. Works like a charm, thanks a million :slight_smile: