Stopping a program, start another, then go back

I'm new to Arduino and taking on a little project. I have a droid that I'm building out of scrap. I want his head to sweep left, right, then stop in the middle. Wait a few, then left, right, stop in the middle again.
To make this more difficult than the regular "sweep" example, I have IR sensors in his chin. I'd like for them to be able to detect motion, stop the sweep program and using the IR, follow movement until it's out of range, then go back to the left to right sweeps.

I see help on multiple sketches in one sketch, but I'm not sure that's exactly what I'm looking for. It would need to actually stop the "sweep" sketch, go to the IR motion detector sketch and when that was invalid (no more movement in front of the IR) it would go back to the sweep program.

Is this beyond the scope of an Arduino Uno?? Again, I'm new to this and maybe not using the correct search terms to find what I'm needing

Look into while loops. "While x is true do y"

For timing don't try to use delay instead look into the millis method which counts the number of milliseconds the Arduino has been running. By recording this to an unsigned long at a given point in your code you can then subtract it from the current time to see how much time had passed. If the desired amount of time has passed then you do something then reset your counter.

Sorry can't give you an example right now because I'm on my handheld computer.

Cheers,
Rhys

If the IR sensor is on the part of the head which rotates, how does it distinguish motion due to targets moving versus motion due to the sensor moving? It's not clear from your description, but maybe the sketch only examines the output of the sensor during the stop in the middle?

You definitely need to read up on the reference section about the control statements, if(), for() and while().

Thanks guys. I've never heard of the "while" statement, so a bit more reading is in order :slight_smile:

Sorry to throw cold water ...

WHILE is unlikely to be suitable because it blocks the Arduino from doing other things. WHILE and FOR are really only suitable for loops that takes a number of microseconds to complete.

Have a look at the demo Several Things at a Time and at the Thread Planning and Implementing a Program. Both of these use millis() to manage timing and use small functions to encapsulate different parts of the code.

Your program needs to do everything in a non-blocking way so that it can (for example) detect IR at the same time as it is moving a servo.

...R

I come from a programming background so I think I can help.

I can give you some pseudo-code that you can use for reference.

What you need is an outer while loop that contains an if statement, so its basically is checking a condition and it the condition is met, you execute the code inside, if not, you move on. Inside the if statement block you have another while loop that executes as long as that same condition is true. If it ever becomes untrue, then it stops and exits the while loop as well as the if statement. Put the sweep logic after the if statement. It will get executed whenever you aren't inside the inner loop, so whenever the condition is not met. Keep in mind that the outer loop needs to run repeatedly and constantly in order to continually check your conditions. That means that you should control the sweep by using if statement as well that check various things in order to turn pins on or off. So if a pin is on, causing the head to rotate, you want to check how much/long it has rotated to know whether you need to stop or reverse direction, etc. Once again, if the condition is met, then you have code inside that executes, if not then it skips it.

Pseudo (mock code):

while(true){

if(motionDetected){

//stop motion here, before loop.

while(motionDetected){

//do whatever you need it to do, or nothing if you want.

}
}

if(rotationToFar){

//reverse direction

}

}

Zephos:
What you need is an outer while loop

That's what the loop() function in the Arduino system is for.

...R

Robin2, thanks for pointing that out. I'm a programmer but I'm new to Arduino so I didn't recall that the loop function was actually built in, so to speak.

Zephos:
I’m a programmer but I’m new to Arduino so I didn’t recall that the loop function was actually built in, so to speak.

Well then, go install the Arduino IDE, get an Arduino UNO and start working through exercises with it, simply to develop familiarity with the system. Start having fun! :grinning:

TexasAutomatons:
I see help on multiple sketches in one sketch, but I’m not sure that’s exactly what I’m looking for. It would need to actually stop the “sweep” sketch, go to the IR motion detector sketch and when that was invalid (no more movement in front of the IR) it would go back to the sweep program.

Multiple modules in one sketch is precisely what you are looking for.

Now note - when you say “stop the sweep sketch”, what you mean is that this particular module - which effectively runs in a loop - contains at the point of looping, a “switch” to determine when to cease and proceed to the alternate function, and the same for that alternate function.

There are actually two ways to do this.

Noting that all programs (or “modules”) execute as a loop, you can either have the two occurring sequentially in your main loop, each in its own loop out of which it breaks in order to permit the other to take over, or you can make the main loop() the only loop, and on each pass, use a “switch” to decide whether one or the other function will be performed on that pass through the overall loop.

As you might guess, the latter approach is favoured as it is by far the most flexible since it permits and is required for “multi-tasking” where more than one - or two - functions are to be performed effectively simultaneously as necessary by alternately performing a single defined step in the process of each. This single step would correspond to a traversal of the operating loop of that particular process if it were the only process running in its own loop.

The consequence of this is that no process is permitted to “busy wait” - to loop in any way within itself dependent on any event. Any event which is waited upon is tested, and if it is not ready for action, then the code simply passes on to whatever code follows that is not dependent on that event in the secure expectation that after all other potential events have been “polled” the code will loop back around to this one.

This mind-set is absolutely critical to writing “real world” applications, so you need to learn it at the point of this exercise, particularly as you probably intend to add further “features”. :wink:

Paul, I have installed the ide and have already done a few things. Not much though.

Then you need to do a lot more! :grinning: