Shop OBEX P1 Docs P2 Docs Learn Events
Servos and motors bug — Parallax Forums

Servos and motors bug

Wrote this code for a small camera rover with servos and motors, using pin 8 to constantly send PWM into the motor controller and when moving on to a different loop for servos only on different pins they wouldn't work until turning off pin 8, why is that?







Comments

  • Here's the code, when it breaks out of the second loop pin 8 sends a 0 PWM value, without this the servos didn't work no matter what I did...

    /*2 Axis Cam Rover by electron spectrum*/
    #include "simpletools.h" // Include simple tools
    #include "servo.h"
    int main() // Main function
    {
    int x = 900;
    int y = 900;
    int t = 13;
    while(1)
    {
    high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
    high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
    while(1)
    {
    high(0);
    high(2);
    high(4);
    high(6);
    servo_angle(8, 7500);
    if ((input(15) == 1) && (input(12) == 1)) break;
    if ((input(14) == 1) && (input(12) == 0)) low(6);
    if ((input(14) == 0) && (input(12) == 1)) low(4);
    if (input(13) == 1) low(2);
    if (input(15) == 1) low(0);
    pause(20);
    }
    high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
    high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
    while(1)
    {
    high(0);
    high(2);
    high(4);
    high(6);
    servo_angle(8, 20000);
    if ((input(15) == 1) && (input(12) == 1)) break;
    if ((input(14) == 1) && (input(12) == 0)) low(6);
    if ((input(14) == 0) && (input(12) == 1)) low(4);
    if (input(13) == 1) low(2);
    if (input(15) == 1) low(0);
    pause(20);
    }
    servo_angle(8, 0);
    high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
    high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
    while(1)
    {
    if ((input(15) == 1) && (input(12) == 1)) break;
    if (input(14) == 1) x = x + 3; servo_angle(24, x);
    if (input(12) == 1) x = x - 3; servo_angle(24, x);
    if (input(13) == 1) y = y + 3; servo_angle(5, y);
    if (input(15) == 1) y = y - 3; servo_angle(5, y);
    if (x > 1799) x = 1799;
    if (x < 1) x = 1;
    if (y > 1799) y = 1799;
    if (y < 1) y = 1;
    pause(1);
    }
    }
    }
  • Here's a video:
  • Try re-pasting that in between code tags, so it keeps formatting. It's very hard to read the way it's posted.

    Code tags look like this:

    code

    /code

    With both inside square brackets.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2016-01-01 19:51
    hi Franny. Can you please try posting your code again and wrap it in the [ code] and [ /code] tags? (remove the spaces inside the brackets)

    Also, are you familiar a "for-loop"? It's a way to repeat a certain snippet of code a specific number of times. You can also write multiple functions to run similar pieces of code in different places. For instance, you could have some code like so:
    int main () {
        high(16);
        pause(t);
        low(16);
        pause(t);
        high(17);
        pause(t);
        low(17);
        pause(t);
        high(18);
        pause(t);
        low(18);
        pause(t);
        high(19);
        pause(t);
        low(19);
        pause(t);
    }
    

    and it could be re-written as such:
    void blink (const int pinNumber, const int delay) {
        high(pinNumber);
        pause(delay);
        low(pinNumber);
        pause(delay);
    }
    
    int main () {
        for (int pin = 16; pin < 20; ++pin) {
            blink(pin);
        }
    }
    
  • OK, I hope it's more readable now, basically if I remove the line " servo_angle(8, 0);" (it's line # 25) then one of the servos doesn't want to work, why is it that I have to turn off certain pin's PWM in order to use the other one?

    #include "simpletools.h" // Include simple tools
    #include "servo.h"
    int main() // Main function
    {
    int x = 900;
    int y = 900;
    int t = 13;
    while(1)
    {
    while(1)
    {
    high(0);
    high(2);
    high(4);
    high(6);
    servo_angle(8, 20000);
    if ((input(15) == 1) && (input(12) == 1)) break;
    if ((input(14) == 1) && (input(12) == 0)) low(6);
    if ((input(14) == 0) && (input(12) == 1)) low(4);
    if (input(13) == 1) low(2);
    if (input(15) == 1) low(0);
    pause(20);
    }
    servo_angle(8, 0);
    pause(1000);
    while(1)
    {
    if ((input(15) == 1) && (input(12) == 1)) break;
    if (input(14) == 1) x = x + 3; servo_angle(24, x);
    if (input(12) == 1) x = x - 3; servo_angle(24, x);
    if (input(13) == 1) y = y + 3; servo_angle(5, y);
    if (input(15) == 1) y = y - 3; servo_angle(5, y);
    if (x > 1799) x = 1799;
    if (x < 1) x = 1;
    if (y > 1799) y = 1799;
    if (y < 1) y = 1;
    pause(1);
    }
    }
    }

  • Select your code and click on the C
    872 x 486 - 17K
  • lardom wrote: »
    Select your code and click on the C

    What for?
  • Here's the code with indentation.
    #include "simpletools.h"                      // Include simple tools
    #include "servo.h"
    int main()                                    // Main function
    {
      int x = 900;
      int y = 900;
      int t = 13;
      while(1)
      {
      while(1)
      {
        high(0);
        high(2);
        high(4);
        high(6);
        servo_angle(8, 20000);             
        if ((input(15) == 1) && (input(12) == 1)) break;
        if ((input(14) == 1) && (input(12) == 0)) low(6);
        if ((input(14) == 0) && (input(12) == 1)) low(4);
        if (input(13) == 1) low(2);
        if (input(15) == 1) low(0); 
        pause(20);                  
      }
      servo_angle(8, 0);
      pause(1000);  
      while(1)
      {
        if ((input(15) == 1) && (input(12) == 1)) break;
        if (input(14) == 1) x = x + 3; servo_angle(24, x);
        if (input(12) == 1) x = x - 3; servo_angle(24, x);
        if (input(13) == 1) y = y + 3; servo_angle(5, y);
        if (input(15) == 1) y = y - 3; servo_angle(5, y);
        if (x > 1799) x = 1799;
        if (x < 1) x = 1;
        if (y > 1799) y = 1799;
        if (y < 1) y = 1;
        pause(1);
      }
     }  
    }
    

  • You have two inner while(1) loops within another while(1). The only way to break out of the first inner while(1) loop is for input(15) and input(12) to be high. However, that will also immediately break out of the second inner loop as well. You should use a different condition to break out of the two loops. Also, the servo on pin 8 is being driven to an angle of 2000 degrees. Maybe you intended the parameter to be 2000 instead of 20000 so that the angle is 200.
  • Franny wrote: »
    lardom wrote: »
    Select your code and click on the C

    What for?
    Do you see how everybody else's code has a 'yellow' background? I don't read C but here's a small sample of your code after I clicked "C".
    #include "simpletools.h" // Include simple tools
    #include "servo.h"
    int main() // Main function
    {
    int x = 900;
    int y = 900;
    int t = 13;
    while(1)
    {
    while(1)
    {
    high(0);
    high(2);
    high(4);
    high(6);
    
  • Dave Hein wrote: »
    You have two inner while(1) loops within another while(1). The only way to break out of the first inner while(1) loop is for input(15) and input(12) to be high. However, that will also immediately break out of the second inner loop as well. You should use a different condition to break out of the two loops. Also, the servo on pin 8 is being driven to an angle of 2000 degrees. Maybe you intended the parameter to be 2000 instead of 20000 so that the angle is 200.

    There's a pause(1000); so it won't break out of the second loop until you hit those to buttons again, that part is fine, the 20000 is for a motor controller so it's fine too, the only question is why do I have to turn off pin 8 so that the next loop works?

  • lardom wrote: »
    Franny wrote: »
    lardom wrote: »
    Select your code and click on the C

    What for?
    Do you see how everybody else's code has a 'yellow' background? I don't read C but here's a small sample of your code after I clicked "C".
    #include "simpletools.h" // Include simple tools
    #include "servo.h"
    int main() // Main function
    {
    int x = 900;
    int y = 900;
    int t = 13;
    while(1)
    {
    while(1)
    {
    high(0);
    high(2);
    high(4);
    high(6);
    

    Gotcha thanks!
  • What do you mean by "turn off pin 8"? How do you do that? Are you doing this by calling servo_angle(8, 0)? Maybe the device connected to pin 8 is interacting with the other servos. If they are sharing the same power line it might be pulling it down to low for the other servos to work. What happens if you don't turn off pin 8, but just disconnect the device connected to pin 8?
  • Great idea! I already tried different pins (as you can kinda tell) I think that cheap motor controller (L298N) has to have something to do with this. Thanks
Sign In or Register to comment.