Last active
August 29, 2015 13:56
-
-
Save jkeefe/9180103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Heartbeat Hoodie | |
* | |
* LED code drawn from Arduino example 01 - Blinking LED | |
* | |
* Heartbeat code baed on Heart Rate Badge code by | |
* Becky Stern for Adafruit Industries ... | |
* http://learn.adafruit.com/heart-rate-badge/program-it | |
* ... who based hers on sample code from | |
* http://learn.parallax.com/KickStart/28048 | |
* | |
* Details on building the Heartbeat Hoodie at | |
* http://johnkeefe.net/making-a-heartbeat-hoodie | |
* | |
* by John Keefe | |
* john@johnkeefe.net | |
* | |
* February 2014 | |
* | |
*/ | |
// set up an array of the pins the LEDs use | |
int light[] = {A3, A4, A5, 5, 6, 9, 10, A2}; | |
// Set the heart monitor to run on pin 2 | |
const int HR_RX = 11; | |
int oldSample, sample; | |
boolean monitor_detected = false; | |
// this code runs once | |
void setup() { | |
for (int x = 0; x < 9; x++) { | |
// set each LED pin as an output | |
pinMode(light[x], OUTPUT); | |
} | |
// Set the heart monitor pin to input | |
pinMode (HR_RX, INPUT); | |
// light the bottom led just to show it's on | |
// during setup | |
digitalWrite(light[0], HIGH); | |
// wait 5 seconds to see if there's a heart signal | |
// loops 500 times, with a 10ms delay | |
for (int x = 0; x <500; x++) { | |
// if there's a heartbeat detected, set the | |
// monitor_detected variable to true and | |
// bail out of the loop | |
if (digitalRead(HR_RX)) { | |
monitor_detected = true; | |
break; | |
// otherwise set the variable to false | |
} else { | |
monitor_detected = false; | |
} | |
delay(10); | |
} | |
} | |
// this code repeats forever | |
void loop() { | |
// do this code if monitor_detected varialbe is true | |
if (monitor_detected) { | |
// Read the heard monitor. Will return a 1 if there's a beat | |
sample = digitalRead(HR_RX); | |
// If the sample exits and it wasn't 1 on the last loop, new heart beat detected | |
if (sample && (oldSample != sample)) { | |
// run the heartbeat function | |
heartBeat(); | |
} | |
oldSample = sample; //Store last signal received | |
} else { | |
// no monitor detected, so run the idle LED show | |
idleBounce(); | |
} | |
} | |
// this is the heart beat function | |
void heartBeat (){ | |
/* light the hoodie in this pattern, | |
radiating from the heart | |
pin, order, light position on hoodie and in array | |
A3 4 light 7 | |
A4 3 light 6 | |
A5 2 light 5 | |
5 1 light 4 | |
6 1 light 3 | |
7 2 light 2 | |
10 3 light 1 | |
A2 4 light 0 | |
*/ | |
int delay_time = 25; // sets zip speed of LEDs | |
digitalWrite(light[4], HIGH); | |
digitalWrite(light[3], HIGH); | |
delay(delay_time); | |
digitalWrite(light[5], HIGH); | |
digitalWrite(light[2], HIGH); | |
digitalWrite(light[4], LOW); | |
digitalWrite(light[3], LOW); | |
delay(delay_time); | |
digitalWrite(light[6], HIGH); | |
digitalWrite(light[1], HIGH); | |
digitalWrite(light[5], LOW); | |
digitalWrite(light[2], LOW); | |
delay(delay_time); | |
digitalWrite(light[7], HIGH); | |
digitalWrite(light[0], HIGH); | |
digitalWrite(light[6], LOW); | |
digitalWrite(light[1], LOW); | |
delay(delay_time); | |
digitalWrite(light[7], LOW); | |
digitalWrite(light[0], LOW); | |
} | |
// this gets called if there's no heart beat signal | |
// (so default if the monitor is not in use) | |
void idleBounce () { | |
// spring the LED's into action! | |
for (int y = 0; y < 8; y++) { | |
digitalWrite(light[y], HIGH); // turns LEDs on | |
delay(100); // waits for 100 milliseconds | |
} | |
// turn them all off in reverse order | |
for (int z = 0; z < 8; z++) { | |
digitalWrite(light[7-z], LOW); // turns LED off | |
delay(100); // waits for 1/10 second | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment