[Solved] Compile errors trying to use interrupt.h with Pololu A-Star 32U4 Micro

I wanted to try out the Pololu A-Star 32U4 Micro. I have a program written for the Arduino Uno that compiles and runs fine for it. But I get compiler errors when I select as my board “Pololu A-Star 32U4” instead of “Arduino Uno.” It seems like the variable names in the libraries are not being loaded. My code is below:

#include <ServoTimer2.h>
#include "DualVNH5019MotorShield.h"
#include <io.h>
#include <interrupt.h>

#define BuzzerPin 5
#define LeftServoPin 11
#define RightServoPin 3

unsigned char cmd, value;
float Steering;
int throttle;
DualVNH5019MotorShield md;

ServoTimer2 leftServo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
ServoTimer2 rightServo;

int posLeft = 0;    // variable to store the servo position 
int posRight = 0;

void setup() {

  // set up Arduino motor shield
  md.init();

  // set up servos for steering
  leftServo.attach(LeftServoPin);
  rightServo.attach(RightServoPin);
  
  pinMode(BuzzerPin, OUTPUT);
  pinMode(RightServoPin, OUTPUT);
  pinMode(LeftServoPin, OUTPUT);
  
  leftServo.write(1245);
  rightServo.write(1606);
    
  // set compare match register to desired timer count:
//  OCR2A = 150624;
 
  // start serial port at 115,200 bits per second
  Serial.begin(115200);
  // wait until serial contact is made 
  while (Serial.available() <= 0) {
    delay(300);
  }
}

// this interrupt service routine for timer 2 toggles the horn buzzer


ISR(TIMER2_COMPA_vect)
{
  digitalWrite(BuzzerPin, !digitalRead(BuzzerPin));

  // reset counter 2
  TCNT2 = B00000000;
}

void loop() {

  // if we get a valid byte, read throttle signal:
  if (Serial.available() > 0) {

    // get an incoming byte:

       cmd = Serial.read();

	// If it's not a command character, ignore it and wait for the next one
	if(cmd != 'F' && cmd != 'B' && cmd != 'H' && 
          cmd != 'R' && cmd != 'L')return;
 
	// got the command, now wait for the data char
	while(Serial.available() == 0);
	value = Serial.read();
        
	// got the command and value, now do the command
	switch(cmd) {

  	  case 'F':
            value = constrain(value, 0, 255);
            throttle = map(value, 0, 255, 0, 400);
            md.setM2Speed(throttle);
//            Serial.println(throttle);
 	    break;
                
          case 'B':
            value = constrain(value, 0, 255);
            throttle = map(value, 0, 255, 0, -400);
            md.setM2Speed(throttle);
  	    break;
 
 	  case 'H':
            if (value == 0) {
              cli();         // disable global interrupts

              // turn Timer2 off
              cli();         // disable global interrupts
              TCCR2A = 0;    // set entire TCCR2A register to 0
              TCCR2B = 0;
              
              // turn off CTC mode:
              TIMSK2 |= 0;
              sei();          // enable global interrupts

            } else {

              // initialize Timer2
              cli();         // disable global interrupts
              TCCR2A = 0;    // clear entire TCCR2A register
              TCCR2B = 0;
              
              // turn on CTC mode:
              TCCR2B |= (1 << WGM21);

              // Set timer clock speed:

              TCCR2B |= (1 << CS22);

              // enable timer compare interrupt:
              TIMSK2 |= (1 << OCIE2A);

              sei();          // enable global interrupts

            }
 	    break;
                
  	  case 'R':
            value = constrain(value, 0, 255);
            Steering = float(value - 127);
            if (Steering > 0) {
              posRight = 103 - int(Steering * 0.258);  // divide by 128 and times by 33
            } else {
              posRight = 103 + int(Steering * -0.449);  // divide by -127 and times by 57
            }
            posRight = map(posRight, 0, 180, 544, 2400);
            rightServo.write(posRight);
 	    break;
                
  	  case 'L':
            value = constrain(value, 0, 255);
            Steering = float(value - 127);
            if (Steering > 0) {
              posLeft = 68 - int(Steering * 0.453); // divide by 128 and times by 58
            } else {
              posLeft = 68 + int(Steering * -0.252); // divide by -127 and times by 32
            }
            posLeft = map(posLeft, 0, 180, 544, 2400);
            leftServo.write(posLeft);
 	    break;

          }
  }
}

The error messages I get are things like TCNT2 is not declared in this scope.

Does the problem lie in my trying to use these libraries with the A-Star 32U4? Or have I just somehow not put the libraries in the right place?

Thank you.

Does the problem lie in my trying to use these libraries with the A-Star 32U4?

Yes. It is NOT an Arduino.

Yes, I know it is not an Arduino. But it does run in the Arduino environment. I thought the program I wrote might not run on this board, but was surprised to find that it would not even compile. But I tried to compile the program for an Arduino Leonardo, which uses the same 32U4 chip, and got the same error.

So I guess the Arduino libraries only work for the 328 and 168 boards, or at least the libraries that use the timers. And I guess the environment is set up so that the libraries do not load, thus generating a compiler error to let people like me know that the libraries don't work. Is that right?

I thought the program I wrote might not run on this board, but was surprised to find that it would not even compile.

Why does that surprise you? I can write code for a Mega that won't compile for other Arduinos if it uses register names that are not defined for the other Arduinos.

You'd need to look at the datasheet for the chip that the code compiles for, to see what the register name is used for. TCNT2 looks like a timer register name. Presumably, timer 2. The smaller Arduinos have only two timers - 0 and 1. The Mega has 5 timers - 0 to 4.

PaulS:
Why does that surprise you?

Because I haven’t worked much with libraries. I didn’t realize that many of them are not going to work with the Arduino Leonardo or similar boards. Now I know.

The 32u4 has three timers. I’ll try to make my way through the code libraries and see where the timer-related variables are defined, and define them for the 32u4 chip.

I see that the definitions that I need (TCNT2, TCCR2A, TCCR2B, WGM21, CS22, etc.) are defined in a header file that gets selected in avr/io.h.

But when I look at the header file io.h it seems to be a version from 12/4/2008. Are there no newer versions of avr/io.h? Or am I reading that date wrong?

Assuming that there is no version of io.h that will work for my board, I guess I can create a new version that I can include in. But any suggestions would be appreciated.

I'm not sure exactly what is going on, but I found out that the problem is not with using the libraries with an ATmega32u4 board. They seem to work fine. The trouble was that I was trying to use Timer2, which apparently doesn't work on those boards. I switched to Timer3, and the code compiles fine now.