Pages: [1] 2   Go Down
Author Topic: State of confusion  (Read 1411 times)
0 Members and 1 Guest are viewing this topic.
Yankee In the South
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Welcome to the Good Life
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4690
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check out millis().


Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
#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.
Logged

Trying to keep my house under control http://www.desert-home.com/

Yankee In the South
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Welcome to the Good Life
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. ;-)
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4245
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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

Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quote
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.
Logged

Yankee In the South
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Welcome to the Good Life
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 smiley-grin


@WizenedEE
That was an awesome connotation smiley-wink


@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
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Trying to keep my house under control http://www.desert-home.com/

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You can't just drive a tutorial directly from an arduino I/O pin.
You can't?
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4690
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Yankee In the South
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Welcome to the Good Life
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


@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!
Logged

Pages: [1] 2   Go Up
Jump to: