error: was not declared in this scope

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 = PWM6
ICR3/100;
}

Please read the "How To Use This Forum" post at the top of any board and learn the forum rules about posting code. Please use code tags in the future. Post your actual complete error message output. Are you sure you have the Mega selected in the tools menu?

Thanks for the hint Delta_G. I had not selected Mega under tools before. Now that I have I am encountered with a different error: exit status 1
lvalue required as left operand of assignment

I get this for all of my if statements.

// 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)

int PWM2;
int PWM6;

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 / 2*N*ICRx
  // 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; // 40 % duty cycle for non-inverted signal
  PWM6 = 60; // 60 % duty cycle for inverted signal

  OCR3B = PWM2*ICR3/100;
  OCR4A = PWM6*ICR3/100;
  }

A3 - What does it represent?

Never mind. Duh.

Can't say I've seen this before

if(analogRead(A3) == 200)
    ICR3 = 14 && ICR4 = 14;

Usually see

if(analogRead(A3) == 200) 
    ICR3 = 14;
    ICR4 = 14;

This

  if(analogRead(A3) == 100) ICR3 = 17 && ICR4 = 17; // ~60 kHz

should be

  if(analogRead(A3) == 100) {
     ICR3 = 17 ;
     ICR4 = 17 ; // ~60 kHz
  }

and similarly for all of the other ‘if’ statements. You cannot use ‘&&’ to create multiple statements. When you have multiple statements, you need to contain them within curly brackets as shown. It is the sequence that is important; some people will place each curly bracket on a separate line; I learned differently.

Also, you should not compare a value from the analogRead(…) function to an exact value. Values from analogRead(…) are rarely so precise. Better would be something like:

  if(analogRead(A3) >= 95 && analogRead(A3) <= 105) {
     ICR3 = 17 ;
     ICR4 = 17 ; // ~60 kHz
  }

Yes, you may use ‘&&’ here because it means that BOTH conditions must be true.

Additionally…
I think that this code may get you into trouble, or perhaps it operates the way that you expect:

  OCR3B = PWM2*ICR3/100;
  OCR4A = PWM6*ICR3/100;

I believe that PWM2, PWM6, and ICR3 are all defined as int. Multiplying two int values is not guaranteed to result in the required int. Dividing by the int 100 is not likely to give the result you expect. int arithmetic is different from floating point arithmetic. For example, 99/100 gives 0 . Better might be:

  float temp ;
  temp = PWM2 ;
  temp *=ICR3 ;
  temp /=100.0 ;
  OCR3B = temp + 0.5 ; //  This gives rounding before truncation to int.
  temp = PWM6 ;
  temp *= ICR3 ;
  temp /=100.0 ;
  OCR4A = temp + 0.5 ; //  This gives rounding before truncation to int.

This assumes that temp is positive. I am not sure what happens if temp is negative.

Of course, all of those analogRead(A3) should be replaced ahead of the first use by something like:

int A3Value=analogRead(A3) ;

and all of those uses of analogRead(A3) should be replaced by A3Value as in

  if(A3Value >= 95 && A3Value <= 105) {
     ICR3 = 17 ;
     ICR4 = 17 ; // ~60 kHz
  }

Good Luck!

@vaj4088, I always use curly brackets. I would have in my reply, but seeing as he had none through that section, I "assumed" he had a reason. Awesome advice, etc. in your reply.

Thanks!

@vaj4088 Thank you for your feedback. I implemented the changes you suggested but I’m still getting the same error. Below is my code in its current form:

// 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)

int PWM2;
int PWM6;

void setup(){
pinMode(2, OUTPUT);
pinMode(7, 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 / 2*N*ICRx
  // where f_clk = 16 MHz, N = prescaler value, ICRx =

  int value_A3 = analogRead(A3);
  float xxx;
  float yyy;
  
  if(value_A3 < 100) {ICR3 = 20; ICR4 = 20;} // ~50 kHz starting frequency for both signals when voltage at A3 = 0V
  if(value_A3 >= 95 && value_A3 <= 105) {ICR3 = 17; ICR4 = 17;} // ~60 kHz
  if(value_A3 >= 195 && value_A3 <= 205) {ICR3 = 14; ICR4 = 14;} // ~70 kHz
  if(value_A3 >= 295 && value_A3 <= 305) {ICR3 = 12; ICR4 = 12;} // ~80 kHz
  if(value_A3 >= 395 && value_A3 <= 405) {ICR3 = 11; ICR4 = 11;} // ~90 kHz
  if(value_A3 >= 495 && value_A3 <= 505) {ICR3 = 10; ICR4 = 10;} // ~100 kHz
  if(value_A3 >= 595 && value_A3 <= 605) {ICR3 = 9; ICR4 = 9;} // ~110 kHz
  if(value_A3 >= 695 && value_A3 <= 705) {ICR3 = 8; ICR4 = 8;} // ~120 kHz
  if(value_A3 >= 795 && value_A3 <= 805) {ICR3 = 7; ICR4 = 7;} // ~130 kHz
  if(value_A3 > 800) {ICR3 = 6; ICR4 = 6;} // ~160 kHz when voltage at A3 = 5V

  
  PWM2 = 35; // 40 % duty cycle for non-inverted signal
  PWM6 = 65; // 60 % duty cycle for inverted signal

  // OCR3B = PWM2*ICR3/100;
  // OCR4A = PWM6*ICR3/100;

  xxx = PWM2;
  xxx *= ICR3;
  xxx /= 100.0;
  OCR3B = xxx + 0.5; // This gives rounding before truncation to int

  yyy = PWM6;
  yyy *= ICR3;
  yyy /= 100.0;
  OCR4A = yyy + 0.5; // This gives rounding before truncation to int

  }

Here’s the error message: I had to omit most of it because of the length constraint.

Arduino: 1.8.4 (Windows 7), Board: “Arduino/Genuino Uno”

C:\Users\dell\Documents\Arduino\variable_frequency_complimentary_signals\variable_frequency_complimentary_signals.ino: In function ‘void setup()’:

variable_frequency_complimentary_signals:11: error: ‘TCCR3A’ was not declared in this scope

TCCR3A = _BV(COM3A1) | _BV(COM3B1) | _BV(COM3C1); // Timer 3 set to non-inverting mode by placing bits in the appropriate slots

^

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,

from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\pgmspace.h:90,

from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28,

from sketch\variable_frequency_complimentary_signals.ino.cpp:1:

variable_frequency_complimentary_signals:11: error: ‘COM3A1’ was not declared in this scope

TCCR3A = _BV(COM3A1) | _BV(COM3B1) | _BV(COM3C1); // Timer 3 set to non-inverting mode by placing bits in the appropriate slots

^

variable_frequency_complimentary_signals:11: error: ‘COM3B1’ was not declared in this scope

TCCR3A = _BV(COM3A1) | _BV(COM3B1) | _BV(COM3C1); // Timer 3 set to non-inverting mode by placing bits in the appropriate slots

^

variable_frequency_complimentary_signals:11: error: ‘COM3C1’ was not declared in this scope

TCCR3A = _BV(COM3A1) | _BV(COM3B1) | _BV(COM3C1); // Timer 3 set to non-inverting mode by placing bits in the appropriate slots

^

Arduino: 1.8.4 (Windows 7), Board: "Arduino/Genuino Uno"

You are compiling code for Mega?

Noticed something with this portion of your sketch.

if(value_A3 < 100) {ICR3 = 20; ICR4 = 20;} // ~50 kHz starting frequency for both signals when voltage at A3 = 0V
  if(value_A3 >= 95 && value_A3 <= 105) {ICR3 = 17; ICR4 = 17;} // ~60 kHz
  if(value_A3 >= 195 && value_A3 <= 205) {ICR3 = 14; ICR4 = 14;} // ~70 kHz
  if(value_A3 >= 295 && value_A3 <= 305) {ICR3 = 12; ICR4 = 12;} // ~80 kHz
  if(value_A3 >= 395 && value_A3 <= 405) {ICR3 = 11; ICR4 = 11;} // ~90 kHz
  if(value_A3 >= 495 && value_A3 <= 505) {ICR3 = 10; ICR4 = 10;} // ~100 kHz
  if(value_A3 >= 595 && value_A3 <= 605) {ICR3 = 9; ICR4 = 9;} // ~110 kHz
  if(value_A3 >= 695 && value_A3 <= 705) {ICR3 = 8; ICR4 = 8;} // ~120 kHz
  if(value_A3 >= 795 && value_A3 <= 805) {ICR3 = 7; ICR4 = 7;} // ~130 kHz
  if(value_A3 > 800) {ICR3 = 6; ICR4 = 6;} // ~160 kHz when voltage at A3 = 5V

Your first if() overlaps the next one. (value_A3 < 100 then value_A3 >= 95. 95 through 99 are less than 100). And the rest have large gaps. Is this by design for some reason? Just curious.

@David_Kujil nice catch; I had not selected Mega under tools.

@DangerToMyself thanks, I fixed the overlapping in the if() statement. The code runs fine now giving me 2 complimentary pwms with variable frequency. I tried to have larger gaps in my if() statements to keep the signals steadier as I adjust the frequency.

So what actually happened back in reply #2 ?