#include <TimerOne.h>
#define PWM_PIN 9 // Pin 9 for PWM output
void setup() {
// Initialize serial communication at 9600 baud rate
Serial.begin(9600);
// Initialize Timer1
Timer1.initialize(16); // Set the period in microseconds for 62,500 Hz frequency (1 / 62500 = 16 microseconds)
// Start the PWM on pin 9 with a 50% duty cycle
Timer1.pwm(PWM_PIN, 512); // 1024 is full duty cycle in TimerOne, so 512 is 50%
Serial.println("PWM setup complete. Send duty cycle percentage (0-100).");
}
void loop() {
if (Serial.available()) {
int dutyPercent = Serial.parseInt();
if (dutyPercent >= 0 && dutyPercent <= 100) {
// Map the percentage to TimerOne's range
int duty = map(dutyPercent, 0, 100, 0, 1023);
Timer1.setPwmDuty(PWM_PIN, duty);
Serial.print("Duty cycle set to: ");
Serial.print(dutyPercent);
Serial.println("%");
} else {
Serial.println("Error: Duty cycle must be between 0% and 100%");
}
}
}