Last active
December 23, 2016 12:20
-
-
Save koenhendriks/794accd29d501b6b780c4d00569ab279 to your computer and use it in GitHub Desktop.
Arduino file for reading temperature and displaying on LCD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
TempLcd.ino | |
2017 Copyright (c) Koen Hendriks. All right reserved. | |
Connect LCD: | |
VCC : 5v | |
GND : GND | |
SDA : A4 Arduino | |
SCL : A5 Arduino | |
Connect Temperature Sensor, | |
Flat side down, left to right: | |
(left) GND : GND | |
(middle) SCL : A0 Arduino | |
(right) VCC : 5v | |
Button : Digital 4 Arduino | |
*/ | |
#include <Wire.h> | |
#include "rgb_lcd.h" | |
rgb_lcd lcd; | |
const int colorR = 118; | |
const int colorG = 232; | |
const int colorB = 220; | |
int TsensorPin = A0; | |
int buttonPin = 4; | |
int ledPin = 13; | |
int loops = 0; | |
boolean screenOn = true; | |
double temperature = 0.0; | |
int totalTemperature = 0; | |
int avegareTemp = 0; | |
void setup() | |
{ | |
// set up the LCD's number of columns and rows | |
lcd.begin(16, 2); | |
// set up the LCD default text | |
lcd.print("Welkom bij Koen!"); | |
lcd.setCursor(0,1); | |
// Background color | |
lcd.setRGB(colorR, colorG, colorB); | |
// Set Pins | |
pinMode(TsensorPin, INPUT); | |
pinMode(ledPin, OUTPUT); | |
pinMode(buttonPin, INPUT); | |
} | |
void loop() | |
{ | |
loops++; | |
temperature = TMP36GT_AI_value_to_Celsius(analogRead(TsensorPin)); | |
totalTemperature += temperature; | |
avegareTemp = totalTemperature / loops; | |
//Start displaying on second row | |
lcd.setCursor(0,1); | |
// Cast to int for rounded number | |
int lastMeasure = (int) temperature; | |
lcd.print("T:"); | |
lcd.print(lastMeasure); | |
lcd.print("C"); | |
// Cast to int for rounded number | |
int temp = (int) avegareTemp; | |
// Set cursor to align left | |
lcd.setCursor(8,1); | |
lcd.print("Gem: "); | |
lcd.print(temp); | |
lcd.print("C"); | |
if(digitalRead(buttonPin)){ | |
onButtonPress(); | |
} | |
delay(1000); | |
} | |
void onButtonPress(){ | |
if(screenOn){ | |
lcd.noDisplay(); | |
lcd.setRGB(0,0,0); | |
screenOn = false; | |
}else{ | |
lcd.display(); | |
lcd.setRGB(colorR, colorG, colorB); | |
screenOn = true; | |
} | |
} | |
double TMP36GT_AI_value_to_Celsius(int AI_value) | |
{ | |
float voltage; | |
voltage = AI_value * (5000.0/1024); | |
// Temperature according to datasheet: 750 mV = 25 °C | |
return ((voltage -750) /10) +25; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment