Details required if you want to build your own Spy Hat!
Parts list: Gemma, battery, HC-SR04 (or similar) ultrasound sensor, NeoPixel
And the code:
/* Spy Hat code AdaFruit Gemma Hats Off Challenge Oct 30, 2014 This code was written for the Spy Hat project in the Adafruit Gemma Hats Off Challenge. It reads distance from an HR-S04 ultrasound sensor, and reports the distance as a color on an AdaFruit NeoPixel (green = no measurement, yellow is the most distant measurement, and shades of orange gradually moving to red show closer measurements. D. Bowden */ #include <Adafruit_NeoPixel.h> #define ECHO_PIN 2 // Echo Pin on HR-S04 #define TRIG_PIN 0 // Trigger Pin on HR-S04 #define LEDPin 1 // NeoPixel communication line #define YELLOW 255 // Value of Green (in RGB) to make NeoPixel look yellow #define MAX_RANGE 200 // Maximum allowed sensor distance (in cm) #define MIN_RANGE 0 // Minimum allowed sensor distance (in cm) -- note sensor is not capable of reading 0 cm /* NeoPixel set up */ //Parameter 1 = number of pixels in strip //Parameter 2 = pin number (most are valid) //Parameter 3 = pixel type flags, add together as needed: //NEO_RGB Pixels are wired for RGB bitstream //NEO_GRB Pixels are wired for GRB bitstream //NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) //NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip) Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, LEDPin, NEO_GRB + NEO_KHZ800); //i.e.(1 NeoPixel, Pin 1, NEO_GRB + NEO_800KHz) long pulseLength_us; // pulseLength_us of pulse length (in microseconds) long distance_cm; // Calculated distance of reported object (in centimeters) /* Note than an int (16 bits) is large enough to hold the maximum color_value (51000 when MAX_RANGE = 200 but if MAX_RANGE is significantly larger (over 257) then a larger variable may be needed) */ int color_value; // Used to vary NeoPixel color from yellow (255) to red (0) void setup() { pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); //Set up NeoPixel strip.begin(); strip.show(); } void loop() { strip.setBrightness(25); /* Set brightness of NeoPixel */ /* The following TRIG_PIN/ECHO_PIN cycle is used to determine the distance of the nearest object by bouncing soundwaves off of it. */ digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); pulseLength_us = pulseIn(ECHO_PIN, HIGH); //Calculate the distance (in cm) based on the speed of sound. distance_cm = pulseLength_us/58.2; /* If sensor reported no reading, LED should be green */ if (distance_cm >= MAX_RANGE || distance_cm <= MIN_RANGE) { /* Send a negative number to computer and Turn LED Green to indicate "out of range" */ //Serial.println("-1"); strip.setPixelColor(0, 0,128,0); // Turn LED green } /* If a reading was reported, calculate the color of the LED. Closer distances are more red, further distances are more yellow. */ else { /* Vary color from yellow (255) to red (0) by distance. Smaller distance = more red */ /* Always multiply before dividing to preserve as much resolution as possible */ color_value = (YELLOW * distance_cm)/MAX_RANGE; strip.setPixelColor(0, 255,color_value,0); // Turn on LED yellow } strip.show(); //Delay 50ms before next reading. delay(50); }