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);
}
}
}
}
}
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!
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;
}
}