Close

Source Code

A project log for The Temperizer

Wearable biofeedback device to help people manage stress, panic, and anger!

dan-fruzzettiDan Fruzzetti 05/01/2015 at 19:190 Comments
/* Dan Fruzzetti 2014
 * Temperizer code sketch
 * Version 0.8
 */

// ----- GLOBAL -----

// skin sensor pin assignment
float sensorPin = 0;

// Accommodation of last few values for smoothing algorithm
float SV1 = 0;
float SV2 = 0;
float SV3 = 0;
float SV4 = 0;
float SV5 = 0;
float SV6 = 0;
float SV7 = 0;
float SV8 = 0;

// buzzer pin assignment
int buzz = 12;

float C = 3;
int debug = 0;

// SVriable to store the SVlue coming from the sensor
float voltage;

// Time SVriables
unsigned long time;

// Reassignable per hardware / sensor needs
float secForGSR;
float curMillisForGSR;
float preMillisForGSR;

// ----- END GLOBAL -----


// initial conditions and one-time setup
void setup() {

// Switch commented lines to toggle debugging output (1 = yes; 0 = no)
debug = 0;
// debug = 1;

// Prepare serial port output
Serial.begin(9600);

// Frequency of sensor data acquisition (in seconds)
secForGSR = 0.125;
curMillisForGSR = 0;
preMillisForGSR = -1;

}
void loop() {

// Uncomment for buzzer and light test at power-up
  /* digitalWrite(buzz, HIGH);
   * digitalWrite(3, HIGH);
   * delay(1000);
   * digitalWrite(3, LOW);
   * delay(1000);
   */

// define extrema
float maximum = 0;
float minimum = 1290;

/* Prior code versions used these
 * time = millis();
 * digitalWrite(12, HIGH);
 * SV1 = analogRead(A0);
 * curMillisForGSR = time / (secForGSR * 1000);
 * if(curMillisForGSR != preMillisForGSR) {
 * Read GSR sensor and send over Serial port
 */

// acquire SVlue # 1
SV1 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV1 == ");
  Serial.println(SV1);
  }

delay(SV2);

// acquire SVlue # 2
SV2 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV2 == "); Serial.print("    TEST SV2 - SV1 == "); Serial.print(SV2 - SV1); Serial.print("  And SV2 == ");
  Serial.println(SV2);
  }

if((SV2 - SV1) >= 1) {
  digitalWrite(3, HIGH); 
  digitalWrite(12, HIGH);
  }

 delay(SV2);
 digitalWrite(3, LOW);
 delay(SV3);  
 digitalWrite(12, LOW);

// acquire SVlue # 3
SV3 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV3 == ");
  Serial.println(SV3);
  }

if((SV3 - SV2) >= 1) {
  digitalWrite(3, HIGH);  
  digitalWrite(12, HIGH);
  }

 delay(SV3); 
 digitalWrite(3, LOW);
 delay(SV4);  
 digitalWrite(12, LOW);

// acquire SVlue # 4
 SV4 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV4 == ");
  Serial.println(SV4);
  }

if((SV4 - SV3) >= 1) {
  digitalWrite(3, HIGH); 
  digitalWrite(12, HIGH);
  }
 delay(SV4);
 digitalWrite(3, LOW);
 delay(SV5);  
 digitalWrite(12, LOW);

// acquire SVlue # 5
SV5 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV5 == ");
  Serial.println(SV5);
  }

if((SV5 - SV4) >= 1) {
  digitalWrite(3, HIGH); 
  digitalWrite(12, HIGH);
  }

 delay(SV4);
 digitalWrite(3, LOW);
 delay(SV5);  
 digitalWrite(12, LOW);

// acquire SVlue # 6
SV6 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV6 == ");
  Serial.println(SV6);
  }

if(((SV6 - SV5)) >= 1) {
  digitalWrite(3, HIGH); 
  digitalWrite(12, HIGH);
  }

 delay(SV5);
 digitalWrite(3, LOW);
 delay(SV6);  
 digitalWrite(12, LOW);

// acquire SVlue # 7
SV7 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV7 == ");
  Serial.println(SV7);
  }

if(((SV7 - SV6)) >= 1) {
  digitalWrite(3, HIGH); 
  digitalWrite(12, HIGH);
  }

 delay(SV6); 
 digitalWrite(3, LOW);
 digitalWrite(12, LOW);
 delay(SV7);

// acquire SVlue # 8
SV8 = analogRead(sensorPin);

// debugging block
if (debug == 1) {
  Serial.print("SV8 == ");
  Serial.println(SV8);
  }

if(((SV8 - SV7)) >= 1) {
  digitalWrite(3, HIGH); 
  digitalWrite(12, HIGH);
  }
 delay(SV6);
 digitalWrite(3, LOW);
 delay(SV7);
 digitalWrite(12, LOW);

// clean-up
 digitalWrite(3, LOW);

// preMillisForGSR = curMillisForGSR;
  /*Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
   print out the SVlue you read:*/
   
   /*if ((SV2-SV1) > 0){

     digitalWrite(buzz, HIGH);
     digitalWrite(3, HIGH);
   delay(1000);
     }
   
// SVlue comparators
 if(SV8 > maximum){
    maximum = SV8;
    }
  if(SV1 < minimum){
    minimum = SV1;
    }
  if(SV2 < minimum){
    minimum = SV2;
    }
  if(SV3 < minimum){
    minimum = SV3;
    }
  if(SV4 < minimum){
    minimum = SV4;
    }
  if(SV5 < minimum){
    minimum = SV5;
    }
  if(SV6 < minimum){
    minimum = SV6;
    }
  if(SV7 < minimum){
    minimum = SV7;
    }
  if(SV8 < minimum){
    minimum = SV8;
    }
   
  SV7 = SV8;
  float SVD78 = (SV8-SV7)/secForGSR;
  if(SV7 > maximum){
    maximum = SV7;
    }
    
  SV6 = SV7;
  float SVD67 = (SV7-SV6)/secForGSR;
  if(SV6 > maximum){
    maximum = SV6;
    }
    
  SV2 = SV3;
  float SVD23 = (SV3-SV2)/secForGSR;
  if(SV2 > maximum){
    maximum = SV2;
    }

  SV3 = SV4;
  float SVD34 = (SV4-SV3)/secForGSR;
  if(SV3 > maximum){
    maximum = SV3;
    }

  SV4 = SV5;
  float SVD45 = (SV5-SV4)/secForGSR;
  if(SV4 > maximum){
    maximum = SV4;
    }

  SV5 = SV6;
  float SVD56 = (SV6-SV5)/secForGSR;
  if(SV5 > maximum){
    maximum = SV5;
    }

  SV1 = SV2;
  float SVD12 = (SV2-SV1)/secForGSR;
  if(SV1 > maximum){
    maximum = SV1;
    }

// clean-up
  digitalWrite(3, LOW);
  delay(1000);

// recalibration - a weighted average in v0.8 code
  float voltage = ((SV1 + SV2 + SV3 + SV4 + SV5 + SV6 + SV7 + SV8 + (3*maximum) + (3*minimum)) / 14);

// debugging block
if (debug == 1) {
  Serial.println(voltage);
  }  
}

Version 0.8 source code posted here. This uses a 'dumb' weighted average process which is not cumulative between samplings. We plan to use a cumulative weighted average process which will aid in squelching false "trend positives" when worn all day. We are, however, happy with the smoothing this simple algorithm offers, which decreases detection of "instantaneous false positives".

Discussions