Pages: 1 [2] 3   Go Down
Author Topic: Code sequencing issue  (Read 2628 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want it to be when both reading are larger than 600 then run the program
Have you read replies 1, 7 and 8?

Definitions like these:
Code:
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;   
should really be qualified with "const".
« Last Edit: May 09, 2011, 08:38:28 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.

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

I believe your problem isn't that your functions are running out of order (well, I knew that wasn't the problem from the start), but your functions are not doing what you think they are doing.

In other words, motor3forward() and motor3backwards aren't necessarily going to drive your vehicle forwards and backwards.  Why?  because you aren't explicitly setting your motor direction pins in those functions, so how do you know what direction the motors are set to run in?

I'm not sure your turn routines are driving the motors correctly either, but would need to know what "robot shield" you are using and how your motors are wired up to give advice.

What I would recommend is starting with a simple sketch that allows you to test each drive routine individually.  Verify that your forward routine actually drives your robot forward (and then stops when it is complete), regardless the state of the IO points prior to calling the routine.  Then do the same for each routine.  I think you'll find all of them have some undesirable behavior on their own.
Logged


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

I have built up this sketch over time checking each stage, the driving of the robot was my first step and works as I believe it should ( the sketch was taken directly from what I was given along with the motor shield I bought, which is a Robot Bits Robot Sheild). I then added the sensors to determine which directon the robot moves, this again works as I want it to. The final stage that I have added is a 3rd motor using a L239D driver, not to drive the robot but to move a pen up and down to draw with.

Awol, I have read 1, 7 and 8, and on reflection   else if (lightright > 600 and lightleft > 600) is what I am trying to do, but I have tried both that and else if (lightright && lightleft>600) and it makes no difference to how my third motor works.
Logged

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

Quote
I have tried both that and else if (lightright && lightleft>600)
Well, just don't do the latter.
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

Wow!

http://www.cppreference.com/wiki/keywords/and

Okay, I guess I learned something new (to me) about C++!
Logged

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

Awol, I have read 1, 7 and 8, and on reflection   else if (lightright > 600 and lightleft > 600) is what I am trying to do, but I have tried both that and else if (lightright && lightleft>600) and it makes no difference to how my third motor works.

Nor will it make a difference in how the motor works.  It will make a difference in when it works though, and it's likely the former method that you want for that.

As for how it works, if we break down the lines of that else if() block, we get the following:

stop(5, 0);                   - The drive motors will stop doing whatever they are currently set to do
motor3forward (100);    - Motor3 will move forward for 100ms, then stop
delay(200);                   - Nothing will happen for 200 ms
turn_left (300, 75);       - The drive motors will turn left for 300ms.  turn_left() has no code in it to stop though.
delay(200);                   - The drive motors will continue to turn left for another 200 msec, as no code has told them to stop
motor3reverse (100);     - Motor3 will move backward for 100ms, and the drive motors are still turning left.  Motor3 then stops
delay (500);                  - And still, the drive motors continue turning left.

If that is not the behavior you are seeing, then please describe in detail the behavior that you are seeing.

As it appears this is not the behavior you want to see, please describe in detail the behavior you want to see.
Logged


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

The behaviour I am seeing is the motor run forward and instantly reversing whilst at the same time he robot turning left.

I have been playing the the code just for the 3rd motor, which might help. The code I have is:
Code:
int motorPin1 = 3;          // pin 2 on L293D
int motorPin2 = 5;          // pin 7 on L293D
int enablePin = 1;          // pin 1 on L293D

int dist;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);
}
 
void motor3reverse(int dist) {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, LOW);
    }
   
void motor3forward(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
    }
   
void motor3stop(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
    }

I have been altering the loop to see what happens:

Code:
motor3forward(100);
runs the motor forward continually (I expected to see this).

Code:
motor3reverse(100);
runs the motor backwards, again what I want, the issues start when I add more tasks:

Code:
motor3forward(100);
         motor3stop();
nothing happens

Code:
motor3froward(100);
         delay(100);
the motor run forward (I believe for the set time plus the delay time but obviously it is a continuous action)


Logged

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

Code:
motor3forward(100);
         motor3stop();
nothing happens

You have removed the delays from all your functions, so you are stopping the motors immediately after setting them.  Passing the 100 into your function doesn't do anything.
Logged


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

so I definately need the delay in the setup as so:
Code:
void motor3reverse(int dist) {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, LOW);
    delay(1000);
    }
   
void motor3forward(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
      delay(1000);
    }

now with the loop
Code:
void loop(){
motor3reverse(1000);
motor3stop();
the motor run continually in reverse. Is this because I need to tell it how long to stop the motors for?
Logged

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

Quote
Is this because I need to tell it how long to stop the motors for?
Yes, it is because your code is in a loop - the motor runs for a second, stops for about 5 microseconds then starts running again.
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

ok, with the code:
Code:
int motorPin1 = 3;          // pin 2 on L293D
int motorPin2 = 5;          // pin 7 on L293D
int enablePin = 1;          // pin 1 on L293D

int dist;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);
}
 
void motor3reverse(int dist) {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, LOW);
    delay(1000);
    }
   
void motor3forward(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
      delay(1000);
    }
   
void motor3stop(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
      delay(1000);
    }
   
void loop(){
  motor3forward(1000);
  motor3stop(1000);
  motor3reverse(1000);
  motor3stop(1000);

}
the motor runs forward, pauses, runs backwards and pauses, perfect. However, when I copy and paste the same void setup into my robot's code I get the error "too few arguements to function 'void motor3stop(int)'. How come it can work in one sketch and not another?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

However, when I copy and paste the same void setup into my robot's code ...

What do you mean by that? You copied and pasted the setup function? Only? Try to be specific. They are called functions. What did you copy? What did you replace? What did you leave there? The answer will be there.
Logged

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

I took
Code:
void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);
}
 
void motor3reverse(int dist) {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, LOW);
    delay(1000);
    }
   
void motor3forward(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, HIGH);
      delay(1000);
    }
   
void motor3stop(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
      delay(1000);
    }

and put that into my robot's code to give:
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 motor3stop(int dist) {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
      delay(1000);
    }

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

             
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 > 600 and lightleft > 600)
              {
                stop(5, 0);
                motor3forward (100);
                motor3stop(300);
                turn_left (300, 75);
                motor3reverse (100);
              }
             
              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
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have this:

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

And in your loop function (where the error message said to look) you have:

Code:
              motor3stop();

Code:
sketch_may10c.cpp: In function 'void loop()':
sketch_may10c:97: error: too few arguments to function 'void motor3stop(int)'
sketch_may10c:128: error: at this point in file

Do you see the problem?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But as AWOL said a while back, why have the argument "dist" when you don't use it? That's just confusing you as you now see. Why not:

Code:
void motor3stop() {
      digitalWrite (motorPin1, LOW);
      digitalWrite (motorPin2, LOW);
      delay(1000);
    }

But then of course this will fail:

Code:
                motor3stop(300);

Programming is all about thinking logically. These error message point you to the problem, you should be able to investigate and see if when you call a function you are passing the correct arguments to it.
Logged

Pages: 1 [2] 3   Go Up
Jump to: