Recognising end of PWM signal

Hello,

I am working on a project on a car and I need to program an arduino that can recognise the end of a signal. the signal can be seen in the screenshot below.

The signal in blue is the crankshaft position signal as a refference, in red you can see the signal the high pressure pump gets to regulate it's pressure and green is the signal the arduino sends out when it thinks it recognises the end of the pump signal.

The length of the signal in green is irrelevant. it just sends out a pulse on the end of signal en stops this puls when the interrupt function is triggert again. You can see that in code below:

const byte End = 2;
const byte Pulse = 7;

//pulsduur
unsigned long delta_HIGH;
unsigned long delta_LOW;
bool eind;

void setup() {
  
  pinMode(End, INPUT);
  pinMode(Pulse, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(End), eind_herkenning, RISING);
  Serial.begin(115200);

}

void loop()
{

  
  if(eind == HIGH){
   digitalWrite(Pulse, LOW);
    delta_HIGH = pulseIn(End, HIGH, 400);
    delta_LOW = pulseIn(End, LOW, 400);
    //Serial.println(delta_end);
    
    
    if(delta_HIGH == 0 && delta_LOW == 0){
      digitalWrite(Pulse, HIGH);
      delta_HIGH = 1;
      delta_LOW = 1;
      //Serial.println("eind");
    }
    
    eind = LOW;
  }
  
}


void eind_herkenning(){
  eind = HIGH;  
}
  

So in short, when a rising edge is triggered, it goes inside a if functie that calculates the pulse-duration of both a logic high and a logic 0. The timeout is because i want both functions to return zero so that it's fast enough to react to the end of the signal. The way that it is programmed now it should return a 0 on delta_HIGH because the signal is LOW and it should return 0 on delta_LOW because it can't measure the pulse-duration in time (400 µs).

this works as it should except for the false positive it returns at the start of the signal. Is there a way to eliminate this false positive?

This variable should be declared volatile because it is updated in the interrupt function and read in loop().

It is not the cause of the problem, but with bool variables, you should assign true or false, not HIGH or LOW. It works ok because HIGH is 1 and LOW is zero, but it is not the "right" way to do it.

1 Like

This code will execute shortly after the rising edge of the red signal. But the signal then remains HIGH for maybe 1ms. So the first pulseIn() could be timing out because it is waiting for another rising edge. Then the second pulseIn() also times out waiting for a falling edge. So both pulseIn() each return zero?

Should i instead trigger on LOW or CHANGE?

Maybe FALLING. Then, the next edge will be a rising edge which the first pulseIn() can begin it's measurement.

1 Like

I will try that and let you know. Maybe I can also try without an interrupt at all. The code that I added in my original post is the full code so maybe it will be fast enough to execute without it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.