#define NUM_PINS_1_TO_6 6
#define NUM_PINS_7_TO_10 4
const int pins_1_to_6[NUM_PINS_1_TO_6] = {4, 0, 2, 15, 13, 12};
const int pins_7_to_10[NUM_PINS_7_TO_10] = {14, 27, 26, 25};
const int frequency_1_to_6 = 285714; // Hz
const int frequency_7_to_10 = 1000000; // Hz
volatile int current_pin_index_1_to_6 = 0;
volatile int current_pin_index_7_to_10 = 0;
void setup() {
for (int i = 0; i < NUM_PINS_1_TO_6; i++) {
pinMode(pins_1_to_6[i], OUTPUT);
}
for (int i = 0; i < NUM_PINS_7_TO_10; i++) {
pinMode(pins_7_to_10[i], OUTPUT);
}
// Initialize Timer1 for pins 1 to 6
cli(); // Disable interrupts
TCCR1A = 0; // Clear Timer1 control registers
TCCR1B = 0;
TCNT1 = 0; // Initialize counter value to 0
OCR1A = 16000000 / frequency_1_to_6 - 1; // Set the compare match register
TCCR1B |= (1 << WGM12); // Configure Timer1 for CTC mode
TCCR1B |= (1 << CS10); // Set prescaler to 1
TIMSK1 |= (1 << OCIE1A); // Enable Timer1 compare match interrupt
sei(); // Enable interrupts
// Initialize Timer3 for pins 7 to 10
cli(); // Disable interrupts
TCCR3A = 0; // Clear Timer3 control registers
TCCR3B = 0;
TCNT3 = 0; // Initialize counter value to 0
OCR3A = 16000000 / frequency_7_to_10 - 1; // Set the compare match register
TCCR3B |= (1 << WGM32); // Configure Timer3 for CTC mode
TCCR3B |= (1 << CS30); // Set prescaler to 1
TIMSK3 |= (1 << OCIE3A); // Enable Timer3 compare match interrupt
sei(); // Enable interrupts
}
void loop() {
// Do nothing here, as we handle pin switching in ISRs
}
ISR(TIMER1_COMPA_vect) {
// Turn off all pins from 1 to 6
for (int i = 0; i < NUM_PINS_1_TO_6; i++) {
digitalWrite(pins_1_to_6[i], LOW);
}
// Turn on the current pin
digitalWrite(pins_1_to_6[current_pin_index_1_to_6], HIGH);
// Increment index for pins 1 to 6
current_pin_index_1_to_6 = (current_pin_index_1_to_6 + 1) % NUM_PINS_1_TO_6;
}
ISR(TIMER3_COMPA_vect) {
// Turn off all pins from 7 to 10
for (int i = 0; i < NUM_PINS_7_TO_10; i++) {
digitalWrite(pins_7_to_10[i], LOW);
}
// Turn on the current pin
digitalWrite(pins_7_to_10[current_pin_index_7_to_10], HIGH);
// Increment index for pins 7 to 10
current_pin_index_7_to_10 = (current_pin_index_7_to_10 + 1) % NUM_PINS_7_TO_10;
}