Problems while execution

Hi

I have a problem when i try to run my program. The program is to run a differential drive robot.

The problem is whenever I include the print statements, there is nothing in the output, I mean the robot does not run at all... if I just remove the print statements, or atleast some of them, there is some output.

anyidea why this is happening.

void setup()
{
          //Initialize Serial Link
          //##################################################    
    Serial.begin(9600);
          //Set Initial Encoder Positions
          //##################################################        
    encoderMotorL.setPosition(0);
    encoderMotorR.setPosition(0);
          //Assign Interrupts for Encoder A and B
          //##################################################        
    attachInterrupt(0, HandleMotorLInterruptA, CHANGE); // Pin 2
    attachInterrupt(1, HandleMotorLInterruptB, CHANGE); // Pin 3
    attachInterrupt(5, HandleMotorRInterruptA, CHANGE); // Pin 18
    attachInterrupt(4, HandleMotorRInterruptB, CHANGE); // Pin 19
          //Initialize motor spin direction
          //##################################################        
    digitalWrite(DirMotorL, LOW); digitalWrite(DirMotorR, LOW);
    Serial.print("Running \n");
          //Initialize Timer1 to 1sec period and attach interrupt
          //##################################################        
    //Serial.print("\nVeld\tXd\tYd\tVxd\tVyd\tAxd\tAyd\tVelL\tVelR\tTheta\tPosX\tPosY\tVx\tVy\tVc\tAc\tv1\tv2\twL\twR\twLa\twRa\n");
    Timer1.initialize(500000);
    Timer1.attachInterrupt(Control);
}
void loop()
{
    --some code--
    delay(10);
}
void Control()
{  
  --again some code --
  printall()
}
void printall()
{
   Serial.print(Veld);Serial.print("\t");Serial.print(Xd);Serial.print("\t");
   Serial.print(Yd);Serial.print("\t");Serial.print(Vxd);Serial.print("\t");
   Serial.print(Vyd);Serial.print("\t");Serial.print(Axd);Serial.print("\t");
   Serial.print(Ayd);Serial.print("\t");
   Serial.print(VelL);Serial.print("\t");Serial.print(VelR);Serial.print("\t");
   Serial.print(Theta);Serial.print("\t");
   Serial.print(PosX);Serial.print("\t"); Serial.print(PosY);Serial.print("\t");
   Serial.print(Vx);Serial.print("\t");
   Serial.print(Vy);Serial.print("\t");Serial.print(Vc);Serial.print("\t");
   Serial.print(Ac);Serial.print("\t");
   Serial.print(v1);Serial.print("\t");Serial.print(v2);Serial.print("\t");
   Serial.print(wL);Serial.print("\t");Serial.print(wR);Serial.print("\t");
   Serial.print(wLa);Serial.print("\t");Serial.print(wRa);Serial.print("\n");
}

Thanks for your help
ksp

One obvious problem is that you are trying to print to the serial port from inside an interrupt service routine, which is a bad idea and asking for trouble. Change your code to avoid doing this.

Interrupt service routines should be small and fast and avoid doing anything that blocks waiting for something to happen - especially when that something depends on interrupts, such as talking to a serial port.

Hi

Thanks for your suggestion. I removed the print from the ISR and placed it in the main loop. Now it works without any problem, but I get output every 10ms :smiley:

ksp

Now it works without any problem, but I get output every 10ms

Look at the blink without delay example to learn how to print data at whatever interval you want, without using the dreaded delay function.

Or, simply change the code to only print what has changed, when a change occurs.