RTC-DHT22 DISPLAY ON MATRIX

DHT 22 RTC matrix eeprom

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/DHT22_RTC_MATRIX.zip

//we can send message to display via bluetooth from serial monitor

#include <MaxMatrix.h>
#include <avr/pgmspace.h>
#include <EEPROM.h>

//===============================
const float fVerNum = 0.03;
#include <Wire.h>
#include “RTClib.h”
RTC_DS1307 RTC;
#include <stdlib.h>
#include “DHT.h” //include the temp sensor library
#define DHTPIN 7 // what pin we’re connected to
#define DHTTYPE DHT22 // DHT 22 temp&humid sensor
DHT dht(DHTPIN, DHTTYPE);
//=====================================
const PROGMEM prog_uchar CH[] = {
3, 8, B00000000, B00000000, B00000000, B00000000, B00000000, // space
1, 8, B01011111, B00000000, B00000000, B00000000, B00000000, // !
3, 8, B00000011, B00000000, B00000011, B00000000, B00000000, // ”
5, 8, B00010100, B00111110, B00010100, B00111110, B00010100, // #
4, 8, B00100100, B01101010, B00101011, B00010010, B00000000, // $
5, 8, B01100011, B00010011, B00001000, B01100100, B01100011, // %
5, 8, B00110110, B01001001, B01010110, B00100000, B01010000, // &
1, 8, B00000011, B00000000, B00000000, B00000000, B00000000, // ‘
3, 8, B00011100, B00100010, B01000001, B00000000, B00000000, // (
3, 8, B01000001, B00100010, B00011100, B00000000, B00000000, // )
5, 8, B00101000, B00011000, B00001110, B00011000, B00101000, // *
5, 8, B00001000, B00001000, B00111110, B00001000, B00001000, // +
2, 8, B10110000, B01110000, B00000000, B00000000, B00000000, // ,
4, 8, B00001000, B00001000, B00001000, B00001000, B00000000, // –
2, 8, B01100000, B01100000, B00000000, B00000000, B00000000, // .
4, 8, B01100000, B00011000, B00000110, B00000001, B00000000, // /
4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // 0
3, 8, B01000010, B01111111, B01000000, B00000000, B00000000, // 1
4, 8, B01100010, B01010001, B01001001, B01000110, B00000000, // 2
4, 8, B00100010, B01000001, B01001001, B00110110, B00000000, // 3
4, 8, B00011000, B00010100, B00010010, B01111111, B00000000, // 4
4, 8, B00100111, B01000101, B01000101, B00111001, B00000000, // 5
4, 8, B00111110, B01001001, B01001001, B00110000, B00000000, // 6
4, 8, B01100001, B00010001, B00001001, B00000111, B00000000, // 7
4, 8, B00110110, B01001001, B01001001, B00110110, B00000000, // 8
4, 8, B00000110, B01001001, B01001001, B00111110, B00000000, // 9
2, 8, B00100100, B00000000, B00000000, B00000000, B00000000, // :
2, 8, B10000000, B01010000, B00000000, B00000000, B00000000, // ;
3, 8, B00010000, B00101000, B01000100, B00000000, B00000000, // <
3, 8, B00010100, B00010100, B00010100, B00000000, B00000000, // =
3, 8, B01000100, B00101000, B00010000, B00000000, B00000000, // >
4, 8, B00000010, B01011001, B00001001, B00000110, B00000000, // ?
5, 8, B00111110, B01001001, B01010101, B01011101, B00001110, // @
4, 8, B01111110, B00010001, B00010001, B01111110, B00000000, // A
4, 8, B01111111, B01001001, B01001001, B00110110, B00000000, // B
4, 8, B00111110, B01000001, B01000001, B00100010, B00000000, // C
4, 8, B01111111, B01000001, B01000001, B00111110, B00000000, // D
4, 8, B01111111, B01001001, B01001001, B01000001, B00000000, // E
4, 8, B01111111, B00001001, B00001001, B00000001, B00000000, // F
4, 8, B00111110, B01000001, B01001001, B01111010, B00000000, // G
4, 8, B01111111, B00001000, B00001000, B01111111, B00000000, // H
3, 8, B01000001, B01111111, B01000001, B00000000, B00000000, // I
4, 8, B00110000, B01000000, B01000001, B00111111, B00000000, // J
4, 8, B01111111, B00001000, B00010100, B01100011, B00000000, // K
4, 8, B01111111, B01000000, B01000000, B01000000, B00000000, // L
5, 8, B01111111, B00000010, B00001100, B00000010, B01111111, // M
5, 8, B01111111, B00000100, B00001000, B00010000, B01111111, // N
4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // O
4, 8, B01111111, B00001001, B00001001, B00000110, B00000000, // P
4, 8, B00111110, B01000001, B01000001, B10111110, B00000000, // Q
4, 8, B01111111, B00001001, B00001001, B01110110, B00000000, // R
4, 8, B01000110, B01001001, B01001001, B00110010, B00000000, // S
5, 8, B00000001, B00000001, B01111111, B00000001, B00000001, // T
4, 8, B00111111, B01000000, B01000000, B00111111, B00000000, // U
5, 8, B00001111, B00110000, B01000000, B00110000, B00001111, // V
5, 8, B00111111, B01000000, B00111000, B01000000, B00111111, // W
5, 8, B01100011, B00010100, B00001000, B00010100, B01100011, // X
5, 8, B00000111, B00001000, B01110000, B00001000, B00000111, // Y
4, 8, B01100001, B01010001, B01001001, B01000111, B00000000, // Z
2, 8, B01111111, B01000001, B00000000, B00000000, B00000000, // [
4, 8, B00000001, B00000110, B00011000, B01100000, B00000000, // \ backslash
2, 8, B01000001, B01111111, B00000000, B00000000, B00000000, // ]
3, 8, B00000010, B00000001, B00000010, B00000000, B00000000, // hat
4, 8, B01000000, B01000000, B01000000, B01000000, B00000000, // _
2, 8, B00000001, B00000010, B00000000, B00000000, B00000000, // `
4, 8, B00100000, B01010100, B01010100, B01111000, B00000000, // a
4, 8, B01111111, B01000100, B01000100, B00111000, B00000000, // b
4, 8, B00111000, B01000100, B01000100, B00101000, B00000000, // c
4, 8, B00111000, B01000100, B01000100, B01111111, B00000000, // d
4, 8, B00111000, B01010100, B01010100, B00011000, B00000000, // e
3, 8, B00000100, B01111110, B00000101, B00000000, B00000000, // f
4, 8, B10011000, B10100100, B10100100, B01111000, B00000000, // g
4, 8, B01111111, B00000100, B00000100, B01111000, B00000000, // h
3, 8, B01000100, B01111101, B01000000, B00000000, B00000000, // i
4, 8, B01000000, B10000000, B10000100, B01111101, B00000000, // j
4, 8, B01111111, B00010000, B00101000, B01000100, B00000000, // k
3, 8, B01000001, B01111111, B01000000, B00000000, B00000000, // l
5, 8, B01111100, B00000100, B01111100, B00000100, B01111000, // m
4, 8, B01111100, B00000100, B00000100, B01111000, B00000000, // n
4, 8, B00111000, B01000100, B01000100, B00111000, B00000000, // o
4, 8, B11111100, B00100100, B00100100, B00011000, B00000000, // p
4, 8, B00011000, B00100100, B00100100, B11111100, B00000000, // q
4, 8, B01111100, B00001000, B00000100, B00000100, B00000000, // r
4, 8, B01001000, B01010100, B01010100, B00100100, B00000000, // s
3, 8, B00000100, B00111111, B01000100, B00000000, B00000000, // t
4, 8, B00111100, B01000000, B01000000, B01111100, B00000000, // u
5, 8, B00011100, B00100000, B01000000, B00100000, B00011100, // v
5, 8, B00111100, B01000000, B00111100, B01000000, B00111100, // w
5, 8, B01000100, B00101000, B00010000, B00101000, B01000100, // x
4, 8, B10011100, B10100000, B10100000, B01111100, B00000000, // y
3, 8, B01100100, B01010100, B01001100, B00000000, B00000000, // z
3, 8, B00001000, B00110110, B01000001, B00000000, B00000000, // {
1, 8, B01111111, B00000000, B00000000, B00000000, B00000000, // |
3, 8, B01000001, B00110110, B00001000, B00000000, B00000000, // }
4, 8, B00001000, B00000100, B00001000, B00000100, B00000000, // ~
};

int data = 12; // 8, DIN pin of MAX7219 module
int load = 10; // 9, CS pin of MAX7219 module
int clock = 11; // 10, CLK pin of MAX7219 module

int maxInUse = 9; //change this variable to set how many MAX7219’s you’ll use

MaxMatrix m(data, load, clock, maxInUse); // define module

byte buffer[10];

const int EEPROM_MIN_ADDR = 0;
const int EEPROM_MAX_ADDR = 511;
void eeprom_erase_all(byte b = 0xFF) {
int i;

for (i = EEPROM_MIN_ADDR; i <= EEPROM_MAX_ADDR; i++) {
EEPROM.write(i, b);
}
}

void eeprom_serial_dump_column() {

int i;
byte b;

char buf[10];

for (i = EEPROM_MIN_ADDR; i <= EEPROM_MAX_ADDR; i++) {
b = EEPROM.read(i);
sprintf(buf, “%03X: %02X”, i, b);
Serial.println(buf);
}
}

void eeprom_serial_dump_table(int bytesPerRow = 16) {

int i;
int j;
byte b;
char buf[10];

j = 0;

for (i = EEPROM_MIN_ADDR; i <= EEPROM_MAX_ADDR; i++) {

if (j == 0) {
sprintf(buf, “%03X: “, i);
Serial.print(buf);
}

b = EEPROM.read(i);

sprintf(buf, “%02X “, b);

j++;

if (j == bytesPerRow) {
j = 0;
Serial.println(buf);
}

else {
Serial.print(buf);
}
}
}

boolean eeprom_is_addr_ok(int addr) {
return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
}

boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {

int i;

if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
return false;
}

for (i = 0; i < numBytes; i++) {
EEPROM.write(startAddr + i, array[i]);
}

return true;
}

boolean eeprom_read_bytes(int startAddr, byte array[], int numBytes) {
int i;

if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
return false;
}

for (i = 0; i < numBytes; i++) {
array[i] = EEPROM.read(startAddr + i);
}

return true;
}

boolean eeprom_write_int(int addr, int value) {
byte *ptr;

ptr = (byte*)&value;
return eeprom_write_bytes(addr, ptr, sizeof(value));
}

boolean eeprom_read_int(int addr, int* value) {
return eeprom_read_bytes(addr, (byte*)value, sizeof(int));
}

boolean eeprom_write_string(int addr, const char* string) {

int numBytes;

numBytes = strlen(string) + 1;

return eeprom_write_bytes(addr, (const byte*)string, numBytes);
}

boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
byte ch;
int bytesRead;
if (!eeprom_is_addr_ok(addr)) {
return false;
}
if (bufSize == 0) {
return false;
}
if (bufSize == 1) {
buffer[0] = 0;
return true;
}
bytesRead = 0;
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
}
if ((ch != 0x00) && (bytesRead >= 1)) {
buffer[bytesRead – 1] = 0;
}

return true;
}

int start_time;
int stop_time;

void start_timing() {
start_time = millis();
}

void print_elapsed() {
stop_time = millis();
Serial.print(“Time elapsed (ms): “);
Serial.println(stop_time – start_time);
}
//======================================
bool write_StringEE(int Addr, String input)
{
char cbuff[input.length() + 1];
input.toCharArray(cbuff, input.length() + 1);
return eeprom_write_string(Addr, cbuff);
}

String read_StringEE(int Addr, int length)
{

char cbuff[length+1];
eeprom_read_string(Addr, cbuff, length+1);

String stemp(cbuff);
return stemp;

}

const int BUFSIZE = 50;
char buf[BUFSIZE];

///===================================================

