State of confusion

Greetings all-

I am very new to the exciting world of Arduino. I have some programming experience and some electronics. This is very exciting to have just discovered a new hobby.

What I am trying to do should be simple. I've looked at the example projects and am working through the code tutorials. But now what should be getting easier is looking like it's more complex.

I want to use my Arduino to turn on a relay that will run a pump for 8 to 10 seconds and then wait about 10 minutes and do it again. Then read from a photo sensor (LDR?) and if there is no light, do it all again.

So looking at the Blink example I thought I would code it as a loop. Check the photocell, delay 8 minutes, turn on the relay for 8 seconds, turn it off, do it all again.

Then I started reading about different libraries, timer1, timer2, Fuse, bla bla bla... now I am thinking there must be a reason not to use Delay to do this or why would all these libraries exist. Timer seems useful as does the fuse thing. But it seems like they would make my code more complex than my noobie brain can handle.

So please help me. Is my project simple enough to just use delay? Is there a reason not to?

I've looked for examples of people using delay to control relays and I didn't find much. Which makes me think maybe I missed something.

Have a Blessed Day.

Check out millis().

Delay is fine. However, the major problem with it is that you can only do one thing. In your example, you could turn the pump on every few minutes with delay, but if you want to add in something to turn on the water heater depending on the temperature of the water, you would have to completely restructure your code to not use delay. However, if it's just four lines of code, you might as well just use delay() and then write the more complicated stuff if the need arises.

Bill_of_the_Midwest: So please help me. Is my project simple enough to just use delay? Is there a reason not to?

Yes, if you are just getting started then the project you describe is a good use of delay. As already mentioned, the major drawback of delay is that you can't do anything else while delaying. (Even that isn't entirely true.)

Take your plan and implement it. There's no substitute for the experience of learning. Just know that later on if you want to start doing multiple things or doing more during your "delays" then you'll need to make some changes.

Also, since you are using relays, make sure you read tutorials on them. You can't just drive a tutorial directly from an arduino I/O pin.

This kind of thing can drive you nuts. However, there is a library that can make this easy. Get TimeAlarms from the playground and use it.

You can set a timer to start the pump, and after starting it, set a timer to turn it off. To handle the light situation, have the turn on check your light sensor to see if it should turn on or not. I do this kind of thing with a light. I set a timer to turn it on and then inside the callback for the timer, I set a timer to turn it off. Then inside the timer to turn it off, set a timer to turn it on, see.

This way the arduino is just loafing along without having any delays to tie up the code. It could look something like this:

#include TimeAlarms.h

void setup(){
  Alarm.timerRepeat(30*60, turniton); //Suppose you want to turn it on every thirty minutes
}

void loop(){

do something in here that is fun
alarmDelay();  // you have to do this to update the timers
}

void turniton(){
  turn on the pump
  Alarm.timeOnce(10*60, turnitoff);  // set a run time timer
}

void turnitoff(){
  turn off the pump
}

This kind of thing is incredibly versatile. You can set events to go off based on days of the week and so forth. You can even base it on something like sundown. The basic timer functions will get you to a routine where you can check anything you want and set other timers depending on what you want to do. I run a little pump on odd days starting at the first of the year. I flash indicator lights at one second intervals to indicate something needs attention, etc. You can do all this without having the code just hang up and wait.

You all are awesome! This gives me lots of ideas on how I could do this. I'm going to check out the timers. Thanks so much for not making fun of my lack of skill at this point and for showing me the light. ;-)

It would be a lot less confusing if everyone would use the term delay() when they are referring to that specific function and plain old delay when they are talking about doing something after some elapsed time. The delay() function is one means for achieving a delay, but it ties up the entire processor in doing so. The millis() function mentioned previously can be used to achieve a delay while permitting the processor to do other things during that delay.

Quiz: What is misleading about the name of the 'Blink without delay' tutorial?

Don

I've found a useful place to use the delay() function is in the setup() function, where the blocking property of delay() rarely is a problem and the use of it makes for a simpler clear flow of intent.

Lefty

floresta: Quiz: What is misleading about the name of the 'Blink without delay' tutorial?

Well... it blinks... and it doesn't use delay... I give up.

The delay() function is one means for achieving a delay, but it ties up the entire processor in doing so.

