/*
Michael Kontopoulos
2009
Sigh Collector - Sender Code
*/
#include <NewSoftSerial.h>
NewSoftSerial mySerial = NewSoftSerial(4, 5); //instead of 1,2 (rx,tx)
//PINS
int stretcher = 1;
int threshPin = 0;
int threshLed=3;
int val = 0; // variable to store the value coming from the sensor
int pot = 0;
int sighPin = 13;
//VARIABLES
int const numVals = 40;
int breathVals[numVals];
//int diffVals[numVals/4];
int total=0, diffTotal=0, avg=0, rate=0;
int state = 0;
int threshold=0;
int normBreath;
int prevMillis=0;
boolean possibleSigh;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(sighPin, OUTPUT);
pinMode(stretcher, INPUT);
possibleSigh = false;
}
void loop() {
serialHandler();
val = analogRead(stretcher);
threshold = analogRead(threshPin);
// Serial.println(val);
checkSigh(avg);
//Array of the last 30 breath values
for(int i=1; i<numVals; i++)
{
//Shift the values down
breathVals[i-1] = breathVals[i];
//Add the most recent value to the last element
breathVals[numVals-1] = val;
//Obtaining the sliding scale average
//First add up all the values in the array
total += abs(breathVals[i-1]);
//Average them
avg = total/numVals;
}
total=0;
diffTotal=0;
//PRINT TESTS
Serial.print("Average Value: ");
Serial.print(avg);
Serial.print("tThreshold: ");
Serial.println(threshold);
if((avg >= threshold) && (avg < 400))
{
// digitalWrite(threshLed, HIGH);
possibleSigh = true;
}
else {
//digitalWrite(threshLed, LOW);
}
}
long int prevMillz = 0;
void checkSigh(int average)
{
if(possibleSigh)
{ prevMillis = millis();
//when avg drops back down, register the sigh
if(average <= 15) {
mySerial.println(8, DEC);
Serial.println("sigh detected. mobile unit");
digitalWrite(sighPin, HIGH);
delay(1000);
digitalWrite(sighPin, LOW);
possibleSigh=false;
}
}
else
possibleSigh=false;
}
///////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
void serialHandler()
{
if (mySerial.available()) {
Serial.print((char)mySerial.read());
}
if (Serial.available()) {
mySerial.print((char)Serial.read());
}
delay(100);
}

Michael kontopoulo1s

  • 1.
    /* Michael Kontopoulos 2009 Sigh Collector- Sender Code */ #include <NewSoftSerial.h> NewSoftSerial mySerial = NewSoftSerial(4, 5); //instead of 1,2 (rx,tx) //PINS int stretcher = 1; int threshPin = 0; int threshLed=3; int val = 0; // variable to store the value coming from the sensor int pot = 0; int sighPin = 13; //VARIABLES int const numVals = 40; int breathVals[numVals]; //int diffVals[numVals/4]; int total=0, diffTotal=0, avg=0, rate=0; int state = 0; int threshold=0; int normBreath; int prevMillis=0; boolean possibleSigh; void setup() { Serial.begin(9600); mySerial.begin(9600); pinMode(sighPin, OUTPUT); pinMode(stretcher, INPUT); possibleSigh = false; } void loop() { serialHandler(); val = analogRead(stretcher); threshold = analogRead(threshPin); // Serial.println(val); checkSigh(avg); //Array of the last 30 breath values for(int i=1; i<numVals; i++) { //Shift the values down breathVals[i-1] = breathVals[i]; //Add the most recent value to the last element breathVals[numVals-1] = val; //Obtaining the sliding scale average //First add up all the values in the array total += abs(breathVals[i-1]); //Average them avg = total/numVals;
  • 2.
    } total=0; diffTotal=0; //PRINT TESTS Serial.print("Average Value:"); Serial.print(avg); Serial.print("tThreshold: "); Serial.println(threshold); if((avg >= threshold) && (avg < 400)) { // digitalWrite(threshLed, HIGH); possibleSigh = true; } else { //digitalWrite(threshLed, LOW); } } long int prevMillz = 0; void checkSigh(int average) { if(possibleSigh) { prevMillis = millis(); //when avg drops back down, register the sigh if(average <= 15) { mySerial.println(8, DEC); Serial.println("sigh detected. mobile unit"); digitalWrite(sighPin, HIGH); delay(1000); digitalWrite(sighPin, LOW); possibleSigh=false; } } else possibleSigh=false; } /////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////// void serialHandler() { if (mySerial.available()) { Serial.print((char)mySerial.read()); } if (Serial.available()) { mySerial.print((char)Serial.read()); } delay(100); }