Timers in Arduino not working

So im working on a school project and the professor forces us to use the build in timers of the atmega chip to create a PWM-signal to control 3 servo’s. After a lot of research and a lot of programming and debugging i managed to activate and set all three timers and utilize 2 of them.

After i finished utilizing the 3rd one i tried to compile and got the error massage “return status 1”

Code:

#define servo1 9
#define servo2 10
#define servo3 11

unsigned char flag0 = 0;
unsigned char flag1 = 0;
unsigned char flag2 = 0;

unsigned int tijd0_2 = 36;        // tweede globale, tweede tijd  ; x ms hoog ; 2 = 0 graden ; 1 = ongeveer 90 graden
unsigned int tijd0_1 = 320-tijd0_2;
unsigned int tijd1_2 = 36;        // tweede globale, tweede tijd  ; x ms hoog ; 2 = 0 graden ; 1 = ongeveer 90 graden
unsigned int tijd1_1 = 320-tijd1_2;
unsigned int tijd2_2 = 36;        // tweede globale, tweede tijd  ; x ms hoog ; 2 = 0 graden ; 1 = ongeveer 90 graden
unsigned int tijd2_1 = 320-tijd2_2;



void setup()
{
  Serial.begin(9600);
  pinMode(servo1, OUTPUT);
  pinMode(servo2, OUTPUT);
  pinMode(servo3, OUTPUT);

  // initialize timer1
  noInterrupts();           // disable all interrupts
  
  TCCR0A = 0;
  TCCR0B = 0;
                            // 2Hz preload value
  TCNT0 = 34286;            // preload timer 65536-16MHz/256/2Hz first trip value (berekening is belangrijk!)
  TCCR0B |= (1 << CS12) | (1 << CS10);    // 1024 prescaler
  
  TIMSK0 |= (1 << TOIE1);   // enable timer overflow interrupt
  
  

  TCCR1A = 0;
  TCCR1B = 0;
                            // 2Hz preload value
  TCNT1 = 34286;            // preload timer 65536-16MHz/256/2Hz first trip value (berekening is belangrijk!)
  TCCR1B |= (1 << CS12) | (1 << CS10);    // 1024 prescaler
  
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt


  
  TCCR2A = 0;
  TCCR2B = 0;
                            // 2Hz preload value
  TCNT2 = 34286;            // preload timer 65536-16MHz/256/2Hz first trip value (berekening is belangrijk!)
  TCCR2B |= (1 << CS12) | (1 << CS10);    // 1024 prescaler
  
  TIMSK2 |= (1 << TOIE1);   // enable timer overflow interrupt
 

  interrupts();             // enable all interrupts
}

/*

ISR(TIMER0_OVF_vect)        // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{
  if (flag0)
  {
    digitalWrite(servo1, HIGH);
    flag0 = 0;
    TCNT0 = 65536 - tijd0_2;          // preload timer with next wait value
        Serial.println("iets");
  }
  else
  {
    digitalWrite(servo1, LOW);
    flag0 = 1;
    TCNT0 = 65536 - tijd0_1;          // preload timer with next wait value
  }
}
*/


ISR(TIMER1_OVF_vect)        // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{

  if (flag1)
  {
    digitalWrite(servo2, HIGH);
    flag1 = 0;
    TCNT1 = 65536 - tijd1_2;          // preload timer with next wait value
    Serial.println("iets");
  }
  else
  {
    digitalWrite(servo2, LOW);
    flag1 = 1;
    TCNT1 = 65536 - tijd1_1;          // preload timer with next wait value
  }
}

ISR(TIMER2_OVF_vect)        // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{

  if (flag2)
  {
    digitalWrite(servo3, HIGH);
    flag2 = 0;
    TCNT2 = 65536 - tijd2_2;          // preload timer with next wait value
    Serial.println("iets");
  }
  else
  {
    digitalWrite(servo3, LOW);
    flag2 = 1;
    TCNT2 = 65536 - tijd2_1;          // preload timer with next wait value
  }
}


void loop()
{
//funcites
}

The commented function is the one causing trouble, as you can see its identical to the other 2 functions utilizing the timer, with exeption to the needed changes in the variables.

I have no idea how to fix this and my project is due on teusday. i’d be great if some of you codewizards can help me out here.

PS: some of the commented out calculations are wrong due to me being sloppy, i’m aware of this and will fix it when i pick this up again

(deleted)

So what exactely does the compiler print out? Please post the entire error message, not only the conclusion of it..

My bad, kind of new here!

Error message:

C:\Users\Jell-o\Google Drive\Project 1C - Delta\Code\PWM met timers\PWM_met_timer_v8\PWM_met_timer_v8.ino: In function 'void setup()':

C:\Users\Jell-o\Google Drive\Project 1C - Delta\Code\PWM met timers\PWM_met_timer_v8\PWM_met_timer_v8.ino:33:9: warning: large integer implicitly truncated to unsigned type [-Woverflow]

TCNT0 = 34286; // preload timer 65536-16MHz/256/2Hz first trip value (berekening is belangrijk!)

^

C:\Users\Jell-o\Google Drive\Project 1C - Delta\Code\PWM met timers\PWM_met_timer_v8\PWM_met_timer_v8.ino:53:9: warning: large integer implicitly truncated to unsigned type [-Woverflow]

TCNT2 = 34286; // preload timer 65536-16MHz/256/2Hz first trip value (berekening is belangrijk!)

^

wiring.c.o (symbol from plugin): In function `__vector_16':

(.text+0x0): multiple definition of `__vector_16'

sketch\PWM_met_timer_v8.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

First of all, the "TCNTn" are 8 bit registers and you cannot assign any value above 255 to them. Secondly, you must declare any global variable (flag0, flag1, flag2) accessed from an interrupt function as volatile.

Thanks a lot for the help, i appreciate it greatly!

First of all, the "TCNTn" are 8 bit registers and you cannot assign any value above 255 to them. Secondly, you must declare any global variable (flag0, flag1, flag2) accessed from an interrupt function as volatile.

Did these things, some of the errors are gone but still ended up with this error:

wiring.c.o (symbol from plugin): In function `__vector_16':

(.text+0x0): multiple definition of `__vector_16'

sketch\PWM_met_timer_v8.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

You have an interrupt conflict.

sterretje:
You have an interrupt conflict.

Could you maybe give me some more context? What do you exactly mean? Why does it only pop up on Timer1 and not on Timer2/3?

It does not pop up on timer 1 but on timer 0. The part that you commented out causes the problem when you uncomment it.

The error message indicates vector 16; looking at the datasheet of the 328P (page 57), vector 16 is the timer 0 overflow interrupt (you need to subtract 1 from the vector number as the table starts at 1). Looking further at the error message, it complains about wring.c.o (and hence wiring.c). Digging through there

#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ISR(TIM0_OVF_vect)
#else
ISR(TIMER0_OVF_vect)
#endif
{
	// copy these to local variables so they can be stored in registers
	// (volatile variables must be read from memory on every access)
	unsigned long m = timer0_millis;
	unsigned char f = timer0_fract;

	m += MILLIS_INC;
	f += FRACT_INC;
	if (f >= FRACT_MAX) {
		f -= FRACT_MAX;
		m += 1;
	}

	timer0_fract = f;
	timer0_millis = m;
	timer0_overflow_count++;
}

So you have two codes for the same interrupt. And that will not work.

I suspect that the professor expects you to use the PWM functionalities of the timers.