I’m trying to make Arduino Mega output complimentary square waves with variable frequency but keep getting ‘TCCR3A’ was not declared in this scope error on the highlighted line. If I have TCCR3A within my setup() function only, why am I getting this error?
// This program outputs 2 complimentary square wave signals with variable frequency
// by using pins on Timer 3 (pins 2,4,5) and pins on Timer 4 (pins 6,7,8)
void setup(){
pinMode(2, OUTPUT);
pinMode(6, OUTPUT);
TCCR3A = _BV(COM3A1) | _BV(COM3B1) | _BV(COM3C1); // Timer 3 set to non-inverting mode by placing bits in the appropriate slots
TCCR4A = _BV(COM4A1) | _BV(COM4B1) | _BV(COM4C1) | _BV(COM4A0) | _BV(COM4B0) | _BV(COM4C0); // Timer 4 set to inverting mode
TCCR3B = _BV(WGM33) | _BV(CS31); // Prescaler set to 8 for Timer 3
TCCR4B = _BV(WGM43) | _BV(CS41); // Prescaler set to 8 for timer 4
}
void loop() {
// This loop lets different values read from the potentiometer place certain values in ICR3 and ICR4
// to produce certain frequencies at pins 2 and 6 according to the formula f_output = f_clk / 2NICRx
// where f_clk = 16 MHz, N = prescaler value, ICRx =
if(analogRead(A3) < 100) ICR3 = 20 && ICR4 = 20; // ~50 kHz starting frequency for both signals when voltage at A3 = 0V
if(analogRead(A3) == 100) ICR3 = 17 && ICR4 = 17; // ~60 kHz
if(analogRead(A3) == 200) ICR3 = 14 && ICR4 = 14; // ~70 kHz
if(analogRead(A3) == 300) ICR3 = 12 && ICR4 = 12; // ~80 kHz
if(analogRead(A3) == 400) ICR3 = 11 && ICR4 = 11; // ~90 kHz
if(analogRead(A3) == 500) ICR3 = 10 && ICR4 = 10; // ~100 kHz
if(analogRead(A3) == 600) ICR3 = 9 && ICR4 = 9; // ~110 kHz
if(analogRead(A3) == 700) ICR3 = 8 && ICR4 = 8; // ~120 kHz
if(analogRead(A3) == 800) ICR3 = 7 && ICR4 = 7; // ~130 kHz
if(analogRead(A3) > 800) ICR3 = 6 && ICR4 = 6; // ~160 kHz when voltage at A3 = 5V
PWM2 = 40;
PWM6 = 60;
OCR3B = PWM2ICR3/100;
OCR4A = PWM6ICR3/100;
}