Go Down

Topic: Code sequencing issue (Read 3041 times) previous topic - next topic

aimes32

Nearly have it solved!

My code now reads:
Code: [Select]
// define the pins used by the Robot Shield
int lock = 5;            // lock is disable input - program will not run if lock is in place
                         // NOTE: DO NOT use pins 0 or 1 for the lock as this will stop
                         // you being able to program your robot!
                         
int speedA = 6;          // pin 6 sets the speed of motor A (this is a PWM output)
int speedB = 9;          // pin 9 sets the speed of motor B (this is a PWM output)
int dirA = 8;            // pin 8 sets the direction of motor A
int dirB = 7;            // pin 7 sets the direction of motor B

int lightleft;
int lightright;
int photoresistorleft = 5;  // define the pin for left photoresistor
int photoresistorright = 4;  //define the pin for right photoresistor
int IRpinLeft = 2;           // analog pin for reading the IR sensor
int IRpinRight = 3;           // analog pin for reading the IR sensor

int motorPin1 = 3;          // pin 2 on L293D
int motorPin2 = 5;          // pin 7 on L293D
int enablePin = 1;          // pin 1 on L293D

// define the direction of motor rotation - this allows you change the  direction without effecting the hardware
int fwdA  =  LOW;         // this sketch assumes that motor A is the right-hand motor of your robot (looking from the back of your robot)
int revA  =  HIGH;        // (note this should ALWAYS be opposite the fwdA)
int fwdB  =  LOW;         //
int revB  =  HIGH;        // (note this should ALWAYS be opposite the fwdB)

// define variables used
int dist;
int angle;
int vel;

void setup() {                             // sets up the pinModes for the pins we are using
              pinMode (dirA, OUTPUT);         
              pinMode (dirB, OUTPUT);
              pinMode (speedA, OUTPUT);
              pinMode (speedB, OUTPUT);
              pinMode(motorPin1, OUTPUT);
              pinMode(motorPin2, OUTPUT);
              pinMode(enablePin, OUTPUT);
              digitalWrite(enablePin, HIGH);
             }

             
void stop() {                              // stop: force both motor speeds to 0 (low)
              digitalWrite (speedA, LOW);
              digitalWrite (speedB, LOW);
              delay(1000);
             }           
             
void forward(int dist, int vel) {          // forward: both motors set to forward direction
              digitalWrite (dirA, fwdA);
              digitalWrite (dirB, fwdB);
              analogWrite (speedA, vel);   // both motors set to same speed
              analogWrite (speedB, vel);
              delay (dist);                // wait for a while (dist in mSeconds)
             }
             
void reverse(int dist, int vel) {          // reverse: both motors set to reverse direction
              digitalWrite (dirA, revA);
              digitalWrite (dirB, revB);
              analogWrite (speedA, vel);   // both motors set to same speed
              analogWrite (speedB, vel);
              delay (dist);                // wait for a while (dist in mSeconds)             
             }   

void rot_cw (int angle, int vel) {         // rotate clock-wise: right-hand motor reversed, left-hand motor forward
              digitalWrite (dirA, revA);
              digitalWrite (dirB, fwdB);
              analogWrite (speedA, vel);   // both motors set to same speed
              analogWrite (speedB, vel);
              delay (angle);               // wait for a while (angle in mSeconds)             
             }
             
void rot_ccw (int angle, int vel) {        // rotate counter-clock-wise: right-hand motor forward, left-hand motor reversed
              digitalWrite (dirA, fwdA);
              digitalWrite (dirB, revB);
              analogWrite (speedA, vel);   // both motors set to same speed
              analogWrite (speedB, vel);
              delay (angle);               // wait for a while (angle in mSeconds)             
             }
             
void turn_right (int angle, int vel) {     // turn right: right-hand motor stopped, left-hand motor forward
              digitalWrite (dirA, revA);
              digitalWrite (dirB, fwdB);
              digitalWrite (speedA, LOW);  // right-hand motor speed set to zero
              analogWrite (speedB, vel);
              delay (angle);               // wait for a while (angle in mSeconds)             
             }
             
void turn_left (int angle, int vel) {      // turn left: left-hand motor stopped, right-hand motor forward
              digitalWrite (dirA, fwdA);
              digitalWrite (dirB, revB);
              analogWrite (speedA, vel);
              digitalWrite (speedB, LOW);  // left-hand motor speed set to zero
              delay (angle);               // wait for a while (angle in mSeconds)             
             }
             
void motor3stop() {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
      delay(5000);
    }

void motor3reverse() {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, LOW);
    delay(500);
    }
   
void motor3forward() {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
      delay(500);
    }   

             
void loop() {
 
 
             
              lightleft = analogRead(photoresistorleft);
              lightright = analogRead(photoresistorright);
             
              float voltsLeft = analogRead(IRpinLeft)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
              float distanceLeft = 65*pow(voltsLeft, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
              float voltsRight = analogRead(IRpinRight)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
              float distanceRight = 65*pow(voltsRight, -1.10); 
             
              motor3stop();
             
              if (lightleft - lightright > 30)
              { 
                rot_ccw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             
              else if (lightright - lightleft > 30)
              { 
                 rot_cw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             
              else if (lightright > 300 and lightleft > 300)
              {
                stop();
                motor3forward ();
                motor3stop();
                turn_left (90, 75);
                delay(20000);
                motor3reverse ();
              }
             
              else if (distanceLeft < 35)
              {             
                  reverse(500, 75);        // reverse for 500 at 75
                  rot_cw (500,75);          // rotate clockwise for 500 at 75
              }
             
              else if (distanceRight < 35)
              { 
                 reverse (500,75);        // reverse for 500 at 75
                 rot_ccw (500,75);        // rotate countercloackwise for 500 at 75
              }
             
              else if (distanceLeft < 35 and distanceRight < 35)
              {
                reverse (500, 75);      // reverse for one second at speed 75
              }
               
              else
              {
                forward (200, 75);     // move forward for half a second (2000 ms) at speed 75 (75/255ths of full speed)
              }           
             
             
}


the only thing with this is that the 3rd motor is reversed whilst the robot is still turning, it may just be that I have to fiddle with the timings now.

AWOL

It would help us if your comments matched the code.
Code: [Select]
forward (200, 75);     // move forward for half a second (2000 ms) at speed 75 (75/255ths

200 != 2000 != half a second.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

You missed this:

Code: [Select]
                reverse (500, 75);      // reverse for one second at speed 75
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

aimes32

Oh sorry about the incorrect notes, like I said I haven't got much time to finish this and I haven't been updating it every time I make a small change.

Do you know why this piece of code...
Code: [Select]
else if (lightright > 300 and lightleft > 300)
              {
                stop();
                motor3forward ();
                motor3stop();
                turn_left (90, 75);
                delay(20000);
                motor3reverse ();
              }


works when the if statement is true, but when it is not the other part of the code isn't working. The robot seems to spin in circles, there is no part of the code that should make that happen that I can tell, but it does work when this section is removed.


wildbill

It's rather hard to say with just a fragment - if you're making changes, post the whole thing. However, based on your most recent posting, turn left sets the motors into turn mode, delays a little, then you run a 20000 millisec delay without turning the motors off. That would likely cause the symptom you see.

aimes32

My full code has been posted a few replies ago, that is why only only posted a segment to indicate which part I am talking about. My issue is that when the if statement is false the robot spins so therefore nothing in the snippet of code I quoted should happen!

If you remove it, one of these will be executed, right?

Code: [Select]
else if (distanceLeft < 35)
  {             
    reverse(500, 75);        // reverse for 500 at 75
    rot_cw (500,75);          // rotate clockwise for 500 at 75
  }

  else if (distanceRight < 35)
  { 
    reverse (500,75);        // reverse for 500 at 75
    rot_ccw (500,75);        // rotate countercloackwise for 500 at 75
  }

  else if (distanceLeft < 35 and distanceRight < 35)
  {
    reverse (500, 75);      // reverse for one second at speed 75
  }

  else
  {
    forward (200, 75);     // move forward for half a second (2000 ms) at speed 75 (75/255ths of full speed)
  }           


I note that there are no delays there, so whatever it does, it may well do continuously.



Can you explain when you think this will be done? ...

Code: [Select]
  else if (distanceLeft < 35 and distanceRight < 35)
  {
    reverse (500, 75);      // reverse for one second at speed 75
  }


We have already failed the test "if (distanceLeft < 35)", or we won't be here. We have also failed the test "if (distanceRight < 35)". So that will definitely not be true.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

aimes32

Code: [Select]
if (lightleft - lightright > 30)
              { 
                rot_ccw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             
              else if (lightright - lightleft > 30)
              { 
                 rot_cw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             
             
              else if (distanceLeft < 35)
              {             
                  reverse(500, 75);        // reverse for 500 at 75
                  rot_cw (500,75);          // rotate clockwise for 500 at 75
              }
             
              else if (distanceRight < 35)
              { 
                 reverse (500,75);        // reverse for 500ms at 75
                 rot_ccw (500,75);        // rotate countercloackwise for 500ms at 75
              }
             
              else if (distanceLeft < 35 and distanceRight < 35)
              {
                reverse (500, 75);      // reverse for 500ms at speed 75
              }
               
              else
              {
                forward (200, 75);     // move forward for 200ms (2000 ms) at speed 75 (75/255ths of full speed)
              }           
             
             
}

should be executed if it is removed.

There are no delay in the distanceright/distanceleft functions as this is an obstacle avoidance function which reads a distance to a solid object, so if either is true then the robot backs and and turns therefore the distance from the object increases and the code is no longer true.

Quote
Can you explain when you think this will be done? ...

Code: [Select]


  else if (distanceLeft < 35 and distanceRight < 35)
  {
    reverse (500, 75);      // reverse for one second at speed 75
  }


We have already failed the test "if (distanceLeft < 35)", or we won't be here. We have also failed the test "if (distanceRight < 35)". So that will definitely not be true.


You are right, I do not need this, I hadn't thought through the process that if singularily distanceleft or distanceright fails then they can never be true together.


Code: [Select]
if (lightleft - lightright > 30)
              { 
                rot_ccw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             
              else if (lightright - lightleft > 30)
              { 
                 rot_cw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             
             
              else if (distanceLeft < 35)
              {             
                  reverse(500, 75);        // reverse for 500 at 75
                  rot_cw (500,75);          // rotate clockwise for 500 at 75
              }
             
              else if (distanceRight < 35)
              { 
                 reverse (500,75);        // reverse for 500ms at 75
                 rot_ccw (500,75);        // rotate countercloackwise for 500ms at 75
              }
             
              else if (distanceLeft < 35 and distanceRight < 35)
              {
                reverse (500, 75);      // reverse for 500ms at speed 75
              }
               
              else
              {
                forward (200, 75);     // move forward for 200ms (2000 ms) at speed 75 (75/255ths of full speed)
              }           
             
             
}

should be executed if it is removed.



Actually this will be executed even if it isn't removed, right?

Code: [Select]
              if (lightleft - lightright > 30)
              { 
                rot_ccw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             
              else if (lightright - lightleft > 30)
              { 
                 rot_cw (500, 75);      // rotate clock-wise for 500 mS at speed 75
              }
             

So the problem code is after the code that you removed, not before it. We presume we are not doing those two things, otherwise it would never reach the code you removed. So the following is what is causing your problems, shouldn't be too hard to find:

Code: [Select]
else if (distanceLeft < 35)
  {             
    reverse(500, 75);        // reverse for 500 at 75
    rot_cw (500,75);          // rotate clockwise for 500 at 75
  }

  else if (distanceRight < 35)
  { 
    reverse (500,75);        // reverse for 500 at 75
    rot_ccw (500,75);        // rotate countercloackwise for 500 at 75
  }

  else
  {
    forward (200, 75);     // move forward for half a second (2000 ms) at speed 75 (75/255ths of full speed)
  }           
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up