Which is like the real connotations of the word, where a delay in a flight wastes everyone's time. Nobody says "I just bought my plane ticket but my flight is delayed until December," since they can do other things before December -- and doing one thing now (buying the plane ticket) and then doing another thing later (flying) is just like using millis in the Arduino language.

So the idea if I used millis() would be to store the value and then wait (somehow) for my 8 minutes to pass and then check millis again to see if 8 minutes in miliseconds had passed?

I really don't need to do anything else while I'm waiting. At least not right now. I just need to turn the pump on and off. But it would be nice to add a manual push button to make it run even if the time had not elapsed... and maybe have LED indicators of which point in the loop is/was.... Ikes my scope is creeping.

I'm starting to see how this goes. First you just want to do one little thing (which is very simple) and pretty soon you have an internet connected Arduino that you monitor using your Droid. Addictive isn't it? I think I'm going to like this. My silly little $30 timer/relay project could cost hundreds and cause much enlightenment.

@floresta We get it. It says without delay, but the point is having a delay without depending on delay(). Also, blinking without a delay wouldn't even be blinking at all. In my opinion, it's a matter of opinion :D

@WizenedEE That was an awesome connotation ;)

@Bill_of_the_Midwest Yes, that's the idea. Though you might not need to do it right now, trust me it would be good practice. In fact, ideally you should go through the examples, just to absorb those concepts and be able to imagine what you can or can't/shouldn't do, and/or how you should do it.

You might want to read and experiment with the easily available documentation. http://arduino.cc/en/Reference/Millis And try the examples provided after the explanation ("See also").

footswitch

In my opinion, delay() is cool and millis() is cool too. However, if you want to time things in seconds, and you want to do something else while you wait, like check to see if a button has been pushed, you want timers. delay() waits, millis() measures, timers time out and do things. The TimeAlarm library has timers and alarms. Alarms do things at a certain time, timers are just like oven timers, they run out and do things.

The blink example is a really, really good beginning example. But, it shouldn't set the tone for everything you do from then on. If you want to measure something, use millis(); if you want to do nothing for a period of time, use delay(); if you want a buzzer to sound in eight minutes, use a timer; if you want something to happen every Tuesday at 07:30, use an alarm.

And, don't get hung up on semantics. A heck of a lot of people here are reading this forum in their second or third language.

You can’t just drive a tutorial directly from an arduino I/O pin.

You can’t?

Bill_of_the_Midwest: So the idea if I used millis() would be to store the value and then wait (somehow) for my 8 minutes to pass and then check millis again to see if 8 minutes in miliseconds had passed?

I really don't need to do anything else while I'm waiting. At least not right now. I just need to turn the pump on and off. But it would be nice to add a manual push button to make it run even if the time had not elapsed... and maybe have LED indicators of which point in the loop is/was.... Ikes my scope is creeping.

I'm starting to see how this goes. First you just want to do one little thing (which is very simple) and pretty soon you have an internet connected Arduino that you monitor using your Droid. Addictive isn't it? I think I'm going to like this. My silly little $30 timer/relay project could cost hundreds and cause much enlightenment.

It depends on what you mean by pretty soon. The last 10 years went by pretty quick for me.

wait (somehow) might work like this;

From power on and stable running your sketch starts by running what you have in setup() and then it runs loop() over and over until you or something makes it stop.

Inside your loop() you might so far be -- checking conditions and turning indicator leds on/off -- having a look at the manual pump control button and if pressed run the pump a while -- seeing if another millisecond has gone by and if it's time to run the pump automatic, do so

And the loop() runs over and over maybe 100,000 or more times a second just watching for something to do.

If you want, you can run the MCU slower that 16,000,000 cycles/sec and probably still be overkill. It will save power and run cooler when you do.

footswitch: @Bill_of_the_Midwest Yes, that's the idea. Though you might not need to do it right now, trust me it would be good practice. In fact, ideally you should go through the examples, just to absorb those concepts and be able to imagine what you can or can't/shouldn't do, and/or how you should do it.

You might want to read and experiment with the easily available documentation. http://arduino.cc/en/Reference/Millis And try the examples provided after the explanation ("See also").

footswitch

I think need to do this project with all of the suggested methods. I can see that they all will provide good learning. Then I can move on to having it Twitter me each time it runs.

