Skip to content

Instantly share code, notes, and snippets.

@Dascr32
Created January 20, 2016 21:18
Show Gist options
  • Save Dascr32/f19aa0de00e76685745f to your computer and use it in GitHub Desktop.
Save Dascr32/f19aa0de00e76685745f to your computer and use it in GitHub Desktop.
/*
Simple Arduino program that reads the temperature from a tmp36 sensor
and show it in a 16x2 LCD. Also a loading splash screen is displayed when
turned on (just for lols). It works perfectly in the arduino simulator
(123d.circuits.io), however due to limitations of the simulator, datetime
libraries cannot be used, so the date and time shown are not real, in a
future revision that functionality will be added.
NOTE: This program has not been tested in a real arduino. So be careful.
Copyright (C) 2016 Daniel Aguilar S.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <LiquidCrystal.h>
// LCD size
const int LCD_WIDTH = 16;
const int LCD_HEIGHT = 2;
// LCD rows
const int FIRST_ROW = 0;
const int SECND_ROW = 1;
// Pins where the lcd is connected too.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Temp sensor
const int SENSOR_PIN = A0;
const float ARDUINO_VOLTS = 5.0;
const int UPDATE_INTERVAL_MS = 500;
const int DEGREE_CHAR = 178;
// ============= MESSAGES =============
const char* WELCOME_MSG = "TEMP SENSOR V1.0";
// ============= ARDUINO FUNCTIONS =============
void setup () {
// Set lcd size
lcd.begin(LCD_WIDTH, LCD_HEIGHT);
// Show splash screen, not functional but it looks nice.
lcd.print(WELCOME_MSG);
// Simulate loading
int i;
for (i = 0; i < 100; i++) {
updateSplashScreen(i);
}
lcd.clear();
}
void loop () {
// Update dateTime first.
lcd.setCursor(0, SECND_ROW);
lcd.print(getDateTime());
lcd.setCursor(0, FIRST_ROW);
lcd.print("Temp: " + String(getSensorTemp()) + char(DEGREE_CHAR) + "C");
delay(UPDATE_INTERVAL_MS);
}
// ============= CUSTOM FUNCTIONS ==============
/**
* Return the temperature reading from the sensor in celsius.
* NOTE: Temps not 100% accurate. Use 3.3v for improved accuracy.
*/
float getSensorTemp() {
int pinReading = analogRead(SENSOR_PIN);
float voltage = (pinReading * ARDUINO_VOLTS) / 1024.0;
// Converting from 10 mv per degree with 500 mV offset
// to degrees ((voltage - 500mV) times 100) (thanks to adafruit)
float temp = (voltage - 0.5) * 100;
return temp;
}
/**
* Apparently time libs are not available in the simulator.
* FIXME: Add real dateTime.
*/
char* getDateTime () {
return "WED JAN 20 14:04";
}
void updateSplashScreen (int value) {
int convertedValue = value / 7;
if (convertedValue == 0) {
lcd.setCursor(0, SECND_ROW);
lcd.print("[");
lcd.setCursor(15, SECND_ROW);
lcd.print("]");
}
else {
lcd.setCursor(convertedValue, SECND_ROW);
lcd.print("=");
}
}
@Dascr32
Copy link
Author

Dascr32 commented Jan 20, 2016

Output looks like this:

Splash Screen

lcdaino2

Monitor

lcdaino

Enjoy! 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment