Code for breathing monitor

I am new to arduino and I am having difficulty with a project I am doing.

I am using a stretch sensor as a breathing monitor. I need to write a code so that it displays the change in voltage in real time and if the wearer stops breathing for 20 seconds an alarm goes off. I have been using nested "if" statements but that seems to only take the last value and runs it through the loop but I need to break out of the loop whenever the condition is false and start again. Haven't even started the graphing part yet. Any help would be greatly appreciated.

my attempt

if (analogValue > threshold) { delay(5000); if (analogValue > threshold){ delay(5000); if (analogValue > threshold){ delay(5000); if (analogValue > threshold){ delay(5000); if (analogValue > threshold){ digitalWrite(ledPin, HIGH);} else { digitalWrite(ledPin, LOW); } } } } }

You do realize that the loop function repeats over and over right? And even if that doesn't help there are while loops and for loops to do the same thing over and over. So what's the point of testing for the same thing over and over in nested ifs like that? I can't imagine how that is supposed to work. I think you need to rethink your logic.

Maybe if you could explain a little better what you want the code to do.

Perhaps take the reading from millis every time you sense a breath and on each pass of the loop function, compare to the current value from millis and see if it has been more than 20 seconds.

This will …
read a new analog value
Test the analog value
Set set Digital I/O Pin as needed
wait 5 seconds
repeat forever …

void loop ()
{

    < read analog value here >

    if (analogValue > threshold)
    {
        digitalWrite(ledPin, HIGH);
    }
    else
    {
        digitalWrite(ledPin, LOW);
    }
 
    delay(5000);
}

So the way I see the code working is a graph that peaks during chest expansion and drops during exhaling and a thresh hold setting that when the voltage drops below that point a timer starts and resets if the voltage goes above the peak. If the voltage doesn't go above the thresh hold setting the timer waits 20 seconds and then an alarm sounds. I really appreciate any guidance that can be offered. Thanks!

Don't think in terms of waiting 20 seconds. Think in terms of seeing if it has been 20 seconds.

pseudocode:

unsigned long lastBreath;

void loop(){

     if(breath detected){
          lastBreath = millis();
          // you may also want to kill any alarms here
     }

     if(millis() - lastBreath >= 20000){
         the person isn't breathing
         sound an alarm
     }
}

mrsummitville:
This will …
read a new analog value
Test the analog value
Set set Digital I/O Pin as needed
wait 5 seconds
repeat forever …

void loop ()

{

< read analog value here >

if (analogValue > threshold)
   {
       digitalWrite(ledPin, HIGH);
   }
   else
   {
       digitalWrite(ledPin, LOW);
   }

delay(5000);
}

So now, all you need to do is …
COUNT consecutive NON-BREATHING
something like this …

void loop ()
{
    int AlarmCounter = 0;

    < read analog value here >

    // If Breath is Detected ?
    if (analogValue > threshold)
    {
        // LED shows Breath
        digitalWrite(ledPin, HIGH);

       // Reset the AlarmCounter
       AlarmCounter = 0;
    }
    else
    {
        // LED Shows No breath
        digitalWrite(ledPin, LOW);

        // Keep track of consecutive non-breathing 
        AlarmCounter++;
    }
  
   // Check every 1/10 second
    delay(100);

   // If NO BREATH for 20 consecutive seconds then 
   If ( AlarmCounter > 200 )
   {
         // Sound The Alarm
        digitalWrite(AlarmPin, High);

       // Wait for Human to Press the RESET Button
       While ( DigitalRead( ResetButtonPin ) == HIGH ) ;
  
       // Reset the Alarm Counter     
       AlarmCounter = 0;
   }

}