Thank you all again. My state of confusion is now clearly a world of possibilities. I'm reading tutorials and examples and I'l be back soon with code that has be stuck along the way. My first shot is going to be the simple delay loop.

Have a Blessed Day!

So the idea if I used millis() would be to store the value and then wait (somehow) for my 8 minutes to pass and then check millis again to see if 8 minutes in miliseconds had passed?

Not exactly. You read the current value of millis() and calculate the value that corresponds to your end time. Then, [u]as part of the other stuff that the processor is doing[/u], it should periodically check millis() looking for a value greater than the calculated end time value. That's how you deal with the 'somehow'.

Don

Make sure you use a relay rated for the pump you use. I blew up a relay trying to control a vacuum pump with it. It was a loud bang. :sweat_smile: There might be a value for peak current of the pump and shop for a relay 120% rated for that current is advised.

Hello again!

I’ve been hitting the books and I have one tried to write one of the functions I will need. Basically it’s a function In intend to call when I want to turn on a relay to run a pump. I have two questions.
First, from my main loop, how do I call this function? Can I just do someting like:

if (some conditon that is true);
pumprun

Then will it just run the function below? Also, did I write this function correctly? Am I getting this? (Ok I know that’s a lot more than 2 questions, but am I on the right track?) I ordered my Arduino from china and I’m still waiting. Been waiting so long that I found one on ebay that is in the US and I’m waiting for that one too… arrugh, I wish one of them would get here.

void pumprun() {

digitalWrite (pumpRelay, HIGH); // Turn on the pump
unsigned long StartMillis = millis(); // var to store the starting time
unsigned long currentMillis = millis(); // var to store the current time
unsigned long lastMillis = millis(); // var to store the last time we blinked the LED

While (currentMillis - startMillis < pumpRunTime) { // while the interval is less than pumpRunTime blink and check the button (Could be 10 second or 30 seconds)
unsigned long currentMillis = millis(); // save the last blink time for the LED

if (currentMillis - lastMillis > 1000) { //Blink the LED during Pump Run // blink one second
lastMillis = currentMillis; // save the last blink of the LED
if (ledState == LOW) // if the LED is off turn it on and vice-versa:
ledState = HIGH;
else
ledState = LOW;
digitalWrite(pumpRunLED, ledState); // Blink the pump running on or off
}

buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:

if (buttonState == HIGH) { // check if the pushbutton is pressed. if it is, the buttonState is HIGH:
pumpRuntime = 1; // turn end the pump running by wiping out the interval value and making the while loop false
}

digitalWrite (pumpRelay, LOW) // turn off the pump
digitalWrite (pumpRunLED, LOW) // Turn off the pumpRunLED just in case it was last on
}

Have a Blessed Day!

That's pretty much it. Your function call to pumprun isn't right though, this:

if (some conditon that is true);
       pumprun

should be this:

if (some conditon that is true) //got rid of semicolon here
       pumprun();

Although by the look of the code in your function, you already know this - typo?

You're missing a couple of semi-colons too, but the compiler will tell you about that.

Without seeing the rest of your code it's hard to say what is happening but I would be careful about using pumpruntime to stop the pump if the button is pressed. You can just use break to exit the while loop. Otherwise you will have to ensure that you set pumpruntime appropriately before each call to pumprun just in case you stopped it in the past with the button. Not a big thing - may not matter at all, but it looks like a potential bug waiting to bite you.

Please don’t post code that you haven’t even bothered verifying.

  While (currentMillis - startMillis < pumpRunTime) {        // while the interval is less than

This won’t compile. There is no While statement.

unsigned long currentMillis = millis();                      // var to store the current time
unsigned long lastMillis = millis();                         // var to store the last time we blinked the LED

  While (currentMillis - startMillis < pumpRunTime) {        // while the interval is less than pumpRunTime blink and check the button (Could be 10 second or 30 seconds)
    unsigned long currentMillis = millis();                  // save the last blink time for the LED

You have two variables in the function with the same name, currentMillis, but different scope. They are DIFFERENT variables. How do you know which one you are referring to?

     buttonState = digitalRead(buttonPin);           // read the state of the pushbutton value:

These are not very meaningful names. What is the switch (buttons are for shirts) for? You don’t have just one multipurpose switch do you?