Skip to content

Instantly share code, notes, and snippets.

@ogrodnek
Created January 30, 2012 03:06
Show Gist options
  • Save ogrodnek/1702223 to your computer and use it in GitHub Desktop.
Save ogrodnek/1702223 to your computer and use it in GitHub Desktop.
Arduino ambient temperature display
/**
* Simple Ambient temperature display.
*
* Will display the temperature in both Fahrenheit and Celsius and
* adjust the backlight color depending on the temperature.
*
* If the temperature range is between 61F and 67F, the display will be
* green. Above and it will be red, below is blue.
*
* More information is available here:
* http://analogmachines.com/blog/2012/01/30/ambient-temperature-display/
*
* This is for use with the Adafruit RGB backlight positive LCD
* (http://www.adafruit.com/products/398) and uses example code from
* their tutorial for changing the backlight color (http://www.ladyada.net/learn/lcd/charlcd.html).
*/
#include <LiquidCrystal.h>
#include <Wire.h>
#define TEMP_PIN 0
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
#define numSamples 5
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// you can change the overall brightness by range 0 -> 255
int brightness = 255;
float samples[numSamples];
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
brightness = 255;
// init samples
for (int i=0; i< numSamples; i++) {
samples[i] = _getTempReading();
}
}
void loop() {
float temperatureC = getAvgTempReading();
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
lcd.setCursor(0,0);
lcd.print(temperatureF);
lcd.print(" F");
lcd.setCursor(0,1);
lcd.print(temperatureC);
lcd.print(" C");
if (temperatureF < 61) {
setBacklight(0, 0, 255);
} else if (temperatureF > 67) {
setBacklight(255, 0, 0);
} else {
setBacklight(0, 255, 0);
}
delay(1000);
}
float _getTempReading() {
int reading = analogRead(TEMP_PIN);
float voltage = (reading * 5.0) / 1024;
return (voltage - 0.5) * 100;
}
unsigned int counter = 0;
float getAvgTempReading() {
samples[counter++ % numSamples] = _getTempReading();
int i=0;
float sum = 0;
for (i=0; i< numSamples; i++) {
sum+=samples[i];
}
return sum/numSamples;
}
void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
// normalize the red LED - its brighter than the rest!
r = map(r, 0, 255, 0, 100);
g = map(g, 0, 255, 0, 150);
r = map(r, 0, 255, 0, brightness);
g = map(g, 0, 255, 0, brightness);
b = map(b, 0, 255, 0, brightness);
// common anode so invert!
r = map(r, 0, 255, 255, 0);
g = map(g, 0, 255, 255, 0);
b = map(b, 0, 255, 255, 0);
analogWrite(REDLITE, r);
analogWrite(GREENLITE, g);
analogWrite(BLUELITE, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment