Go Down

Topic: My functions do not operate in order, why? (Read 703 times) previous topic - next topic

Warrior

Feb 19, 2011, 12:32 am Last Edit: Feb 19, 2011, 12:48 am by Warrior Reason: 1
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: [Select]
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();
  }

}


MetroExpress

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

Warrior

#2
Feb 19, 2011, 12:46 am Last Edit: Feb 19, 2011, 12:49 am by Warrior Reason: 1
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.

MetroExpress

Ok does it run correctly without delay();

and say get rid of move();

Warrior

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

MetroExpress

not the prettiest but try that maybe?

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

Warrior

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

PaulS

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.

Go Up