Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1] 2 3   Go Down
Author Topic: Code sequencing issue  (Read 2956 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using an elseif code to run a motor on a robot, however I am having problems with a small section of my code.

The problem is that when the light is above a certain value (600) I want to run a 3rd motor forward,  then move the robot and finally reverse the 3rd motor. The code I have looks ike this:

 else if (lightright and lightleft > 600)
              {
                stop(5, 0);                             // stop the robot
                motor3forward (100);            // 3rd motor forward for 100ms
                motor3stop();                       // stop the 3rd motor
                turn_left (100, 75);               // turn the robot left
                motor3reverse (100);           // 3rd motor revered for 100ms
                motor3stop();                     // stop the 3rd motor
                delay (5000);
              }

but what happens when I run the robot is the 3rd motor will run forward and immediately reverse then the robot will turn left, I thought it should run through all the steps one after another but it seems to run all at the same time.

I have also tried putting a delay between all the action but this just makes each section run for the time it is set to PLUS the time of the delay!

I have a final year university crit on Wednesday and need this finished! Thanks in advance
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 319
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
else if (lightright > 600 and lightleft > 600
?
Logged

"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.

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 else if (lightright and lightleft > 600)


This compiles?  It should be:

 else if (lightright && lightleft > 600)


I suspect your code is not compiling and your uploading an earlier version.


Quote

              else if (lightright and lightleft > 600)
              {
                stop(5, 0);                             // stop the robot
                motor3forward (100);            // 3rd motor forward for 100ms
                motor3stop();                       // stop the 3rd motor
                turn_left (100, 75);               // turn the robot left
                motor3reverse (100);           // 3rd motor revered for 100ms
                motor3stop();                     // stop the 3rd motor
                delay (5000);
              }


If motor3forward(100) is only supposed to run the motor for 100 ms, why do you need to follow it with a motor3stop() ???

If correcting the issue already pointed out does not help, we'll need to see the code for motor3forward().

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 319
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This compiles?  It should be:

Yes, why not?
It doesn't do what the OP wants, but it compiles.
It tests to see if lightright is non-zero and that lightleft is greater than 600, not that both are greater than 600.
« Last Edit: May 09, 2011, 05:04:41 am by AWOL » Logged

"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.

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


 else if (lightright and lightleft > 600)


This compiles?  It should be:

 else if (lightright && lightleft > 600)


I suspect your code is not compiling and your uploading an earlier version.

Check out:

http://arduino.cc/forum/index.php?topic=60082.0
Logged

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 319
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have now updated it to  "else if (lightright && lightleft > 600)".
"and" is perfectly acceptable, however see my comments about what this expression does, and whether or not that is what you want?
Logged

"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.

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

Thans for the 'and/&&' tip off, I have now changed this. I have also taken out the motor3stop() after each action as I only added this in hope of it forcing the code to run in the order I want but I doesn't make a difference. My code now reads:

 else if (lightright && lightleft > 600)
              {
                stop(5, 0);
                motor3forward (100);
                turn_left (100, 75);
                motor3reverse (100);
                delay (500);
              }

I think what is happening is that motor3forward runs at the same time as turn_left, then motor3reverse runs for 100 plus the delay 500, no delay is hapening between the actions.

My setup looks like:

void motor3reverse(int dist) {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, LOW);
    delay (dist);
    }
    
void motor3forward(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
      delay (dist);
    }
    
void motor3stop() {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
    }
      

Logged

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you agree that :

if (lightright && lightleft > 600)

means 'if (lightright!=0)  AND (lightleft > 600))' ....

and does not means : if (lightright>600) AND (lightleft>600))



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 319
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Agree?
I've been saying it since reply #1.
Logged

"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.

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want your forward and stop function to run only for the specified delay and then stop, then the functions themselves should issue a stop after the delay, something like:

Code:
void motor3forward(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
      delay (dist);
      motor3stop();
    }


without the stop after the delay, the motor is going to keep on doing what it was last told to do.

I would be interested in seeing your stop and turn functions as well, and why you are passing two params into each.

And finally, please post your code in code tags, as I've done above.  Makes it easier to read.
Logged


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

I've changed that now so motor3stop is included in the setup but still no change in the outcome. My stop and turn functions look like:

Code:
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 stop(int dist, int vel) {                              // stop: force both motor speeds to 0 (low)
              digitalWrite (speedA, LOW);
              digitalWrite (speedB, LOW);
             }

I am not sure what you mean by passing two params into each, I've not been using arduino long and have only grasped the basics.

Apologies for the lack of structure to my posts too, I also haven't used forums before!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 319
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you please post all you code as it is now?
Logged

"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.

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

Code:
// 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(int dist, int vel) {                              // stop: force both motor speeds to 0 (low)
              digitalWrite (speedA, LOW);
              digitalWrite (speedB, LOW);
             }           
             
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 motor3reverse(int dist) {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, LOW);
    delay (dist);
    motor3stop();
    }
   
void motor3forward(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
      delay (dist);
      motor3stop();
    }
   
void motor3stop() {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
    }
             
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 && lightleft > 600)
              {
                stop(5, 0);
                motor3forward (100);
                delay(200);
                turn_left (300, 75);
                delay(200);
                motor3reverse (100);
                delay (500);
              }
             
              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)
              }           
             
             
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 319
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm going to ask again.
Is this:
Code:
              else if (lightright && lightleft > 600)
really what you want to do?
Logged

"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.

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

I think so, I want it to be when both reading are larger than 600 then run the program. However this is not the problem I really need solving!
Logged

Pages: [1] 2 3   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: