img_20160924_191031img_20160924_194116
For my Temporal Machine, I created an introverted LED friend who pulses slowly in a blue-white glow unless someone comes too close!
The LED “breathes” by pulsing gently, but starts to breathe faster and faster as people approach closer. If someone approaches too close it starts flashing red while pulsing fast enough to be considered hyperventilation. But if you back away it relaxes and starts breathing easy again.

// Define constant values.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
// The wiring assignment.
#define TRIG_PIN 8
#define ECHO_PIN 7

// The rated distance limit of the sensor, in cm.
#define MAX_DISTANCE 450

// A typical speed of sound, specified in cm/sec.
#define SOUND_SPEED 34000

// Determine the maximum time to wait for an echo. The maximum rated distance is
// 4.5 meters; if no echo is received within the duration representing this
// round-trip distance, stop measuring. The timeout is specified in
// microseconds.
#define TIMEOUT (2 * MAX_DISTANCE * 1000000)/SOUND_SPEED

void setup()
{
// Initialize the serial UART at 9600 bits per second.
Serial.begin(9600);

// Initialize the trigger pin for output.
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);

// Initialize the echo pin for input.
pinMode(ECHO_PIN, INPUT);

strip.begin();
strip.show(); // Initialize all pixels to 'off'
}


// Ping function to run one measurement cycle using the sonar. Returns a ping
// travel duration in microseconds, or 0 if no echo was observed.

long ping_sonar(void)
{
// Generate a short trigger pulse.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Measure the pulse length
return pulseIn(ECHO_PIN, HIGH, TIMEOUT);
}


//Global Variables
uint8_t wait = 100;
uint8_t r = 0;
void loop()
{
// Read the distance as an uncalibrated timing value in microseconds.
long duration = ping_sonar();

// If valid, scale into real-world units.
if (duration > 0) {

// Convert to a distance. Note that the speed of sound is specified in
// cm/sec, so the duration is scaled from microsecondst o seconds. The
// factor of 2 accounts for the round-trip doubling the time.
float distance = (duration * 1e-6 * SOUND_SPEED) / 2;

Serial.print("Ping: ");
Serial.print(duration);
Serial.print(" usec Distance: ");
Serial.print(distance);
Serial.print(" cm");
Serial.print(" wait: ");
Serial.println(wait);

} else {
// if no pulse detected
Serial.println("No ping.");
}

// Allow a little extra time for the sonar to recover.
delay(30);
if (duration < 6000){
   wait = duration / 100;
   if (wait < 24)
      r = 150 - wait*5;
   else
      r = 0;
}
breathing(wait,r);
}

void breathing(uint8_t wait, uint8_t r) {
uint16_t i, j;
double x;

for(j=0; j<180; j+=2) {
    x = sin(j*PI/180);
    for(i=0; i<strip.numPixels(); i++) {
      //x = abs(sin(j+((i%2)*90)*PI/180));
      strip.setPixelColor(i, strip.Color(10+x*50+r,30+x*75,200- x*160 - r));

}
   strip.show();
   delay(wait);
}

}