Heartbeat Sensor Code

/* ProtoSnap LilyPad Development Platform Example Code
by: Pete Lewis
SparkFun Electronics
date: 8/24/11
license: Creative Commons Attribution-Share-alike v3.0 CC BY-SA 3.0
http://creativecommons.org/licenses/by-sa/3.0/

This is example code for the Protosnap LilyPad Development Platform
It’ll do a quick startup sequence, flashing all LEDs, then it’ll go int the loop.
In the loop it’ll check for button or switch presses, and buzz or vibrate
depending on if they’re engaged.
If neither switch is on, it’ll check the light sensor and temperature sensor,
and light up some LEDs if the proper values are met. Hold your finger over the
light sensor to turn the white LEDs on. Try blowing on the temperature sensor to
turn the red LED on.
Finally, the status of each input is streamed over the serial line at 9600 bps.
To see what the values are, open up the serial monitor (the top right icon)
and make sure the baud is set to 9600.
*/

/* Pin Definitions */
// LEDs: white LEDs are connected to 5, 6, A2, A3, A4
int ledPins[] = {5, 6, A2, A4, A3, 9, 10, 11};
int switchPin = 2;
int buttonPin = A5;
int vibePin = 3;
int buzzerP = 7;
int buzzerN = 12;
int lightSensorPin = A6;
int tempSensorPin = A1;

//pulse sensor pin******************************************************************************
int PulseSensorPurplePin = A5;
int Signal; // holds the incoming raw data. Signal value can range from 0-1024
int Threshold = 550; // Determine which Signal to “count as a beat”, and which to ingore.
int tempSample = 0;
int tempSampleCount = 0;
//***********************************************************************************************
/* Global variables */
int light = 100;

void setup()
{
//STARTUP LED SEQUENCE
for (int i = 0; i < 5; i++) // This for loop will run 5 times
{ // blink the white LEDs (first 5 LEDs in ledPins array)
pinMode(ledPins[i], OUTPUT); // set pin as output
digitalWrite(ledPins[i], HIGH); // turn LED on
delay(250); // wait for a quarter second
digitalWrite(ledPins[i], LOW); // turn LED off
}
for (int i = 5; i < 8; i++) // This for loop will run 3 times
{ // blink the RGB LEDs (last 3 LEDs in ledPins array)
pinMode(ledPins[i], OUTPUT); // set the pin as an output
digitalWrite(ledPins[i], LOW); // turn RGB LED on
delay(250); // wait a quarter second
digitalWrite(ledPins[i], HIGH); // turn the RGB LED off
// Note that a HIGH turns the RGB LED off, LOW is on
// that’s backwards from the white LEDs
}
////////////////////////////////////////////

//Switch
pinMode(switchPin, INPUT); // set the switch as an input
digitalWrite(switchPin, HIGH); // enable the pull-up resistor

//Button
pinMode(buttonPin, INPUT); // set the pin as an input
digitalWrite(buttonPin, HIGH); // enable the pull-up resistor

////////////////////////////////////////////////////////////////
pinMode(PulseSensorPurplePin, INPUT); /////////////////Declaring pulse sensor pin as input

//****************************************************

//VIBE
pinMode(vibePin, OUTPUT); // set the pin as an output

//Buzzer
pinMode(buzzerP, OUTPUT); // set both buzzer pins as outputs
pinMode(buzzerN, OUTPUT);
digitalWrite(buzzerP, LOW);
digitalWrite(buzzerN, LOW);

Serial.begin(9600); // We’ll output some information over serial
}

void loop()
{

if (digitalRead(switchPin) == 0)
{ // If the switch is on, vibrate
Signal = analogRead(PulseSensorPurplePin); // Read the PulseSensor’s value.
// Assign this value to the “Signal” variable.
Serial.println(“Heartbeat: “);
Serial.println(Signal); // Send the Signal value to Serial Plotter.

//Raw values
float voltage= Signal * (5.0 / 1023.0);
Serial.println(“Raw Value: “);
Serial.println(voltage);
}

if (Signal > Threshold) { // If the signal is above “550”, then “turn-on” Arduino’s on-Board LED.
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(A4, HIGH);
digitalWrite(A3, HIGH);

//digitalWrite(buzzerP, HIGH);
// digitalWrite(buzzerN, LOW);
//digitalWrite(vibePin, HIGH);

delay(10);

//digitalWrite(buzzerP, LOW);
//digitalWrite(buzzerN, HIGH);
//digitalWrite(vibePin, LOW);

} else {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
//digitalWrite(ledPinCircle, LOW); // Else, the sigal must be below “550”, so “turn-off” this LED.
// digitalWrite(buzzerP, LOW);
// digitalWrite(buzzerN, LOW);
}
delay(10);
}