// See http://bildr.org/2011/02/mlx90614-arduino/ for i2c library and instructions // You must download the "twimaster.cpp" and "i2cmaster.h" files, and place them in a folder called "I2Cmaster". This must be placed in a folder called "libraries" which in turn should be placed in your Sketchbook folder (see Arduino's Preferences menu item to see where this is on your machine.). // Typically, once you install these files, you must relaunch Arduino. //This script is an altered version of the script found at http://publiclaboratory.org/notes/warren/12-12-2011/circuit-diagram-simple-thermal-flashlight //and is being used under the creative commons license. #include #include "Wire.h" //#include "BlinkM_funcs.h" const float lowReading = 60; const float highReading = 85; const unsigned char separatorCharacter = 255; void setup(){ pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); Serial.begin(9600); Serial.println("starting setup..."); i2c_init(); //Initialise the i2c bus PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups Serial.println("completed setup"); } float normf(float x, float low, float high) { float y = (x - low) * 255.f / (high - low); if(y > 255) { y = 255; } if(y < 0) { y = 0; } return y; } void loop(){ int dev = 0x5A<<1; int data_low = 0; int data_high = 0; int pec = 0; i2c_start_wait(dev+I2C_WRITE); i2c_write(0x07); // read i2c_rep_start(dev+I2C_READ); data_low = i2c_readAck(); //Read 1 byte and then send ack data_high = i2c_readAck(); //Read 1 byte and then send ack pec = i2c_readNak(); i2c_stop(); //This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614) double tempData = 0x0000; // zero out the data int frac; // data past the decimal point // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte. tempData = (double)(((data_high & 0x007F) << 8) + data_low); tempData = (tempData * tempFactor)-0.01; float celcius = tempData - 273.15; float fahrenheit = (celcius*1.8) + 32; Serial.println(fahrenheit); float state = normf(fahrenheit, lowReading, highReading); //Serial.write((unsigned int) state); //Serial.write(separatorCharacter); // BlinkM MaxM super-bright LED: // 165 is blue, 0 is red //BlinkM_fadeToHSB(blinkm_addr, map(state, 0, 255, 165, 0), 255, 255); Serial.println(state); // Regular ol' RGB LED: int hue = map(state,0,255,359,(160)); // not the whole color wheel setLedColorHSV(hue,1,1); //We are using Saturation and Value constant at 1 Serial.print(fahrenheit); Serial.print(" degrees F, hue: "); Serial.println(+hue); } //Convert a given HSV (Hue Saturation Value) to RGB(Red Green Blue) and set the led to the color // h is hue value, integer between 0 and 360 // s is saturation value, double between 0 and 1 // v is value, double between 0 and 1 //http://splinter.com.au/blog/?p=29 void setLedColorHSV(int h, double s, double v) { //this is the algorithm to convert from RGB to HSV double r=0; double g=0; double b=0; double hf=h/60.0; int i=(int)floor((h)/60.0); double f = h/60.0 - i; double pv = v * (1 - s); double qv = v * (1 - s*f); double tv = v * (1 - s * (1 - f)); Serial.println(i); switch (i) { case 0: //rojo r = v; g = qv; b = pv; break; case 1: // rojo dominate r = v; g = tv; b = pv; break; case 2: // rojo r =1-v*f; g =.1*v*f; b = 0 ; break; case 3: //azul new rojo dominate r = .4*(1-v*f); g = v; b = .4*v*f; break; case 4: r = 0; g = .7*v; b = tv; break; case 5: //rojo r = pv; g = .5*qv; b = v; break; } //set each component to a integer value between 0 and 255 int red=constrain((int)255*r,0,255); int green=constrain((int)255*g,0,255); int blue=constrain((int)255*b,0,255); setLedColor(red,green,blue); } //Sets the current color for the RGB LED void setLedColor(int red, int green, int blue) { //Note that we are reducing 1/4 the intensity for the green and blue components because // the red one is too dim on my LED. You may want to adjust that. analogWrite(9,255-red); //Red pin attached to 9 analogWrite(10,255-green); //Red pin attached to 9 analogWrite(11,255-blue); //Red pin attached to 9 Serial.println(red); Serial.println(green); Serial.println(blue); }