#include <Servo.h>
#include <TimerOne.h> // Include TimerOne library
#include <TimerThree.h> // Include TimerThree library
// Create Servo objects
Servo servo1; // Servo for Timer 1
Servo servo2; // Servo for Timer 3
void setup() {
// Attach the servos to PWM-capable pins (use pins 9 and 10 on Arduino Mega)
servo1.attach(9); // Pin 9, using Timer1
servo2.attach(10); // Pin 10, using Timer3
// Set up Timer1 for independent PWM signal (50Hz frequency)
Timer1.initialize(20000); // 20ms period for 50Hz (PWM frequency)
Timer1.pwm(9, 1280); // Set the initial pulse width (1.5ms) for Servo 1 (at 90 degrees)
// Set up Timer3 for independent PWM signal (50Hz frequency)
Timer3.initialize(20000); // 20ms period for 50Hz (PWM frequency)
Timer3.pwm(10, 1280); // Set the initial pulse width (1.5ms) for Servo 2 (at 90 degrees)
}
void loop() {
// Adjust servo positions by changing the pulse width (1ms to 2ms range)
// For Servo 1 (Pin 9 - controlled by Timer1)
Timer1.pwm(9, 1024); // 1ms pulse width, move to 0 degrees
delay(1000); // Wait for 1 second
Timer1.pwm(9, 1536); // 1.5ms pulse width, move to 90 degrees
delay(1000); // Wait for 1 second
Timer1.pwm(9, 2048); // 2ms pulse width, move to 180 degrees
delay(1000); // Wait for 1 second
// For Servo 2 (Pin 10 - controlled by Timer3)
Timer3.pwm(10, 1024); // 1ms pulse width, move to 0 degrees
delay(1000); // Wait for 1 second
Timer3.pwm(10, 1536); // 1.5ms pulse width, move to 90 degrees
delay(1000); // Wait for 1 second
Timer3.pwm(10, 2048); // 2ms pulse width, move to 180 degrees
delay(1000); // Wait for 1 second
}