#include #include #define DEBUG 1 #define I2C_ADDRESS 10 #define LCD_ADDRESS 0x2e #define RTC_ADDRESS 0x68 #define SHT_CLOCK_PIN 11 #define SHT_DATA_PIN 10 #define HOUR 0 #define MINUTE 1 #define SECOND 2 #define DAY_OF_WEEK 3 #define MONTH 4 #define DAY 5 #define YEAR 6 // SHT15 byte ioByte; byte ackBit; int temp; int humidity; byte const bin2bcd_data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99 }; byte const bcd2bin_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 0, 0, 0, 0, 0, 0, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 0, 0, 0, 0, 0, 0, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 0, 0, 0, 0, 0, 0, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0, 0, 0, 0, 0, 0, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 0, 0, 0, 0, 0, 0, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 0, 0, 0, 0, 0, 0, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 0, 0, 0, 0, 0, 0, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 }; byte timestamp[7] = {0,0,0,0,0,0,0}; char key; char lastkey = ' '; enum screen {status, config}; screen mode = status; void setup() { // Setup debug port Serial.begin(38400); // Turn on power led digitalWrite(13,HIGH); // Init I2C bus Wire.begin(); //byte datetime[7] = {19,43,0,0,9,16,7}; //setClock(datetime); initLcd(); initSHT(); } void loop() { key = readKeypad(); readClock(); if(key != 0) { lastkey = key; switch(key) { case 'D': Serial.println('D'); mode = config; break; default: mode = status; } } temp = getTemp(); humidity = getHumidity(); Serial.print("Humidity: "); Serial.println(humidity); Serial.print("Temp: "); Serial.println(temp); refreshScreen(); delay(100); } void refreshScreen() { if(mode == config) { configScreen(); mode = status; } else { drawStatusScreen(); } } void drawStatusScreen() { lcdHome(); char text[21]; snprintf(text, 21, "%02i:%02i:%02i %02i/%02i/%02i", timestamp[HOUR], timestamp[MINUTE], timestamp[SECOND], timestamp[MONTH], timestamp[DAY], timestamp[YEAR]); writeLcd(text); #ifdef DEBUG Serial.println(text); #endif snprintf(text, 21, "T: % 3u%c RH: % 3u%%", temp, 0xdf, humidity); writeLcd(text); snprintf(text, 21, "Last keypress: %c ", lastkey); writeLcd(text); snprintf(text, 21, "Mode: %d ", mode); writeLcd(text); } void configScreen() { byte loopctr = 0; char text[21]; while(true) { lcdHome(); snprintf(text, 21, "Config menu "); writeLcd(text); snprintf(text, 21, " "); writeLcd(text); snprintf(text, 21, " "); writeLcd(text); snprintf(text, 21, " "); writeLcd(text); delay(100); if(++loopctr > 10) break; } } // RTC code void setClock(byte datetime[7]) { Wire.beginTransmission(RTC_ADDRESS); Wire.send(0x00); Wire.send(bin2bcd_data[datetime[SECOND]]); //sec Wire.send(bin2bcd_data[datetime[MINUTE]]); //min Wire.send(bin2bcd_data[datetime[HOUR]]); //hour Wire.send(bin2bcd_data[datetime[DAY_OF_WEEK]]); //day Wire.send(bin2bcd_data[datetime[DAY]]); //date Wire.send(bin2bcd_data[datetime[MONTH]]); //month Wire.send(bin2bcd_data[datetime[YEAR]]); //year Wire.endTransmission(); } void readClock() { Wire.beginTransmission(RTC_ADDRESS); Wire.send(0x00); Wire.endTransmission(); Wire.requestFrom(RTC_ADDRESS, 7); timestamp[SECOND] = bcd2bin_data[Wire.receive()]; timestamp[MINUTE] = bcd2bin_data[Wire.receive()]; timestamp[HOUR] = bcd2bin_data[Wire.receive()]; timestamp[DAY_OF_WEEK] = bcd2bin_data[Wire.receive()]; timestamp[DAY] = bcd2bin_data[Wire.receive()]; timestamp[MONTH] = bcd2bin_data[Wire.receive()]; timestamp[YEAR] = bcd2bin_data[Wire.receive()]; } // LCD code void initLcd() { clearDisplay(); } void clearDisplay() { Wire.beginTransmission(LCD_ADDRESS); Wire.send(0xFE); Wire.send(0x58); Wire.endTransmission(); } void lcdHome() { Wire.beginTransmission(LCD_ADDRESS); Wire.send(0xFE); Wire.send(0x48); Wire.endTransmission(); } void writeLcd(char* input) { Wire.beginTransmission(LCD_ADDRESS); Wire.send(input); Wire.endTransmission(); } // Keypad related code char readKeypad() { byte key = readKeypadByte(); if(key == 'T') { return '1'; } else if(key == 'S') { return '2'; } else if(key == 'R') { return '3'; } else if(key == 'Q') { return 'A'; } else if(key == 'O') { return '4'; } else if(key == 'N') { return '5'; } else if(key == 'M') { return '6'; } else if(key == 'L') { return 'B'; } else if(key == 'J') { return '7'; } else if(key == 'I') { return '8'; } else if(key == 'H') { return '9'; } else if(key == 'G') { return 'C'; } else if(key == 'E') { return '*'; } else if(key == 'D') { return '0'; } else if(key == 'C') { return '#'; } else if(key == 'B') { return 'D'; } else { return 0; } } byte readKeypadByte() { byte retVal = 0x0; // Ask the keypad for button presses Wire.beginTransmission(LCD_ADDRESS); Wire.send(0xFE); Wire.send(0x26); Wire.endTransmission(); Wire.requestFrom(LCD_ADDRESS, 1); if(Wire.available()) { retVal = Wire.receive(); } Wire.beginTransmission(LCD_ADDRESS); Wire.send(0xFE); Wire.send(0x45); Wire.endTransmission(); return retVal; } int getTemp() { Serial.println("temp requested"); return (SHT_Measure(3) * 0.018) - 40; } int getHumidity() { Serial.println("humidity requested"); int temp = getTemp(); int humidity = SHT_Measure(5); return (-4 + (0.0405 * humidity) + (-0.0000028 * humidity * humidity)); } // SHT15 Code (Temp and Humidity Sensor) void initSHT() { pinMode(SHT_CLOCK_PIN, OUTPUT); digitalWrite(SHT_CLOCK_PIN, HIGH); pinMode(SHT_DATA_PIN, OUTPUT); SHT_Connection_Reset(); } void SHT_Write_Byte(void) { pinMode(SHT_DATA_PIN, OUTPUT); shiftOut(SHT_DATA_PIN, SHT_CLOCK_PIN, MSBFIRST, ioByte); pinMode(SHT_DATA_PIN, INPUT); digitalWrite(SHT_DATA_PIN, LOW); digitalWrite(SHT_CLOCK_PIN, LOW); digitalWrite(SHT_CLOCK_PIN, HIGH); ackBit = digitalRead(SHT_DATA_PIN); digitalWrite(SHT_CLOCK_PIN, LOW); } byte shiftIn() { byte cwt=0; uint8_t bitmask=128; while(bitmask) { digitalWrite(SHT_CLOCK_PIN, HIGH); cwt = cwt + bitmask * digitalRead(SHT_DATA_PIN); digitalWrite(SHT_CLOCK_PIN, LOW); bitmask=bitmask >> 1; } return(cwt); } void SHT_Read_Byte(void) { ioByte = shiftIn(); digitalWrite(SHT_DATA_PIN, ackBit); pinMode(SHT_DATA_PIN, OUTPUT); digitalWrite(SHT_CLOCK_PIN, HIGH); digitalWrite(SHT_CLOCK_PIN, LOW); pinMode(SHT_DATA_PIN, INPUT); digitalWrite(SHT_DATA_PIN, LOW); } void SHT_Connection_Reset(void) { shiftOut(SHT_DATA_PIN, SHT_CLOCK_PIN, LSBFIRST, 255); shiftOut(SHT_DATA_PIN, SHT_CLOCK_PIN, LSBFIRST, 255); } void SHT_Soft_Reset(void) { SHT_Connection_Reset(); ioByte = 30; ackBit = 1; SHT_Write_Byte(); delay(15); } void SHT_Wait(void) { delay(5); int dly = 0; while (dly < 600) { if (digitalRead(SHT_DATA_PIN) == 0) dly=2600; delay(1); dly++; } } void SHT_Start(void) { digitalWrite(SHT_DATA_PIN, HIGH); pinMode(SHT_DATA_PIN, OUTPUT); digitalWrite(SHT_CLOCK_PIN, HIGH); digitalWrite(SHT_DATA_PIN, LOW); digitalWrite(SHT_CLOCK_PIN, LOW); digitalWrite(SHT_CLOCK_PIN, HIGH); digitalWrite(SHT_DATA_PIN, HIGH); digitalWrite(SHT_CLOCK_PIN, LOW); } int SHT_Measure(int vSvc) { unsigned int retValue; SHT_Soft_Reset(); SHT_Start(); ioByte = vSvc; SHT_Write_Byte(); SHT_Wait(); ackBit = 0; SHT_Read_Byte(); int msby; msby = ioByte; ackBit = 1; SHT_Read_Byte(); retValue = msby; retValue = retValue * 0x100; retValue = retValue + ioByte; if (retValue <= 0) retValue = 1; return retValue; } /* int SHT_Get_Status(void) { SHT_Soft_Reset(); SHT_Start(); ioByte = 7; SHT_Write_Byte(); SHT_Wait(); ackBit = 1; SHT_Read_Byte(); return(ioByte); }*/ /* void SHT_Heater(void) { SHT_Soft_Reset(); SHT_Start(); ioByte = 6; SHT_Write_Byte(); ioByte = 4; SHT_Write_Byte(); ackBit = 1; SHT_Read_Byte(); delay(500); SHT_Soft_Reset(); SHT_Start(); ioByte = 6; SHT_Write_Byte(); ioByte = 0; SHT_Write_Byte(); ackBit = 1; SHT_Read_Byte(); } */