UNO timer interrupt used by Lib <IfxMotorControlShield.h>

Hi to all,

I'm trying to drive a DC-motor as fast as the (manual) rotation of an encoder.
Therefore I use the library in topic, and a fixed timerinterrupt to find out the speed of the encoder.
It seems that can't control the H-bridge when I enable a timer interrupt.
It probably uses the same timer, but I don't know how to control that.
Underneath a simple test:

void setup() 
{
  Serial.begin(9600);
  while(!Serial);
  
  //Drive motor
  if(ifxMcsUniDirectionalMotor1.begin())
  Serial.println("Init fail +++ Init Fail +++ Init Fail");
  ifxMcsUniDirectionalMotor1.start();
  ifxMcsUniDirectionalMotor1.setSpeed(120);
  delay(1000);
  ifxMcsUniDirectionalMotor1.setSpeed(160);
  delay(1000);
  
  //Setup Timer interrupt
  TimeInterrupt.begin(PRECISION); //Initialize the interrupt with high precision timing
  TimeInterrupt.addInterrupt(encoderDelta, 100); 
  TimeInterrupt.addInterrupt(info, 500);
  
  // Drive motor again, no succes  
  ifxMcsUniDirectionalMotor1.start();  
  ifxMcsUniDirectionalMotor1.setSpeed(120);
  delay(1000);
  ifxMcsUniDirectionalMotor1.setSpeed(160);
  delay(1000);
  
  //Serial.println("Stopping motor");
  //ifxMcsUniDirectionalMotor1.stop();
}

Attached the library code that shows the timer use, but it does that kinda directly to the CPU. I can't read that very well. Anyone any ideas how to get around this one? Tx. Kind regards.

IfxMotorControlShieldUtil.cpp (4.6 KB)

Post complete code, that code doesn't compile! Also post a link to your TimerInterrupt library, there are several with such a name.

Tx Pylon, it's the standard TimeIterrupt lib in arduino online. GitHub - matthew-dickson-epic/TimeInterrupt

#include <TimeInterrupt.h>
#include <IfxMotorControlShield.h>
#include <Encoder.h>
#include <MovingAverage.h>

Encoder myEnc(5, 6);
MovingAverage<unsigned> speedMA(10, 0);
MovingAverage<unsigned> encoderDeltaMA(10, 0);
 
void setup() 
{
  Serial.begin(9600);
  while(!Serial);
  
  //Drive motor
  if(ifxMcsUniDirectionalMotor1.begin())
  Serial.println("Init fail +++ Init Fail +++ Init Fail");
  ifxMcsUniDirectionalMotor1.start();
  ifxMcsUniDirectionalMotor1.setSpeed(120);
  delay(1000);
  ifxMcsUniDirectionalMotor1.setSpeed(160);
  delay(1000);
  
  //Setup Timer interrupt
  TimeInterrupt.begin(PRECISION); //Initialize the interrupt with high precision timing
  TimeInterrupt.addInterrupt(encoderDelta, 100); 
  TimeInterrupt.addInterrupt(info, 500);
  
  // Drive motor again, no succes  
  ifxMcsUniDirectionalMotor1.start();  
  ifxMcsUniDirectionalMotor1.setSpeed(120);
  delay(1000);
  ifxMcsUniDirectionalMotor1.setSpeed(160);
  delay(1000);
  
  //Serial.println("Stopping motor");
  //ifxMcsUniDirectionalMotor1.stop();
}


long oldPositionEncoder  = -999;
long newPositionEncoder = 0;

int potValueSpeedOld = 0;
int potValueLimitOld = 0;
int potValueSpeed = 0;
int potValueLimit = 0;
int speedGain = 0;
unsigned int motorSpeed = 0;
unsigned int deltaMA = 0;
int output = 0;


void loop() {
  
  // read encoder value and print on change

    
  // read pot values 
  potValueSpeed = analogRead(A4);
  potValueLimit = analogRead(A5);
  

    
    
      
   
    /*Serial.print("\t");
    Serial.print("Delta");
    Serial.print("\t");
    Serial.println(newPositionEncoder);
    
    
  
    
    
  
  if (abs(potValueLimit - potValueLimitOld) > 10) {
    potValueLimitOld = potValueLimit;
    Serial.print("Limit");
    Serial.print("\t");
    Serial.println(potValueLimit);
  }
  
  
  

  
  // Print to serial
  Serial.print("DeltMA  ");
  Serial.print("\t");
  Serial.println(deltaMA);
  

    

  */
  newPositionEncoder = myEnc.read();
  if (newPositionEncoder != oldPositionEncoder) {
    
    Serial.println(newPositionEncoder);
    
  }
  
/*    long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
*/  


  
  ifxMcsUniDirectionalMotor1.setSpeed(output);

  
  
  //Serial.println("Stopping motor");
  //ifxMcsUniDirectionalMotor1.stop();  
  
}

int encoderDelta() {
  
  Serial.println(millis());
  
  // Pushes the input in the moving average object
  speedMA.push(potValueSpeed);
  // Get moving average
  speedGain = speedMA.get();
  // Print to serial
  
  // Push the input in the moving average object
  encoderDeltaMA.push(abs(newPositionEncoder - oldPositionEncoder));
  //oldPositionEncoder = newPositionEncoder;  
  //Get moving average
  deltaMA = encoderDeltaMA.get();
  oldPositionEncoder = newPositionEncoder;
  
  // Amplify
  motorSpeed = speedGain * deltaMA;
  // Map motorSpeed to 8-bit output 
  output = map(motorSpeed, 0, 32000, 0, 255);
    // Set output duty cycle

      // Print to serial
    Serial.print("motorSpeed");
    Serial.print("\t");
    Serial.println(motorSpeed);
    
return output;

}

void info() {
  Serial.print("G  ");
  Serial.println(speedGain);
  Serial.print("SP ");
  Serial.println(potValueSpeed);
  Serial.print("OU ");
  Serial.println(output);
}

I seemed to have misread that the external interrupt pins on UNO are 5&6. They appear to be pins 2&3. Now 3 is the PWM output to my motor shield. That can't work well... Tx Pylon. Problem solved! :smiley: