-
-
Save ariejan/fb7a7802f326f4e4de7c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <LiquidCrystal.h> | |
| #include <Wire.h> | |
| #include <Stdio.h> | |
| #define aref_voltage 3.3 | |
| byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; | |
| LiquidCrystal lcd(8, 7, 1, 2, 3, 4); // RS, E, D4, D5, D6, D7 | |
| const int contrastPin = 6; | |
| const int backlightPin = 5; | |
| // I2C for RTC | |
| int clockAddress = 0x68; | |
| int command = 0; | |
| bool timeIndicator = true; | |
| int timerCount = 0; | |
| const int timeDelta = 200; | |
| byte timeChar[8] = { | |
| B01110, | |
| B10101, | |
| B10101, | |
| B10101, | |
| B10111, | |
| B10001, | |
| B10001, | |
| B01110 | |
| }; | |
| byte tempChar[8] = { | |
| B00100, | |
| B01010, | |
| B01010, | |
| B01110, | |
| B01110, | |
| B11111, | |
| B11111, | |
| B01110 | |
| }; | |
| // TMP Sensor | |
| int tmpPin = 0; | |
| const int numReadings = 10; | |
| int readings[numReadings]; | |
| int index = 0; | |
| int total = 0; | |
| int average = 0; | |
| int tempReady = 0; | |
| // Convert dec. to binary coded decimal | |
| byte decToBcd(byte val) { | |
| return (val / 10 * 16) + (val %10); | |
| } | |
| byte bcdToDec(byte val) { | |
| return (val /16 * 10) + (val %16); | |
| } | |
| void setup() { | |
| // Set pins | |
| analogReference(EXTERNAL); | |
| pinMode(contrastPin, OUTPUT); | |
| pinMode(backlightPin, OUTPUT); | |
| // Initialize Wire | |
| Wire.begin(); | |
| lcd.createChar(0, timeChar); | |
| lcd.createChar(1, tempChar); | |
| lcd.begin(8, 2); | |
| initScreen(); | |
| // Clear temperature cache | |
| for(int i = 0; i < numReadings; i++) { | |
| readings[i]= 0; | |
| } | |
| lcd.clear(); | |
| } | |
| void initScreen() { | |
| lcd.clear(); | |
| lcd.setCursor(0,0); | |
| lcd.print(" =[ IN"); | |
| lcd.setCursor(0,1); | |
| lcd.print("IT ]= "); | |
| // 3-second display init | |
| int maxT = 3000; | |
| int delayT = 10; | |
| for (int i = 0; i < (maxT / delayT); i++) { | |
| float percentage = (float)(delayT * i) / (float)maxT; | |
| int contrast = 0x20; // - (0x30 * percentage); | |
| int backlight = (int)(196 * percentage); | |
| analogWrite(contrastPin, contrast); | |
| analogWrite(backlightPin, backlight); | |
| lcd.setCursor(7, 1); | |
| if ((i / 40) % 2 == 0) { | |
| lcd.print("."); | |
| } else { | |
| lcd.print(" "); | |
| } | |
| delay(delayT); | |
| } | |
| } | |
| void printTime() { | |
| Wire.beginTransmission(clockAddress); | |
| Wire.write(byte(0x00)); | |
| Wire.endTransmission(); | |
| Wire.requestFrom(clockAddress, 7); | |
| // A few of these need masks because certain bits are control bits | |
| second = bcdToDec(Wire.read() & 0x7f); | |
| minute = bcdToDec(Wire.read()); | |
| // Need to change this if 12 hour am/pm | |
| hour = bcdToDec(Wire.read() & 0x3f); | |
| dayOfWeek = bcdToDec(Wire.read()); | |
| dayOfMonth = bcdToDec(Wire.read()); | |
| month = bcdToDec(Wire.read()); | |
| year = bcdToDec(Wire.read()); | |
| char date[8]; | |
| if (timeIndicator) { | |
| sprintf(date, " %02d:%02d", hour, minute); | |
| } else { | |
| sprintf(date, " %02d %02d", hour, minute); | |
| } | |
| timeIndicator = !timeIndicator; | |
| lcd.setCursor(0,0); | |
| lcd.write((uint8_t)0); | |
| lcd.print(date); | |
| } | |
| void printTemp() { | |
| // Smoothing | |
| total -= readings[index]; | |
| readings[index] = analogRead(tmpPin); | |
| total += readings[index]; | |
| index++; | |
| if (index >= numReadings) { | |
| index = 0; | |
| } | |
| average = total / numReadings; | |
| char temp[4]; | |
| if (tempReady >= numReadings) { | |
| float voltage = average * aref_voltage; | |
| voltage /= 1024.0; | |
| float tempC = (voltage - 0.5) * 100; | |
| // sprintf does not support %f... | |
| int decimal = (tempC - (int)tempC) * 10; | |
| sprintf(temp, "%d.%d", (int)tempC, decimal); | |
| } else { | |
| tempReady += 1; | |
| sprintf(temp, " -- "); | |
| } | |
| lcd.setCursor(0,1); | |
| lcd.write(1); | |
| lcd.print(" "); | |
| lcd.print(temp); | |
| lcd.print((char)223); | |
| lcd.print("C"); | |
| } | |
| void loop() { | |
| // Only update time and temp every second. | |
| if (timerCount >= 1000) { | |
| printTime(); | |
| printTemp(); | |
| timerCount -= 1000; | |
| } | |
| timerCount += timeDelta; | |
| delay(timeDelta); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment