omie10
20
#define duty_cycle 25
void setup() {
// Set pins 5 and 6 as output
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// Configure Timer0 for Fast PWM mode
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11) | (1 << WGM10);
TCCR1B = (1 << CS11); // No prescaler
// Set TOP value for desired frequency (30 kHz)
// Formula: TOP = (F_CPU / (N * desired frequency)) - 1
// For Arduino Nano (16 MHz), N = 1
OCR1A = 511; // TOP value for pin 5 (16 MHz / (8 * 30 kHz) - 1)
OCR1B = 511; // TOP value for pin 6 (16 MHz / (8 * 30 kHz) - 1)
}
void loop() {
// Adjust duty cycle if needed
// Example: 50% duty cycle for both pins
analogWrite(9, (duty_cycle * 128) / 100);
analogWrite(10, (duty_cycle * 128) / 100);
}