Latest code v1.4 can be found here http://code.google.com/p/arduino-fun/downloads/list
// Arduino - TMP36 and 7 segment LED v1.3
// By Chris Hawkins
// February 2010
// Temperature code from http://oomlout.com/TMP36/TMP36-Guide.pdf
//
// using bitRead help from http://arduinofun.com/blog/2009/12/06/connecting-a-7-segment-led-to-the-arduino-build-it/
//
// Changes from 1.2
// Push button added to pin 12
// Added conversion scales from Celsius and Fahrenheit
// streamlined splitting the two digits
// Code Start
// pins that 7 segment is connect to
int ledSegment[] = {
2,3,4,5,6,7,8,9};
// Analog pin that TMP36 is connect to
int tempMonitor =0;
//pin for Switching from C to F
int pinSwitch =12;
// Button state for temperature scales
boolean displayType = false;
// encode the on/off state of the LED segments for the characters
// '0' to '9' and 'DP' into the bits of the bytes
const byte numDef[11] = {
63, 6, 91, 79, 102, 109, 124, 7, 127, 103,128 };
void setup()
{
// Serial.begin (9600);
// set pinmode for switch
pinMode (pinSwitch,INPUT);
// Sets the pinMode for all LEDs
for (int x = 0; x<=7; x++)
{
pinMode (ledSegment[x],OUTPUT);
}
// enable on by one each LED to test them.
for (int x =0;x<=7;x++){
digitalWrite (ledSegment[x],HIGH);
delay (50);
}
// disable one by one to finish test of each LED
for (int x =0;x<=7;x++){
digitalWrite (ledSegment[x],LOW);
delay (50);
}
}
void loop(){
// Check if button is pressed
if (digitalRead(pinSwitch)) {
displayType = !displayType;
if (displayType) {
setSegments(113); // send byte for C to display
delay(1500);
}
else{
setSegments(57); // send byte to F to display
delay(1500);
}
}
float temperature = getVoltage(tempMonitor); //getting the voltage reading from the temperature sensor
temperature = (temperature - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
if (displayType){
temperature = ((temperature *1.8) +32);
}
// Print temperature to serial for use in processing
//Serial.println (temperature);
// Split two digit number to send to display
int digit1 = temperature / 10;
int digit2 = int(temperature) % 10;
// send ten's number to display
setSegments( numDef[digit1] );
// Delay to display the 10's digit of the temperature
delay (1000);
// send unit number to display and DP
setSegments( numDef[digit2] + numDef[10] );
// Delay to display the unit digit of the temperature
delay (2000);
}
void setSegments(byte segments)
{
// for each of the segments of the LED
for (int s = 0; s < 8; s++)
{
int bitVal = bitRead( segments, s ); // grab the bit
digitalWrite(s+2, bitVal); // set the segment
}
}
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
Breadboard view of how I configured the Arduino:
[caption id="attachment_82" align="alignnone" width="300" caption="Designed using the Fritzing program"][/caption]
No comments:
Post a Comment