Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Servo8Bit.h"
- void delay(uint16_t milliseconds); //forward declaration to the delay function
- int main()
- {
- Servo8Bit myServoA; //create a servo object.
- //a maximum of five servo objects can be created
- Servo8Bit myServoB;
- Servo8Bit myServoC;
- Servo8Bit myServoD;
- myServoA.attach(1); //attach the servo to pin PB1
- myServoB.attach(2); //attach the servo to pin PB2
- myServoC.attach(3); //attach the servo to pin PB3
- myServoD.attach(4); //attach the servo to pin PB4
- myServoA.write(0); //rotate to the 0 degree position
- myServoB.write(0); //rotate to the 0 degree position
- myServoC.write(0); //rotate to the 0 degree position
- myServoD.write(0); //rotate to the 0 degree position
- delay(2000); //wait 2 seconds
- myServoA.write(180); //rotate to the 180 degree position
- myServoB.write(180); //rotate to the 180 degree position
- myServoC.write(180); //rotate to the 180 degree position
- myServoD.write(180); //rotate to the 180 degree position
- delay(2000); //wait 2 seconds
- myServoA.write(90); //rotate to the center (90 degree) position
- myServoB.write(90); //rotate to the center (90 degree) position
- myServoC.write(90); //rotate to the center (90 degree) position
- myServoD.write(90); //rotate to the center (90 degree) position
- delay(2000); //wait 2 secondS
- }
- void delayMicroseconds(uint16_t us)
- {
- #if F_CPU >= 16000000L
- // for the 16 MHz clock on most Arduino boards
- // for a one-microsecond delay, simply return. the overhead
- // of the function call yields a delay of approximately 1 1/8 us.
- if (--us == 0)
- return;
- // the following loop takes a quarter of a microsecond (4 cycles)
- // per iteration, so execute it four times for each microsecond of
- // delay requested.
- us <<= 2;
- // account for the time taken in the preceeding commands.
- us -= 2;
- #else
- // for the 8 MHz internal clock on the ATmega168
- // for a one- or two-microsecond delay, simply return. the overhead of
- // the function calls takes more than two microseconds. can't just
- // subtract two, since us is unsigned; we'd overflow.
- if (--us == 0)
- return;
- if (--us == 0)
- return;
- // the following loop takes half of a microsecond (4 cycles)
- // per iteration, so execute it twice for each microsecond of
- // delay requested.
- us <<= 1;
- // partially compensate for the time taken by the preceeding commands.
- // we can't subtract any more than this or we'd overflow w/ small delays.
- us--;
- #endif
- // busy wait
- __asm__ __volatile__ (
- "1: sbiw %0,1" "\n\t" // 2 cycles
- "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
- );
- }//end delayMicroseconds
- void delay(uint16_t milliseconds)
- {
- for(uint16_t i = 0; i < milliseconds; i++)
- {
- delayMicroseconds(1000);
- }
- }//end delay
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement