RGB Matrix p4 64*32 DHT11 DS1307 Arduino Mega

RGB Matrix p4 64*32 DHT11 DS1307 Arduino Mega

Arduino tutorial: RGB Matrix p4 64*32 DHT11 DS1307 Arduino Mega  
VCC→5V
GND→GND
DIN→11
CS →10
CLK→13

Schema


Code

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - DS1307 library (MD_DS1307) found at https://github.com/MajicDesigns/DS1307
// - RGB matrix Panel Library: https://github.com/adafruit/RGB-matrix-Panel
// - Adafruit_GFX Library: https://github.com/adafruit/Adafruit-GFX-Library
// Find All "Great Projects" Videos : https://www.youtube.com/channel/UCCC8DuqicBtP3A_aC53HYDQ/videos

#include "DHT.h"
#define DHTPIN 2     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#include <FreeMonoBold12pt7b.h>
#include <kongtext4pt7b.h>
#include <MD_DS1307.h>
#include <Wire.h>
#define CLK 11 // USE THIS ON ARDUINO MEGA
#define OE   9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

DHT dht(DHTPIN, DHTTYPE);
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 64);
// Global variables
#define MAX_MESG  9
#define MAX_MES  20
#define MAX_ME  50
  float h = 0;
  float t = 0;
uint8_t r=7, g=0, b=0;
unsigned int NewRTCh = 24;
unsigned int NewRTCm = 60;
unsigned int NewRTCs = 10;
  static bool Mode = false; 
int readings[8]; 

char szTime[4];    // 00
char szDate[MAX_MES+1] = "";
char szMesg[MAX_MESG+1] = "";
int16_t   hue = 0;
char *mon2str(uint8_t mon, char *psz, uint8_t len)
{
  static const char str[][4] PROGMEM =
  {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };

  *psz = '\0';
  mon--;
  if (mon < 12)
  {
    strncpy_P(psz, str[mon], len);
    psz[len] = '\0';
  }

  return(psz);
}

char *dow2str(uint8_t code, char *psz, uint8_t len)
{
  static const char str[][10] PROGMEM =
  {
    "Dimanche", "Lundi", "Mardi", "Mercredi",
    "Jeudi", "Vendredi", "Samedi"
  };

  *psz = '\0';
  code--;
  if (code < 7)
  {
    strncpy_P(psz, str[code], len);
    psz[len] = '\0';
  }

  return(psz);
}

void getTemperature()
{
  h = dht.readHumidity();
  t = dht.readTemperature();
      r++;
      if(r == 8) {
        r = 0;
        g++;
        if(g == 8) {
          g = 0;
          b++;
        if(b == 8) {
          b = 0;
        }
        }
      }
}
void getRTCh(char *psz)
// Code for reading clock time
{
  RTC.readTime();
  sprintf(psz, "%02d", RTC.h);
  matrix.setCursor(0, 24); 
  matrix.setFont(&FreeMonoBold12pt7b);
  matrix.setTextColor(matrix.Color333(7, 0, 7));
  matrix.fillRect(0, 16, 24, 15, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
  matrix.setFont();
  NewRTCh=RTC.h;
}

void getRTCm(char *psz)
// Code for reading clock time
{
  RTC.readTime();
  sprintf(psz, "%02d", RTC.m);
  matrix.setCursor(26, 24); 
  matrix.setFont(&FreeMonoBold12pt7b);
  sprintf(psz, "%02d", RTC.m);
  matrix.setTextColor(matrix.Color333(7, 0, 7));
  matrix.fillRect(26, 16, 24, 15, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
  matrix.setFont();
  NewRTCm=RTC.m;
}
   
void getTime(char *psz, bool f = true)
// Code for reading clock time
{
  RTC.readTime();
  if (NewRTCs != RTC.s/10) 
  {
  matrix.setCursor(20, 16); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(r, g, b));
  matrix.fillRect(24, 20, 2, 6, matrix.Color333(0, 0, 0));
    matrix.print(f ? ':' : ' ');
  matrix.setCursor(53, 18); 
  matrix.setTextSize(1);
  matrix.fillRect(53, 18, 10, 6, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
  matrix.setTextColor(matrix.Color333(b, g, r));
    matrix.print(f ? ' ' : '*');
  matrix.setFont();
  matrix.setCursor(50, 24); 
  matrix.setTextSize(1);
  matrix.setTextColor(matrix.Color333(b, r, g));
  sprintf(psz, "%02d", RTC.s);
  matrix.fillRect(50, 25, 13, 6, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
    matrix.print(szTime);
  matrix.setFont();
      NewRTCs=RTC.s/10;
    if (Mode) 
   {
        getDate(szDate);
          dtostrf(h, 3, 1, szMesg);
          strcat(szMesg, "%");
  matrix.setCursor(0, 0);
  matrix.setTextColor(matrix.Color333(b, g, r));
  matrix.fillRect(0, 0, 64, 8, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
    matrix.print("RH:");
    matrix.print(szMesg);
  matrix.setFont();
   } else
   {
        getDay(szMesg);
        getTemperature();
          dtostrf(t, 3, 1, szMesg);
          strcat(szMesg, "$");
  matrix.setCursor(0, 0);
  matrix.setTextColor(matrix.Color333(r, g, b));
  matrix.fillRect(0, 0, 64, 8, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
    matrix.print("Tmp:");
    matrix.print(szMesg);
  matrix.setFont();
   }
    Mode = !Mode;
    }
    else
  {
  matrix.setCursor(20, 16); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(b, g, r));
  matrix.fillRect(24, 20, 2, 6, matrix.Color333(0, 0, 0));
    matrix.print(f ? ':' : ' ');
  matrix.setCursor(53, 18); 
  matrix.setTextSize(1);
  matrix.fillRect(53, 18, 10, 6, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
  matrix.setTextColor(matrix.Color333(b, r, g));
    matrix.print(f ? ' ' : '*');
  matrix.setFont();
  matrix.setCursor(50, 24); 
  matrix.setTextColor(matrix.Color333(r, g, b));
  sprintf(psz, "%02d", RTC.s);
  matrix.fillRect(57, 25, 6, 6, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
    matrix.print(szTime);
  matrix.setFont();
    }
}
  
void getDate(char *psz)
// Code for reading date
{
  RTC.readTime();
  char  szBuf[10];
  sprintf(psz, "%02d%s%04d", RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy);
  matrix.setCursor(0, 7);
  matrix.setFont(&kongtext4pt7b);
  matrix.fillRect(0, 7, 64, 8, matrix.Color333(0, 0, 0));
  uint8_t y = 0;
  for (y=0; y<10; y++) {
    matrix.setTextColor(Wheel(y));
    matrix.print(szDate[y]);
    }
  matrix.setFont();
}
void getDay(char *psz)
// Code for reading day date
{
  RTC.readTime();
  dow2str(RTC.dow, szMesg, MAX_MESG);
  matrix.setCursor(readings[RTC.dow], 7);
  matrix.fillRect(0, 7, 64, 8, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
  uint8_t w = 0;
  for (w=0; w<9; w++) {
    matrix.setTextColor(Whel(w));
    matrix.print(szMesg[w]);
  }
  matrix.setFont();
}

void setup() {
  readings[0] = 6;
  readings[1] = 6; 
  readings[2] = 16;
  readings[3] = 16;
  readings[4] = 6;
  readings[5] = 16;
  readings[6] = 6;
  readings[7] = 12;
  matrix.begin();
  matrix.setTextWrap(false);
  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);
  dht.begin();
    RTC.h = 5;
    RTC.m = 37;
    RTC.s = 0;
    RTC.dd = 12;
    RTC.mm = 3;
    RTC.yyyy = 2019;
    RTC.dow = 3;
    RTC.writeTime();
}

void loop() {
  
  static uint32_t lastime = 0; // millis() memory
  static bool flasher = false;  // seconds passing flasher
  static bool Mode = false;  // seconds passing flasher
  if (millis() - lastime >= 1000)
  
 {
    millis() == 0;
    lastime = millis();
    getTime(szTime, flasher);
    flasher = !flasher;
    if (NewRTCh != RTC.h) 
   {
        getRTCh(szTime);
   }
    if (NewRTCm != RTC.m) 
   {
        getRTCm(szTime);
   }
 }
 }

// Input a value 0 to 24 to get a color value.
// The colours are a transition r - g - b - back to r.
uint16_t Wheel(byte WheelPos) {
  if(WheelPos < 2) {
   return matrix.Color333(0, 7, 0);
  } else if(WheelPos < 5) {
   WheelPos -= 2;
   return matrix.Color333(7 , 0, 0);
  } else {
   WheelPos -= 5;
   return matrix.Color333(0, 7, 0);
  }
}

// Input a value 0 to 24 to get a color value.
// The colours are a transition r - g - b - back to r.
uint16_t Whel(byte WheelPos) {
  if(WheelPos < 1) {
   return matrix.Color333(7 , WheelPos, 0);
  } else if(WheelPos < 2) {
   WheelPos -= 1;
   return matrix.Color333(0, 7, WheelPos);
  } else if(WheelPos < 3) {
   WheelPos -= 2;
   return matrix.Color333(0, WheelPos, 7);
  } else if(WheelPos < 4) {
   WheelPos -= 3;
   return matrix.Color333(7,7, WheelPos);
  } else if(WheelPos < 5) {
   WheelPos -= 4;
   return matrix.Color333(7,0, WheelPos);
  } else if(WheelPos < 6) {
   WheelPos -= 5;
   return matrix.Color333(0, 7, WheelPos);
  } else if(WheelPos < 7) {
   WheelPos -= 6;
   return matrix.Color333(0, WheelPos, 7);
  } else if(WheelPos < 8) {
   WheelPos -= 7;
   return matrix.Color333(7,7, WheelPos);
  } else if(WheelPos < 9) {
   WheelPos -= 8;
   return matrix.Color333(7,0, WheelPos);
  } else {
   WheelPos -= 9;
   return matrix.Color333(WheelPos, 7, 7);
  }
}