void setup(){
m.init(); // module initialize
m.setIntensity(0); // dot matix intensity 0-15
Serial.begin(9600); // serial communication initialize
m.setIntensity(1); // dot matix intensity 0-15
dht.begin();
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println(“RTC is NOT running!”);
RTC.adjust(DateTime(__DATE__, __TIME__));
}

//====================================================
}

void loop(){
String text;
if (Serial.available()) {

for (int i = 0 ; i < 1000 ; i++) {
EEPROM.write(i, 0);
}
text = Serial.readString();
write_StringEE(0, text);//Write string starting at address 0

}
else{

int i;

for (i = 0; i <= 1000; i++) {
byte c = EEPROM.read(i);
printCharWithShift(c, 100);
}
printStringWithShift(“$$ “, 100);
printStringWithShift(“TODAY : “, 100);
waktu();
}

}

void printCharWithShift(char c, int shift_speed){
if (c < 32) return;
c -= 32;
memcpy_P(buffer, CH + 7*c, 7);
m.writeSprite(maxInUse*8, 0, buffer);
m.setColumn(maxInUse*8 + buffer[0], 0);

for (int i=0; i<buffer[0]+1; i++)
{
delay(shift_speed);
m.shiftLeft(false, false);
}
}

void printStringWithShift(char* s, int shift_speed){
while (*s != 0){
printCharWithShift(*s, shift_speed);
s++;
}
}

void printString(char* s)
{
int col = 0;
while (*s != 0)
{
if (*s < 32) continue;
char c = *s – 32;
memcpy_P(buffer, CH + 7*c, 7);
m.writeSprite(col, 0, buffer);
m.setColumn(col + buffer[0], 0);
col += buffer[0] + 1;
s++;
}
}

void waktu(){

float t = dht.readTemperature();
float h = dht.readHumidity();
char temp[4];
char hum[4];
itoa(t,temp,10); //convert int to char!!!!
itoa(h,hum,10); //convert int to char!!!!
DateTime now = RTC.now();

int mt = (now.month());
int dy = (now.day());
int y = (now.year());
int jm = (now.hour());
int men = (now.minute());
int dt = (now.second());
char tahun[5];
char bulan[4];
char tanggal[4];
char jam[4];
char menit[4];
char detik[4];

itoa(mt,bulan,10); //convert int to char!!!!
itoa(dy,tanggal,10); //convert int to char!!!!
itoa(y,tahun,10); //convert int to char!!!!
itoa(jm,jam,10); //convert int to char!!!!
itoa(men,menit,10); //convert int to char!!!!
itoa(dt,detik,10); //convert int to char!!!!

if(now.dayOfTheWeek()==1){
printStringWithShift(“MONDAY”, 100);
}
if(now.dayOfTheWeek()==2){
printStringWithShift(“TUESDAY”, 100);
}
if(now.dayOfTheWeek()==3){
printStringWithShift(“WEDNESDAY”, 100);
}
if(now.dayOfTheWeek()==4){
printStringWithShift(“THURSDAY”, 100);
}
if(now.dayOfTheWeek()==5){
printStringWithShift(“FRIDAY”, 100);
}
if(now.dayOfTheWeek()==6){
printStringWithShift(“SATURDAY”, 100);
}
if(now.dayOfTheWeek()==0){
printStringWithShift(“SUNDAY”, 100);
}
printStringWithShift(” “, 100);
printStringWithShift(tanggal, 100);
printStringWithShift(“/”, 100);
printStringWithShift(bulan, 100);
printStringWithShift(“/”, 100);
printStringWithShift(tahun, 100);
printStringWithShift(” “, 100);
printStringWithShift(jam, 100);
printStringWithShift(“:”, 100);
printStringWithShift(menit, 100);
printStringWithShift(“:”, 100);
printStringWithShift(detik, 100);
printStringWithShift(” “, 100);
printStringWithShift(“temp: “, 100);
printStringWithShift(temp, 100);
printStringWithShift(” C “, 100);
printStringWithShift(“humidity: “, 100);
printStringWithShift(hum, 100);
printStringWithShift(” % “, 100);
printStringWithShift(” “, 100);

m.shiftLeft(false, true);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s