I cant see the problem with this code. It just hangs - Any help appreciated

folks

I need help with some code.
its pretty simple, but I cant see what Im doing wrong.

I have a couple of Interrupts and I'm wondering if maybe I've set them up incorrectly and its causing the issue I'm seeing?

I'm listening on Pin 2 for a digital signal coming from a speed sensorr that is measuring distance travelled.

For each 10 meters I get 63 pulses.

The timer interrupt fires every second and is supposed to display various stats including KPH and distance travelled.

For some reason the serial.print statements in the 'timeElapsed' function cause the program to hang.

If I delete any 2 statement, it works fine.

Code is below

Sam

#include <PinChangeInt.h>	
#include <PinChangeIntConfig.h>
#include <TimerOne.h>		

int state=0;
int burp=0;   
int burpsPerSecond=0;  

double offset =0.0;
double previousOffset=9.9;
double startingValue=1.50;

int metersTravelled=0;
int oneHundredMeters=0;
int kilometers=0;

  
#define PIN 2  
#define AnalogPIN 1
//=============================================================
//
//=============================================================
void setup()
{
        Serial.begin(9600);
	Serial.println("Reading Pin 2  - interrupt test");
        pinMode(PIN, INPUT);                           
	digitalWrite(PIN, HIGH);                     
	PCintPort::attachInterrupt(PIN, risingPinDetected,RISING);  
        Timer1.initialize(1000000);                 // initialize timer1, and set a 1 second period
        Timer1.attachInterrupt(timeElapsed);   // attaches callback() as a timer overflow interrupt
}  
//=============================================================
//
//=============================================================
void loop() 
{
  offset = analogRead(AnalogPIN) * ((5.0 / 1023.0)/10) + startingValue; // check if the trim pot value has changed - this is the offset for fine adjustment
  delay(1000);
}
 //=============================================================
 //
 //=============================================================
void risingPinDetected()	//on the rising edge of the currently inturrupted pin
{
	PCintPort::detachPinChangeInterrupt(PIN); 
	burp++;            //  burps per distance
        burpsPerSecond++;  //  burps per second (used in timeElapsed function)
        if(burp==63)       // 63 = 10 meters travelled
        {
          metersTravelled+=10;
          if(metersTravelled>=100)
          {
            metersTravelled=0;
            oneHundredMeters+=100; // keep track of 1/10 of a Kilometer
            if(oneHundredMeters>=1000)
            {
              oneHundredMeters=0;
               kilometers++;
            }
          }
           burp=0;
        }
 	PCintPort::attachInterrupt(PIN, risingPinDetected,RISING); 
 }
 //=============================================================
 //
 //=============================================================
void timeElapsed()
{
       Serial.print("Burps per second [");
       Serial.print(burpsPerSecond); // burps per second
       Serial.print("] offset [");
       Serial.print(offset );
       Serial.print("] KPH [");
       Serial.print(burp / offset);
       Serial.print("] Meters x 10 [");
       Serial.print(oneHundredMeters );
       Serial.print(" Total Kilometers: ");
       Serial.print(kilometers );  
       Serial.println("]"); 
       burpsPerSecond=0;
}

Moderator edit: Why is it so difficult to do such a simple thing as code tags?

How big is the Serial output buffer?

Serial.print() uses it's own set of interrupts. Could it be causing a conflict?

You cannot use Serial.print in an interrupt handler.

Also, why do you detach, then re-attach the risingPinDetected interrupt?

Regards,
Ray L.

RayLivingston:
You cannot should not use Serial.print in an interrupt handler.

. . .which is why I asked the question about the size of the serial buffer :wink:

If you read the original post, it says

If I delete any 2 statement, it works fine.

Why in the world would you have an interrupt driving display code. Do you really think your human eyes could tell if the display update was a few hundred microseconds off?

sammypati:
For some reason the serial.print statements in the 'timeElapsed' function cause the program to hang.

Hanging a program with Serial.print while handling an interrupt is just like shooting yourself in the foot when handling a gun:

Don't try this at home, kids!

	PCintPort::attachInterrupt(PIN, risingPinDetected,RISING);

Why are you doing pin change interrupts on an external interrupt pin? That's like doing SoftwareSerial on the hardware serial pins - possible but ridiculous.