element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
Wearable Technology
  • Technologies
  • More
Wearable Technology
Blog Christmas Hat: The Code
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Wearable Technology to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: brenyc13
  • Date Created: 29 Nov 2014 1:55 PM Date Created
  • Views 787 views
  • Likes 0 likes
  • Comments 1 comment
  • flora
  • programming
  • code
  • christmas_hat
  • adafruit
  • haturday
  • temperature_sensor
Related
Recommended

Christmas Hat: The Code

brenyc13
brenyc13
29 Nov 2014

A brief post about how I assembled the code from several sources.

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

The Idea

The basic behavior I want is for the hat to be red and green for temperatures above 60 degrees fahrenheit, and to be blue and white for temperatures below. The colors should intensify the more extreme the temperature.


The Code (so far)

I built up the basic code for the Christmas Hat by piecing it together from Becky Stern's Punk Collar, the Heat Map Gun, and the simple Temp Sensor. The breakthrough came when I figured out how to "map" the temperature onto the color. I created different responsive versions of red, blue, and green for both the warm and cold states.

 

#include <Adafruit_NeoPixel.h>


// change these to adjust the range of temperatures you want to measure 
// (these are in Farenheit, multiplied by 10 to account for a decimal point)
#define COLDTEMP 400
#define HOTTEMP  800


//The long Neopixel Strip is called "strip1"
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(60, 6, NEO_GRB + NEO_KHZ800);
//The two 5mm Neopixels and three 8mm Neopixels are called "strip2"
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(5, 9, NEO_RGB + NEO_KHZ800);


//TMP36 Pin Variables
int sensorPin = 10; //the analog pin the TMP36's data-out (middle) pin is connected to




/*
 * the setup() function runs once when you turn on the Flora
 * Initialize the serial connection with the computer
 * Initialize strip1 and strip2
 */
void setup()
{
  Serial.begin(9600);  //Start the serial connection with the computer
                      //to view the result open the serial monitor 
  pinMode(6, INPUT_PULLUP); //initializes strip1
  pinMode(9, INPUT_PULLUP); //initializes strip2
  strip1.begin();
  strip2.begin();
  strip1.show(); // Initializes strip1 to 'off'
  strip2.show(); // Initializes strip2 to 'off'
}


/* 
 *this next bit is the Flora getting the temp from the sensor
 * and also converting it into fahrenheit
 */
int get_tempF()  // gets the temp
{
 //getting the voltage reading from the temperature sensor
 int reading = analogRead(sensorPin);  

 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 3.3;
 voltage /= 1024.0; 

 // print out the voltage
 Serial.print(voltage); Serial.println(" volts");

 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                              //to degrees ((voltage - 500mV) times 100)
 //Serial.print(temperatureC); Serial.println(" degrees C");

 // now convert to Fahrenheit
 float tempF = (temperatureC * 9.0 / 5.0) + 32.0;
 Serial.print(tempF); Serial.println(" degrees F");

 return tempF * 10;
}




/* Here's the meat of the program -- the loop
*/
void loop()
{
    uint8_t red1, green1, blue1;
    uint8_t red2, green2, blue2;
  float temp = get_tempF();

  if (temp < COLDTEMP) {
    temp = COLDTEMP;
  }
  if (temp > HOTTEMP) {
    temp = HOTTEMP;
  }

  // map temperature to red/blue color
  // hotter temp -> more green
  green1 = map(temp, COLDTEMP, HOTTEMP, 0, 75);  //limiting maximum at 175 for both
  // hotter temp -> less blue
  blue1 = map(temp, COLDTEMP, HOTTEMP, 100, 0);
  //hotter temp -> more red
  red1 = map(temp, COLDTEMP, HOTTEMP, 0, 175);  
  
  green2 = map(temp, COLDTEMP, HOTTEMP, 0, 215);  //limiting maximum at 175 for both
  // hotter temp -> less blue
  blue2 = map(temp, COLDTEMP, HOTTEMP, 215, 0);
  //hotter temp -> more red
  red2 = map(temp, COLDTEMP, HOTTEMP, 0, 230); 

  colorWipe(strip1.Color(0, green1, blue1), 0);
  strip2.setPixelColor(0, 10, green2, blue2); // strip.setPixelColor(pixel number in strip, R,G,B)
  strip2.setPixelColor(1, 10, green2, blue2); // Fiber Shapes are green when warm, blue when cold
  strip2.setPixelColor(2, red2, 0, blue2); // Berries are red when warm, blue when cold
  strip2.setPixelColor(3, red2, 0, blue2);
  strip2.setPixelColor(4, red2, 0, blue2);
  
  strip1.show();
  strip2.show();
  
  delay(3000); // can adjust this for faster/slower updates


//build in something how it fades???
}
  
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip1.numPixels(); i++) {
      strip1.setPixelColor(i, c);
      strip1.show();
      delay(wait);
  }
}

 

The Results

It works! I'm going to keep tinkering with the colors and temperature threshold (the "berries" are still too red in the cold state) but the general behavior is there and now it's just a matter of refining. Huzzah.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 11 years ago +1
    Nice update Barbara, Yes, the first step is to just make things work. Then you start looking at how to make it better. DAB
  • DAB
    DAB over 11 years ago

    Nice update Barbara,

     

    Yes, the first step is to just make things work.

    Then you start looking at how to make it better.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
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