/* Analog LED Thermometer, by Karl Laun, March 2012 Circuit Description *******74HC595 wiring (1/2)********* 595(1) drives LEDs 1-8 (lower bank) 595(2) drives LEDs 9-16 (upper bank) Connect pins 10 (MR-reset) to +5v Connect pins 13 (OE-output enable) to GND Connect pins 8 (GND) to GND Connect pins 16 (Vcc) to +5v Connect pin 9(1) to pin 14(2) (serial out to data) Conect data(14), latch(12) and clock(11) pins to digital outputs (see define pins) ********* TMP36 wiring (facing flat side) *********** Connect pin 1 to +5v Connect pin 2 to analog input (see define pins) Connect pin 3 to GND */ // declare variables -------------------------------------- int LEDs[10] = {0, 1, 3, 7, 15, 31, 63, 127, 255, 128}; // set number of LEDs to light up int tempRange[2] = {20, 95}; // set desired upper and lower Farenheit temps int cutoff[15]; // array to store the intermediate steps between LED activations int upperBank = 0; int lowerBank = 0; int samplePeriod = 1000; // set period for polling of temp sensor int testDelay = 50; // define pins -------------------------------------------- int dataPin = 8; // connect to pin 14(Ds) int latchPin = 9; // connect to pin 12(STcp) connect to both 595 chips int clockPin = 10; // connect to pin 11(SHcp) connect to both 595 chips int tempPin = A3; // connect to center pin on TMP36 void setup() { Serial.begin(9600); // define inputs/outputs pinMode(dataPin, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(tempPin, INPUT); // run a sequential LED test for (int n=1; n < 9; n++) // lower bank { lowerBank = LEDs[n]; sendTemp(); delay(testDelay); } for (int j=1; j < 9; j++) // lower + upper bank { lowerBank = LEDs[8]; upperBank = LEDs[j]; sendTemp(); delay(testDelay); } delay(1000); // keep all LEDs lit for a bit, then turn them all off upperBank = LEDs[0]; lowerBank = LEDs[0]; sendTemp(); delay(samplePeriod); // define the temperature cutoff increment points for the LEDs float incr =(((tempRange[1] - tempRange[0]) /15.0)); // 15 steps between 16 LEDs cutoff[0] = tempRange[0] + (incr /2.0); for (int i=1; i < 15; i++) { cutoff[i] = cutoff[0] + (i * incr); } } void loop() { int reading = analogRead(tempPin); // read temperature probe voltage float probeVolts = reading * 5.0; // adjust it to 5v / 1024 to get millivolts probeVolts /= 1024.0; float tempC = (probeVolts - 0.5) * 100 ; // convert voltage to degrees (subtract 500 mv offset) float tempF = (tempC * 9.0 / 5.0) + 32.0; // convert C to F Serial.print("Temp = "); // send temperature to serial Serial.println(tempF); // assign byte values to be sent to LEDs if(tempF > tempRange[1]) // Warn if out of range (HOT) { upperBank = LEDs[9]; lowerBank = LEDs[0]; sendTemp(); delay(testDelay); upperBank = LEDs[0]; } else if(tempF < tempRange[0]) // Warn if out of range (COLD) { upperBank = LEDs[0]; lowerBank = LEDs[1]; sendTemp(); delay(testDelay); lowerBank = LEDs[0]; } else if(tempF >= tempRange[0] && tempF < cutoff[0]) // Set 1-16 LEDs to light up depending on temp { upperBank = LEDs[0]; lowerBank = LEDs[1]; } else if(tempF >= cutoff[0] && tempF < cutoff[1]) { upperBank = LEDs[0]; lowerBank = LEDs[2]; } else if(tempF >= cutoff[1] && tempF < cutoff[2]) { upperBank = LEDs[0]; lowerBank = LEDs[3]; } else if(tempF >= cutoff[2] && tempF < cutoff[3]) { upperBank = LEDs[0]; lowerBank = LEDs[4]; } else if(tempF >= cutoff[3] && tempF < cutoff[4]) { upperBank = LEDs[0]; lowerBank = LEDs[5]; } else if(tempF >= cutoff[4] && tempF < cutoff[5]) { upperBank = LEDs[0]; lowerBank = LEDs[6]; } else if(tempF >= cutoff[5] && tempF < cutoff[6]) { upperBank = LEDs[0]; lowerBank = LEDs[7]; } else if(tempF >= cutoff[6] && tempF < cutoff[7]) { upperBank = LEDs[0]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[7] && tempF < cutoff[8]) { upperBank = LEDs[1]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[8] && tempF < cutoff[9]) { upperBank = LEDs[2]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[9] && tempF < cutoff[10]) { upperBank = LEDs[3]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[10] && tempF < cutoff[11]) { upperBank = LEDs[4]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[11] && tempF < cutoff[12]) { upperBank = LEDs[5]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[12] && tempF < cutoff[13]) { upperBank = LEDs[6]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[13] && tempF < cutoff[14]) { upperBank = LEDs[7]; lowerBank = LEDs[8]; } else if(tempF >= cutoff[14] && tempF <= tempRange[1]) { upperBank = LEDs[8]; lowerBank = LEDs[8]; } sendTemp(); // send to LEDs function delay(samplePeriod); // wait awhile for next update } // Send bytes to 74HC595 chips to light LEDs void sendTemp() { digitalWrite(latchPin, LOW); // latch LOW to send data shiftOut(dataPin, clockPin, MSBFIRST, upperBank); // send byte for LEDs 1-8 shiftOut(dataPin, clockPin, MSBFIRST, lowerBank); // send byte for LEDs 9-16(shift upper) digitalWrite(latchPin, HIGH); // latch HIGH to stop data transfer }