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

 

 

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.