Receiving overflow output and loop stops

Hi, I've been working on a project to calculate the speed of a moving object by using frequencies and i have a separate circuit to transmit and receive my frequencies. However the problem i'm facing is in my Arduino coding itself, not the hardware. When i start up my serial monitor, it shows the maximum value (I constraint it to 99, apparently it has overflown) and only showed one value without it looping like it is suppose to. Im expecting a value 0 as output since there is no pulses received at the strobepin.

unsigned long gatinginterval;
unsigned long int pulseCount = 0,previousPulseCount = 0, latestPulseCount = 0, savedPulseCount = 0;
static unsigned long lastPeriodMs = 0 ;
float frequency,frequency1,radar_speed;

int specialpin = 9;
int strobepin = 6;

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin (9600);
      attachInterrupt(digitalPinToInterrupt(strobepin),pulseISR,RISING); 
      pinMode(strobepin,INPUT);
      pinMode(specialpin,INPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  interrupts();
  
  while(digitalRead(specialpin) != 0); //loop and do nothing, wait for pulse back to 0 to start reading
  

 
    if (millis() - lastPeriodMs > gatinginterval ) {   // gatinginterval in mS
         
         previousPulseCount = latestPulseCount; // save the previous value
         noInterrupts();
            latestPulseCount = pulseCount;  // take a copy of the current value while interrupts are off
         interrupts();
         savedPulseCount = latestPulseCount - previousPulseCount;
         lastPeriodMs += gatinginterval;
    } else {
      return;
    }


    frequency = (savedPulseCount / gatinginterval);
    frequency1 = 1000000.0 * frequency;
    radar_speed = (frequency1) / 4.82;
    Serial.print("The speed is ");
    Serial.print(constrain(radar_speed,0.0,99.0));
    Serial.print(" ");
    Serial.print("km/hr");
    Serial.println("");
 
   
    gatinginterval = pulseIn(specialpin,LOW); //This pin is connected to a variable resistor on a separate PCB which adjust my gating/time interval

}

void pulseISR()
{
  pulseCount++;
}

And i thought the problem lies in this

 gatinginterval = pulseIn(specialpin,LOW); //This pin is connected to a variable resistor on a separate PCB which adjust my gating/time interval

being at the bottom, so i brought it up to the top and tried it. The output this time doesnt even give me any output at all

I noticed that pulseCount should be declared volatile but there may be other issues as well.

So i decided to add

Serial.println(pulseCount);

to see if there is actually any pulses being recorded however it shows 0, indicating that the attachinterrupt is not triggering at all.

But when i use an oscilloscope to probe the pin where the pulses are going in, it shows the presence of pulses going into the pin

What type of Arduino are you using? The attachInterrupt(...) function may only be used on a few pins; which pins is dependent on which Arduino.

See attachInterrupt() - Arduino Reference for more information if necessary.

Thanks, problem has been fixed :slight_smile: Everything is working fine now

I am very happy for you that you got things working. Please help us to help others by explaining what was wrong and how you fixed it. We all have problems. We all make mistakes. It will not be embarrassing to explain. THANKS!

Pattowick:
Thanks, problem has been fixed :slight_smile: Everything is working fine now

This seems to be the same subject as your other Thread. Please don't double post. It just wastes everyone's time when the project info is scattered in different Threads and especially if two people give similar replies.

...R