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++;
}