Pages: [1]   Go Down
Author Topic: My functions do not operate in order, why?  (Read 525 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I am having an issue with my code. I have a mobile robot and a webcam to search for an object, the robot centers itself to the object and moves forward to some amount. However, what the codes does is run the functions pan() and move(), at the same time. I would like it to run the pan() for some amount than move() for some other amount. I am not sure where to begin, I've read a few on the millis() and tried it out but it still didn't help. Any help would be appreciated. I posted my loop to show where I would like this to happen.

Code:
void loop()
{
   readNextByte();
    
//============================================================================
// Servo/Motor Control Algorithms
//============================================================================
// Print back parsed data

    if(count == 3)
    {    
       for (int i = 0; i < 3; i++)
       {
          Serial.print(command[i]);Serial.print(var[i]);Serial.print(' ');
       }
              
// Pan servo algorithm
    
       pan();
       move();
   }

}
« Last Edit: February 18, 2011, 06:48:43 pm by Warrior » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe use your pan function then a delay(1000); and then goto move();

// Pan servo algorithm
   
       pan();
       delay(1000);          // delay 1 second
       move(); 
 

or use the do while loop until a condition is met.

Hope that helps
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've also tried to delay but the robot functions very slow. Any delay I add only slows down the robot, plus anything in my function.
« Last Edit: February 18, 2011, 06:49:57 pm by Warrior » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok does it run correctly without delay();

and say get rid of move();
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I do remove the "move();" and the delay. The robot will pan correctly.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

not the prettiest but try that maybe?

if(x != 5)
{
pan();
x++
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried your method but it still runs both functions, now it moves forward and tries to pan at the same time.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45973
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
However, what the codes does is run the functions pan() and move(), at the same time.
No, it doesn't. The Arduino can only do one thing at a time. If pan() starts some motor(s) turning, and then ends, and move() starts some motor(s) turning, and then ends, it will appear that both things are happening at the same time, but they did not.

Without seeing your pan() and move() functions, we're just guessing at how to help you.
Logged

Pages: [1]   Go Up
Jump to: