How would I gradually decrement and increment?

This is a discussion on How would I gradually decrement and increment? within the C Programming forums, part of the General Programming Boards category; I have this code that I'm using to control a Servo and I'd like to modify it so that it ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    24

    How would I gradually decrement and increment?

    I have this code that I'm using to control a Servo and I'd like to modify it so that it gradually decreases the degrees of sweep by a set amount (say 1 degree per pass). Right now it sweeps back and forth from ZERO to 180. I can manually change that by changing the numbers but I'm guessing there's an easier way to do this. Any ideas?

    I'd like the sweep range range to decrease (i.e. say zero to 180, 1 to 179, 2 to 178, etc then eventually turning around and increasing to full width passes again). Thanks in advance for ideas!

    Code:
    // Sweep
    // by BARRAGAN <http://barraganstudio.com>
    // This example code is in the public domain.
    
    
    #include <Servo.h>
     
    Servo myservo;  // create servo object to control a servo
                    // a maximum of eight servo objects can be created
     
    int pos = 0;    // variable to store the servo position
     
    void setup()
    {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    }
     
     
    void loop()
    {
      for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
      {                                  // in steps of 1 degree
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
      for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
      {                                
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
    }
    Last edited by 777funk; 11-08-2010 at 11:13 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First off... is this a DC servo or a stepper motor?

    Sounds like you nee some serious for() loops....

    Code:
    for (int x = 0; x < 90; x++)
      for (int y = 0; y < 90; y++)
        { myservo.write(x - y);
           delay (15)
           myservo.write(x+y); }
    Last edited by CommonTater; 11-08-2010 at 11:17 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    First off... is this a DC servo or a stepper motor?

    Sounds like you nee some serious for() loops....

    Code:
    for (int x = 0; x < 90; x++)
      for (int y = 0; y < 90; y++)
        { myservo.write(x - y);
           delay (15)
           myservo.write(x+y); }
    Thanks for the input and cool username lol

    As far as the motor it is a DC Servo. I have it working with the above code that I'd posted (taken from a library for this microcontroller) but would like to modify the code to fit my application.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually I just realized, my code sample has a bug in it but I'm betting you'll find it....

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    Actually I just realized, my code sample has a bug in it but I'm betting you'll find it....

    Don't bet your life on it! I'm a beginner when it comes to programming

    but is it a set of brackets missing for the first "for" loop to encapsulate the second?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 777funk View Post
    Don't bet your life on it! I'm a beginner when it comes to programming

    but is it a set of brackets missing for the first "for" loop to encapsulate the second?
    Nope... that's legal if you are only executing one statement, in this case the inner loop.

    First, you can do it with a single loop... but there's one other thing...

    Code:
    for ( int x = 90; x > 0; x--)
       { SetServo(90 - x);
          sleep(30);
          SetServo(90 + x);
          sleep(30); }
    Get it now?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    I think that makes sense to me. I'll try it. What if I want to offset it by say + or - 10 with a maximum still capping at 180?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    I just tried the following and it did some weird things. Definitely not what I was thinking it'd do (work from the outside bounds (67 and 107 in my case) and gradually get smaller as the numbers head toward the midpoint of 87 degrees). Here's my source:

    Code:
    #include <Stepper.h>
    
    #include <Servo.h>
     
    Servo myservo;  // create servo object to control a servo
                    
    int x = 0;    // variable to store the servo position
     
    void setup()
    {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    }
     
    void loop()
    {                
     
      for(int x = 20; x > 0; x--)  
      {                                  
        myservo.write(87 - x);             
        delay(9);                        
        myservo.write(87 + x);          
        delay(9);                 
        
      }
      
    }
    Last edited by 777funk; 11-08-2010 at 08:23 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Ok I changed it to x -= 1 and it's actually more within the range but it's not doing what I had hoped (going the full range and decrementing a degree per side i.e. 67 to 107, 68 to 106, 69 to 105 and so on).

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Add some diagnostic prints in there so you can see the values of x when it moves...

    Also I see you are working in what looks like C++ ... perhaps you would get better help in the C++ forum...

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    It's arduino C/C++. A very strange hybrid.

    Since you have the arduino library, you can just use delay().
    Arduino - Delay

    EDIT: oh nevermind. I see you figured that out.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    I finally got it to do what I've been trying to get it to do. But it seems to not be sweeping the entire degree range (around 40 degrees from 67 to 107).

    Code:
    void loop()
    {                
    for (a = 0; a < 15; a++)
    {
     for(pos = 87 + a; pos < 107 - a; pos += 1)  
      {                                  
        myservo.write(pos);             
        delay(15);                       
      } 
      
      for(pos = 107 - a; pos>=88 + a; pos-=1)  
      {                                
        myservo.write(pos);          
        delay(15);                      
      }
    }  
    for (a = 15; a >= 0; a--)
    {
     for(pos = 87 + a; pos < 107 - a; pos += 1)  
      {                                  
        myservo.write(pos);             
        delay(15);                       
      } 
      
      for(pos = 107 - a; pos>=88 + a; pos-=1)  
      {                                
        myservo.write(pos);          
        delay(15);                      
      }
    } 
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... those loops are looking ominously complex and might well get you into unpredictable behavior...

    If you gave it the sequence of:
    Code:
    myservo.write(106);  
    delay(15);
    myservo.write(31);  
    delay(15);
    myservo.write(87);  
    delay(15);
    myservo.write(159);
    exactly what happens?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 777funk View Post
    I think that makes sense to me. I'll try it. What if I want to offset it by say + or - 10 with a maximum still capping at 180?
    In that case make your center position a variable.

    To keep it in bounds use some pre-move testing...
    Code:
    pos = (pos > 180) ? 180 : pos;
    pos = (pos < 0) ? 0 : pos;
    
    or if your language doesn't support that...
    
    if (pos > 180) pos = 180;
    if (pos < 0) pos = 0;

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    Ok... those loops are looking ominously complex and might well get you into unpredictable behavior...

    If you gave it the sequence of:
    Code:
    myservo.write(106);  
    delay(15);
    myservo.write(31);  
    delay(15);
    myservo.write(87);  
    delay(15);
    myservo.write(159);
    exactly what happens?
    Well shoot! Looks like my servo burned itself out so I'll have to continue the research later this week (got more ordered).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment and Decrement
    By Johnathan1707 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 02:20 PM
  2. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 08:24 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  4. Replies: 11
    Last Post: 08-30-2004, 03:56 PM
  5. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21