Wednesday, December 7, 2016

Week 16 Final Project





int ledPin1=13;
int ledPin2=12;
int ledPin3=11;
int ledPin4=10;
int ledPin5=9;
int ledPin6=8;
int ledPin7=7;
int ledPin8=6;
int ledPin9=5;
int ledPin10=4;
int ledPin11=3;
int ledPin12=2;

int sensorPin=0;

float alpha=0.75;
int period=50;
float change=0.0;

void setup()
{
  // Built-in arduino board pin
  pinMode(ledPin1,OUTPUT);
  pinMode(ledPin2,OUTPUT);
  pinMode(ledPin3,OUTPUT);
  pinMode(ledPin4,OUTPUT);
  pinMode(ledPin5,OUTPUT);
  pinMode(ledPin6,OUTPUT);
  pinMode(ledPin7,OUTPUT);
  pinMode(ledPin8,OUTPUT);
  pinMode(ledPin9,OUTPUT);
  pinMode(ledPin10,OUTPUT);
  pinMode(ledPin11,OUTPUT);
  pinMode(ledPin12,OUTPUT);
  Serial.begin(9600);
  Serial.print("Heartrate detection.\n");
  delay(100);
}

float max =0.0;

void loop()
{
  static float oldValue=1009;
  static float oldChange=0.2;

  // This is generic code provided with the board.
  int rawValue=analogRead(sensorPin);
  float value= alpha*oldValue +(1-alpha)* rawValue;
  change=value-oldValue;

  // Display data on the LED via a blip:
  // Empirically, if we detect a peak as being X% from
  // absolute max, we find the pulse even when amplitude
  // varies on the low side.
  
  // Reset max every time we find a new peak
  if (change >= max) {
    max= change;
    Serial.println("  |");
    digitalWrite(ledPin1,1);
    digitalWrite(ledPin2,1);
    digitalWrite(ledPin3,1);
    digitalWrite(ledPin4,1);
    digitalWrite(ledPin5,1);
    digitalWrite(ledPin6,1);
    digitalWrite(ledPin7,1);
    digitalWrite(ledPin8,1);
    digitalWrite(ledPin9,1);
    digitalWrite(ledPin10,1);
    digitalWrite(ledPin11,1);
    digitalWrite(ledPin12,1);
  } else {
    Serial.println("|");
    digitalWrite(ledPin1,0);
    digitalWrite(ledPin2,0);
    digitalWrite(ledPin3,0);
    digitalWrite(ledPin4,0);
    digitalWrite(ledPin5,0);
    digitalWrite(ledPin6,0);
    digitalWrite(ledPin7,0);
    digitalWrite(ledPin8,0);
    digitalWrite(ledPin9,0);
    digitalWrite(ledPin10,0);
    digitalWrite(ledPin11,0);
    digitalWrite(ledPin12,0);
  }
  // Slowly decay max for when sensor is moved around
  // but decay must be slower than time needed to hit
  // next heartbeat peak.
  max = max * 0.98;
  
  // Display debug data on the console
//  Serial.print(value);
//  Serial.print(", ");
//  Serial.print(change);
//  Serial.print(", ");
//  Serial.println(max);

  oldValue=value;
  oldChange=change;
  delay(period);
}

No comments:

Post a Comment