# Noob question: Doing a function() for x seconds

I just started with Arduino and Robotics in general, so forgive the extremely basic question. I developed an algorithm for Line following but I'm trying to improve on it now, with different methods of coding. I did it with 7 sensors and basic turning and path correction without analog input or PWM. Now I'm trying something else which involves me doing:

if(abc happens) { execute function(); for 10 seconds }

How do I do this? Which command/function?

I'm basically trying to differentiate between path deviation and right angle turns. As soon as I hit a right angle turn, I want the bot to rotate (via differential steering) in that direction for 'x' seconds. I know this would have problems with extremely acute turns (V turns), but I'm not tackling those right now.

The bot does turn right angles with simple path correction too, but it's motion is erratic and not-so-pretty.

Any other suggestion is welcome :)

Hmm, I think youd be better of with the swithc case() statement(http://arduino.cc/en/Reference/SwitchCase) and I dont get why and if you would want it to be executed for 10 sec. Could you post the code?

There are two ways to approach this problem:

The delay(msec) function does just what its name implies. It gives up control which is returned msec milliseconds later. Coding delay(10000); will provide a 10-second delay.

The other approach is to use the millis() function which returns a millisecond (up-)time, so that you can read this value, compute an ending time, and use it however needed to provide interval timing:

``````   finish = millis() + 10000;

/* Do whatever you want to do that takes less than 10 seconds */

while (millis() != finish) /* spin your wheels */ ;
``````

Any other suggestion is welcome

I would suggest you work through some of the example sketches in the IDE, in particular, the 'blink without delay' sketch.

I would go so far as to suggest it should be a requirement for joining the forum.

What does the robot do during those 10 seconds ? Is it acceptable to stop the program for 10s or does it have to monitor other inputs while it's in the "steering" state ?

I would suggest you work through some of the example sketches in the IDE, in particular, the 'blink without delay' sketch.

I would go so far as to suggest it should be a requirement for joining the forum.

Amen. :stuck_out_tongue_closed_eyes:

Are you suggesting an admission test for first timers ? :P

I'm giving it a lot of thought!

I remembered one user who came back to the forum only about ten minutes after being pointed at "blink without delay" for the first time, complaining s/he didn't understand it. My reply was that it took most people several hours to not understand it, and that they should persist. ]:)

Not necessarily. But there are clearly far too many new members that get an Arduino, try and do something too complicated for their first project, and come on here and ask for help, without learning the basics.

In my opinion, before posting here, users should have a basic understanding of a few things:

1. Most important - Serial.print(), the serial monitor and meaningful output, for debugging purposes.
2. Using millis() for timing, e.g. blink without delay.
3. How to post code - I think there should perhaps be a sticky for this.
4. How to search the forum/google.

My two(insert appropriate currency symbol).

Apologies to cyanide911, for hijacking his thread.

My reply was that it took most people several hours to not understand it, and that they should persist.

I have GOT to stop reading AWOL's posts while drinking anything. Tea splattered everywhere...

ROTFL!

My reply was that it took most people several hours to not understand it, and that they should persist.

I think that came out wrong - I'm not doing noobs down for not understanding a particular concept, just saying that they should stick at it, before posting "I don't get it".

I think that came out wrong

I understood what you said, and what you meant. In the context, it was a perfectly appropriate response. Out of context, it was funny as heck.

That came out pretty nice, IMHO

Seriously, though, what I sometimes find annoying are not “stupid” questions (I’m sure I’ve posted some too in the past, and probably will do in the future…) but people that seem to just want their homework done, without putting any effort in actually reading, understanding and trying the solutions that are proposed to them.

Seriously, though, what I sometimes find annoying

Must be an age thing. The "just need my homework done" crowd all seem to be young. The "study til you understand it" crowd all seem to be older people. At least as far as I can tell with no ability to discern age on the forum.

Ugh… you’re saying I just put myself into the “old” group ?

Heh, not a very friendly place, is it.

Forgive me for asking such a basic question. Should probably have kept researching. Will come back later when I don't need as much help with my code ;) (paradox, anyone?)

Anyway, thanks for all those who helped, I'll work on it.

not a very friendly place, is it.

Au contraire.

However, some questions come up almost daily, when a little research would have saved everyone time.

Maybe there should be a "Noobs start here" section of the forum, with lots of stickies.

Heh, not a very friendly place, is it.

It's a very friendly place when you tell us a little about yourself, and show that you are willing to try to learn.

Forgive me for asking such a basic question.

OK. You're forgiven.

Should probably have kept researching.

Always a good idea. Not the only avenue available, but a good one.

Asking questions here is fine. Even basic ones. Sometimes we get a little testy when people don't listen to suggestions (not going to mention any names, but if I were cutebuddy comes to mind).

Can you forgive us?

Heh, not a very friendly place, is it.

I think we got far too OT (I feel guilty). The OT part was not directed at you IMHO.

I'll make my attempt at steering back the topic to your original question, and try to be friendlier ;-)

``````finish = millis() + 10000;

/* Do whatever you want to do that takes less than 10 seconds */

while (millis() != finish) /* spin your wheels */ ;
``````

I think the != operator should be replaced with >=

Also let me repeat my question: what is the robot doing during those 10 seconds ?

Finally, a general advice: usually it's easier to help if the question is followed by the (possibly full) code it refers to.

HTH

:)