Skip to content

Instantly share code, notes, and snippets.

@cannikin
Created March 25, 2010 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cannikin/343903 to your computer and use it in GitHub Desktop.
Save cannikin/343903 to your computer and use it in GitHub Desktop.
// Read temperature/humidity from sensor and display on LCD screen
#include <SoftwareSerial.h>
#include <Sensirion.h>
#define lcdTxPin 2
#define sensorDataPin 3
#define sensorClockPin 4
float temperature;
float humidity;
float dewpoint;
Sensirion tempSensor = Sensirion(sensorDataPin, sensorClockPin);
SoftwareSerial LCD = SoftwareSerial(0, lcdTxPin);
void setup() {
pinMode(lcdTxPin, OUTPUT);
LCD.begin(9600);
Serial.begin(9600);
backlightOn();
clearLCD();
LCD.print(" Starting...");
}
void loop() {
delay(3000);
// get the temp/humidity
tempSensor.measure(&temperature, &humidity, &dewpoint);
goTo(0);
LCD.print(" Temp Humidity");
goTo(16);
LCD.print(" ");
serialPrintFloat(LCD, convertToFahrenheit(temperature));
LCD.print(" ");
serialPrintFloat(LCD, humidity);
LCD.print("%");
}
// Prints a floating point number to the screen
void serialPrintFloat(SoftwareSerial output, float f){
output.print((int)f);
output.print(".");
int decplace = (f - (int)f) * 10;
output.print(abs(decplace));
}
// Convert celsius to fahrenheit
float convertToFahrenheit(float c) {
return ((9.0 / 5.0) * c) + 32.0;
}
// Sets the cursor to the given position
// line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
void goTo(int position) {
if (position < 16) {
serCommand(); //command flag
LCD.print((position+128), BYTE);
} else if (position < 32) {
serCommand(); //command flag
LCD.print((position+48+128), BYTE);
} else {
goTo(0);
}
}
// Resets the display
void clearLCD() {
serCommand();
LCD.print(0x01, BYTE);
}
// Turns the backlight on
void backlightOn() {
serCommand();
LCD.print(157, BYTE);
}
// Turns the backlight off
void backlightOff() {
serCommand();
LCD.print(128, BYTE);
}
// Initiates a function command to the display
void serCommand() {
LCD.print(0xFE, BYTE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment