Loop between rising and falling edge

I am unable to solve the following:

  • Detect rising edge
  • then check if a pin x went high
  • if yes then store a boolean True in some variable
  • end loop when falling edge is detected
  • Turn on LED if boolean True was found in the variable

Any help guy would be greatly appreciated.

  • end loop when falling edge is detected

What is going on in this loop ?

How many states can the system be in ?

What have you tried ?

This loop checks if a certain pin went HIGH between the rising and falling edge. If that certain pin high during this period, then switch ON a Led.

I have tried to have an interrupt fired when the pin goes high (i.e. when rising edge is detected). Now I am trying in the ISR to check if that pin went HIGH. But I need to store this result and use it outside the ISR (i.e. after falling edge has been detected).

I dont know how to proceed now.

But I need to store this result and use it outside the ISR

What is stopping you putting the value in a variable for use later ? If you do store it in a variable then declare it globally and volatile.

How long is the period between the rising and falling edge where you want to be checking the state of another pin?

What is creating the rising and falling edges of the period?

What else is going on in the program, and can the check be blocking except for the detection of the falling edge?

This loop

What loop? Why haven't you posted code, using code tags?

bitoff_arduino:
I am unable to solve the following:

  • Detect rising edge

while(pulse == low ) wait for rising edge
{
rising edge detected
Pulse detected = HIGH;
while(pulse == HIGH) wait for trailing edge

or
while (pulse);

and skip monitoring

{
optional pulse monitoring now
} stay in trilling edge loop
break; exit entry while loop
} stay in rising edge loop

  • then check if a pin x went high
  • if yes then store a boolean True in some variable
  • end loop when falling edge is detected
  • Turn on LED if boolean True was found in the variable

Any help guy would be greatly appreciated.

bitoff_arduino:
I have tried to have an interrupt fired when the pin goes high (i.e. when rising edge is detected). Now I am trying in the ISR to check if that pin went HIGH.

If the interrupt was triggered by a rising edge why waste time checking for a HIGH - what else could it be?

...R

bitoff_arduino:
This loop checks if a certain pin went HIGH between the rising and falling edge.

Well, of course it does go high, as otherwise it would not have a rising or falling edge. Your pin was LOW, then went HIGH, then LOW again. That's how you get a rising and falling edge.

Normally you would detect it using a state change detection:

bool state = LOW;
void loop() {
  if (digitalRead(pin) == HIGH && state = LOW) {
    // Rising edge detected!
    state = HIGH; // Record current state.
  }
  if (digitalRead(pin) == LOW && state == HIGH) {
    // Falling edge detected!
    state = LOW; // Record current state.
  }
}

Thank you for your suggestions. I have slightly changed my approach.

The problem to be solved is:

I have a device which detects presence of red and green dye in flowing microliter droplet and outputs digital signal if a particular dye is present.
So if both red and green are present in a droplet it outputs two digital signals. If only red or green is present it outputs only one corresponsing signal. The more the concentration of the dye higher the pulse width of the digital signal.

In this case all my droplets will have green signal but some will have red missing. Now my task is to find the droplet in which the red signal is absent, and trigger a camera to take images of that particular droplet. The camera takes digital pulses.

Here is what I have done. The thing I have a digital trigger exactly opposite to my aim. I have a digital signal when I have both red and green signals. But I need to have a signal when only green is present and red is absent.

See the attached picture for results. The green and red are the output signals from the device. White signal is the output (which will be used to trigger the camera) of the following code.

const int  redChannel = 9;                
const int greenChannel = 10;
const int  OutputPin = 5;  
int pulseCounter = 0; 

int redState;
int greenState;
boolean storeState = LOW;
 
void setup() {
  pinMode(redChannel, INPUT); 
  pinMode(greenChannel, INPUT);     
  pinMode(OutputPin, OUTPUT); 
 Serial.begin(9600);    
  }
 
void loop() {
    greenState  = digitalRead(greenChannel);
    
    while (greenState == HIGH) {
      redState  = digitalRead(redChannel);
      if (redState == HIGH){
        storeState = HIGH;}  
      greenState  = digitalRead(greenChannel);
  }

     if (storeState == HIGH){
     digitalWrite(OutputPin, HIGH);
     delayMicroseconds(200);
     digitalWrite(OutputPin, LOW);}
     storeState = LOW;
}

Not too hard, and you're quite close. Timing is important here of course.

Do you want to trigger the camera always exactly when the green signal is finished, or a certain time after the signal begins?

You need to do some state checking.
Set two flags when you see a green signal: that you see a green signal, and that you did not (yet) see a red signal.
If you see a red signal, reset that second flag.
If the green signal stop and the "no red" flag is still set, you haven't seen red and trigger the camera.

bool haveGreen;
bool noRed;

void loop() {
  if (digitalRead(greenChannel) && haveGreen == false) { // Detect start of the green pulse.
    haveGreen = true;
    noRed = true;
  }
  if (digitalRead(redChannel) && noRed) { // Start of the red pulse. May or may not happen.
    noRed = false;
  }
  if (digitalRead(greenChannel) == LOW && haveGreen) { // End of the green pulse.
    haveGreen = false;
    if (noRed) { // Green is finished and we didn't see a red pulse.
     digitalWrite(OutputPin, HIGH);
     delayMicroseconds(200);
     digitalWrite(OutputPin, LOW);
    }
  }
}

Also note how this code is not blocking: you could do other things in the meantime.

Thank you so very much. This is what i have been trying to do for days. I have attached the result.

wvmarle:
Do you want to trigger the camera always exactly when the green signal is finished, or a certain time after the signal begins?

The camera should take the photo of the droplet of interest, i.e. the one with no red. So ideally it should be triggered the green signal is still high. Is that possible in this solution, since we make a decision of no Red was found after the green has gone down?

But this will work too, after some optics adjustments.

Then you have to record when the green pulse started (millis() or micros() depending on the required time scale), wait until you're sure there's no red pulse coming, and take your photo.

Or if that doesn't work, take a photo of every single droplet (the moment you get the green pulse, or after a fixed interval), and have the Arduino provide a signal so the camera can mark the images as "hasRed" and "doesntHaveRed" afterwards.