Thread: How would I gradually decrement and increment?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28

    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 12:13 PM.

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, 09: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