Control at two objectives at the same time

motor_driver.pde (1769Bytes)

hi there LMR

ive studyed alot of the robots here and now finaly made my own one with arduino uno board and the SN754410SE chip

along with a servo i want my robot to ride and look if there's nothing on his pad wich might block'm.

 

now i got everything right except for this one issue:

(look to my script) i cant control the motors turn when the servo is stil movin I HAVE TO WAIT TILL THEY ARE IN RIGHT POSITION

so how can i end this wiattingsissue caz now he's bumping and riding to a object and than turn IT cant take the time of what the servo needs..

 

 

 

i hope you understand caz my english pretty sucs

thnx alot nickn4

Ok no problem

Look at what is actually happening in your code. You enter the main loop and first have to make a decision: If distance is too close, then turn --if it is far away, go forward. The problem is that you have not taken a reading yet. You don’t take your first reading until you get all the way down to the bottom of the main loop. Next, the 2 servo loops. You sweep the servo from one side to the other, and you take a reading at each step but nothing happens after that. Each time you take a reading (at each step) you should be comparing that reading to your threashold. The way it is now, you are only working with the very last reading of each sweep.

Servos usually use a range of 0-180 on an Arduino --I don’t know what this 190 is about. Also, 140 to 180 is a pretty narrow sweep.

I would put your servo sweeps into your main loop and run a too-close/ far-away check EVERY time you step your servo and check your reading.

ah yes ofcource i geus i can
ah yes ofcource i geus i can get it to work -SMILE- lots of tnxx, nickn4

now i have my code but

now i have my code but servo180(); wont be activated strangly

aslo now my servo is moving to left fast and to the right relay slow…

im pretty sure i’ve got every thing wired up correctly and its just the code

 

<code>

#include <Servo.h>

 

  int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)

  int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)

  int motor3Pin = 5;    // H-bridge leg 3 (pin 10, 3A)

  int motor4Pin = 6;    // H-bridge leg 4 (pin 15, 4A)

  int enablePin1 = 9;    // H-bridge enable pin

  int enablePin2 = 10;    // H-bridge enable pin

  int mustTurn = 1;

 

  int distance;

 

  //servo configs

  int lastPos = 180;

  int pos;

  int servoPin = 7;

  Servo myservo;

 

  void setup() { 

    // set all the other pins you’re using as outputs:

    pinMode(motor1Pin, OUTPUT); 

    pinMode(motor2Pin, OUTPUT); 

    pinMode(motor3Pin, OUTPUT);

    pinMode(motor4Pin, OUTPUT);

    pinMode(enablePin1, OUTPUT);

    pinMode(enablePin2, OUTPUT);

 

    // set enablePin high so that motor can turn on:

    digitalWrite(enablePin1, HIGH); 

    digitalWrite(enablePin2, HIGH); 

 

    Serial.begin(9600);

    myservo.attach(servoPin);

    myservo.write(180);

 

         distance = analogRead(5);

         if(distance > 410)

           {

             mustTurn = 1;

           }

  }

 

  void loop()

  {

    if(mustTurn = 1)

      {

        turn();

      }else{

        forward();

      }

   ////////////////////////////

 

   if(lastPos < 180)

     {

       servo130();

     }

   else

     {

       if(lastPos > 130)

         {

           servo180();

         }

         else

         {

           endedServoPos();

         }

     }

 }

 

void servo180(){

   for(pos = 130; pos < 180; pos++)

     {

       myservo.write(pos);

       lastPos = pos;

       distance = analogRead(5);

         if(distance > 410)

           {

             mustTurn = 1;

             break;

           }

       delay(10);

     }

     delay(100);

}

 

void servo130(){

   for(pos = 180; pos > 130; pos–)

     {

       myservo.write(pos);

       lastPos = pos;

       distance = analogRead(5);

         if(distance > 410)

           {

             mustTurn = 1;

             break;

           }

       delay(10);

     }

     delay(100);

}

 

void endedServoPos()

{

   for(pos = lastPos; pos > 130; pos–)

     {

       myservo.write(pos);

       lastPos = pos;

       distance = analogRead(5);

         if(distance > 410)

           {

             mustTurn = 1;

             break;

           }

       delay(10);

     }

}

 

void forward()

{

      digitalWrite(motor1Pin, LOW); 

      digitalWrite(motor2Pin, HIGH); 

      digitalWrite(motor3Pin, HIGH);

      digitalWrite(motor4Pin, LOW);

}

 

void turn()

{

      mustTurn = 0;

      digitalWrite(motor1Pin, HIGH); 

      digitalWrite(motor2Pin, LOW); 

      digitalWrite(motor3Pin, HIGH);

      digitalWrite(motor4Pin, LOW);

}

<code>

 

 

 

 

PS: how can i put this in some sort of code block so it cant wipe all space at this page out?

thnx nickn4