element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Hats Off Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Hats Off Design Challenge
  • More
  • Cancel
Hats Off Design Challenge
Blog Spy Hat progress 2
  • Blog
  • Forum
  • Documents
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: bowdends
  • Date Created: 31 Oct 2014 2:07 AM Date Created
  • Views 351 views
  • Likes 0 likes
  • Comments 0 comments
  • hats_off
  • adafruit_gemma
  • spy_hat
Related
Recommended

Spy Hat progress 2

bowdends
bowdends
31 Oct 2014

Details required if you want to build your own Spy Hat!

 

Parts list: Gemma, battery, HC-SR04 (or similar) ultrasound sensor, NeoPixel

 

image

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);
}

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube