daily pastebin goal
46%
SHARE
TWEET

Four servos example

a guest Apr 30th, 2012 288 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Servo8Bit.h"
  2.  
  3. void delay(uint16_t milliseconds); //forward declaration to the delay function
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12.     Servo8Bit myServoA; //create a servo object.
  13.                         //a maximum of five servo objects can be created
  14.     Servo8Bit myServoB;
  15.     Servo8Bit myServoC;
  16.     Servo8Bit myServoD;
  17.  
  18.  
  19.     myServoA.attach(1); //attach the servo to pin PB1
  20.     myServoB.attach(2); //attach the servo to pin PB2
  21.     myServoC.attach(3); //attach the servo to pin PB3
  22.     myServoD.attach(4); //attach the servo to pin PB4
  23.  
  24.  
  25.     myServoA.write(0); //rotate to the 0 degree position
  26.     myServoB.write(0); //rotate to the 0 degree position
  27.     myServoC.write(0); //rotate to the 0 degree position
  28.     myServoD.write(0); //rotate to the 0 degree position
  29.     delay(2000); //wait 2 seconds
  30.  
  31.     myServoA.write(180); //rotate to the 180 degree position
  32.     myServoB.write(180); //rotate to the 180 degree position
  33.     myServoC.write(180); //rotate to the 180 degree position
  34.     myServoD.write(180); //rotate to the 180 degree position
  35.     delay(2000); //wait 2 seconds
  36.  
  37.     myServoA.write(90); //rotate to the center (90 degree) position
  38.     myServoB.write(90); //rotate to the center (90 degree) position
  39.     myServoC.write(90); //rotate to the center (90 degree) position
  40.     myServoD.write(90); //rotate to the center (90 degree) position
  41.     delay(2000); //wait 2 secondS
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. void delayMicroseconds(uint16_t us)
  54. {
  55. #if F_CPU >= 16000000L
  56.     // for the 16 MHz clock on most Arduino boards
  57.  
  58.     // for a one-microsecond delay, simply return. the overhead
  59.     // of the function call yields a delay of approximately 1 1/8 us.
  60.     if (--us == 0)
  61.         return;
  62.  
  63.     // the following loop takes a quarter of a microsecond (4 cycles)
  64.     // per iteration, so execute it four times for each microsecond of
  65.     // delay requested.
  66.     us <<= 2;
  67.  
  68.     // account for the time taken in the preceeding commands.
  69.     us -= 2;
  70. #else
  71.     // for the 8 MHz internal clock on the ATmega168
  72.  
  73.     // for a one- or two-microsecond delay, simply return. the overhead of
  74.     // the function calls takes more than two microseconds. can't just
  75.     // subtract two, since us is unsigned; we'd overflow.
  76.     if (--us == 0)
  77.         return;
  78.     if (--us == 0)
  79.         return;
  80.  
  81.     // the following loop takes half of a microsecond (4 cycles)
  82.     // per iteration, so execute it twice for each microsecond of
  83.     // delay requested.
  84.     us <<= 1;
  85.  
  86.     // partially compensate for the time taken by the preceeding commands.
  87.     // we can't subtract any more than this or we'd overflow w/ small delays.
  88.     us--;
  89. #endif
  90.  
  91.     // busy wait
  92.     __asm__ __volatile__ (
  93.         "1: sbiw %0,1" "\n\t" // 2 cycles
  94.         "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
  95.     );
  96. }//end delayMicroseconds
  97.  
  98.  
  99.  
  100. void delay(uint16_t milliseconds)
  101. {
  102.     for(uint16_t i = 0; i < milliseconds; i++)
  103.     {
  104.         delayMicroseconds(1000);
  105.     }
  106. }//end delay
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top