RGB Matrix 64*32 P4-256*128-2121-A2

RGB Matrix 64*32 P4-256*128-2121-A2


RGB Matrix p4 with arduino mega 64x32
DS1307 Real Time Clock
64x32 rgb led matrix arduino
64x32 rgb led matrix arduino code
matrice led rgb arduino

Schema

 


Code

RGB_Matrix_p4
Fonts
// testshapes demo for RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x64 RGB LED matrix.

// WILL NOT FIT on ARDUINO UNO -- requires a Mega, M0 or M4 board



#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#include <FreeMonoBoldOblique9pt7b.h>
#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

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
uint8_t r=7, g=0, b=0;
unsigned int NewRTCh = 24;
unsigned int NewRTCm = 60;
unsigned int NewRTCs = 10;
unsigned int NewRTCd = 8;

volatile boolean buttonA = false;
volatile boolean buttonB = false;
volatile boolean buttonC = false;
int StateOfbuttonA = 0;
int StateOfbuttonB = 0;  
int StateOfbuttonC = 0;        
int NewStateOfbuttonA = 0;        
int NewStateOfbuttonB = 0;        
int NewStateOfbuttonC = 0;
int Mode = 0;

char szTime[4];    // 00
char szDate[MAX_MES+1] = "";
char szDat[MAX_ME+1] = "";
char szMesg[MAX_MESG+1] = "";
int16_t    textX         = matrix.width(),
           textMin       = sizeof(szDat) * -4,
           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 =
  {
    " Sunday", " Monday", " Tuesday", "Wednesday",
    "Thursday", " Friday", "Saturday"
  };

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

  return(psz);
}

void getRTCh(char *psz)
// Code for reading clock time
{
  RTC.readTime();
  sprintf(psz, "%02d", RTC.h);
  matrix.setCursor(3, 6); 
  matrix.setFont(&FreeMonoBoldOblique9pt7b);
  matrix.setTextColor(matrix.Color333(7, 0, 7));
  matrix.fillRect(3, 1, 19, 12, 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(28, 6); 
  matrix.setFont(&FreeMonoBoldOblique9pt7b);
  matrix.setTextColor(matrix.Color333(7, 0, 7));
  matrix.fillRect(28, 1, 19, 12, 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, 0); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(r, g, b));
  matrix.fillRect(24, 4, 2, 6, matrix.Color333(0, 0, 0));
    matrix.print(f ? ':' : ' ');
  matrix.setCursor(52, 0); 
  matrix.setTextSize(1);
  matrix.fillRect(52, 0, 10, 6, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
  matrix.setTextColor(matrix.Color333(g, r, b));
    matrix.print(f ? ' ' : '*');
  matrix.setFont();
  matrix.setCursor(49, 6); 
  matrix.setTextSize(1);
  matrix.setTextColor(matrix.Color333(b, r, g));
  sprintf(psz, "%02d", RTC.s);
  matrix.fillRect(49, 7, 13, 6, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
    matrix.print(szTime);
  matrix.setFont();
      NewRTCs=RTC.s/10;
    }
    else
  {
  matrix.setCursor(20, 0); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(b, r, g));
  matrix.fillRect(24, 4, 2, 6, matrix.Color333(0, 0, 0));
    matrix.print(f ? ':' : ' ');
  matrix.setCursor(52, 0); 
  matrix.setTextSize(1);
  matrix.fillRect(52, 0, 10, 6, matrix.Color333(0, 0, 0));
  matrix.setFont(&kongtext4pt7b);
  matrix.setTextColor(matrix.Color333(g, r, b));
    matrix.print(f ? ' ' : '*');
  matrix.setFont();
  matrix.setCursor(49, 6); 
  matrix.setTextColor(matrix.Color333(r, g, b));
  sprintf(psz, "%02d", RTC.s);
  matrix.fillRect(56, 7, 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(1, 22);
  matrix.setFont(&kongtext4pt7b);
  matrix.fillRect(0, 22, 64, 10, 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(0, 14);
  matrix.setTextColor(matrix.Color333(0, 7, 0));
  matrix.fillRect(0, 14, 64, 9, 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();
  NewRTCd=RTC.dow;
}
void getDat(char *psz)
// Code for reading clock date
{
  char  szBuf[10];
  RTC.readTime();
  sprintf(psz, "%s %d %s %04d", dow2str(RTC.dow, szMesg, MAX_MESG), RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy);
  matrix.setTextColor(matrix.ColorHSV(hue, 255, 255, true));
  matrix.setCursor(textX, 22);
  matrix.setFont(&FreeMonoBold12pt7b);
  matrix.fillRect(0, 14, 64, 18, matrix.Color333(0, 0, 0));
    matrix.print(szDat);
  matrix.setFont();
  if((--textX) < textMin) textX = matrix.width();
  if(hue >= 1536) hue -= 1536;
  hue += 7;
  // Update display
  matrix.swapBuffers(false);
}

void setup() {

  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  matrix.begin();
  matrix.setTextWrap(false);
  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);
}

void loop() {
  
  static uint32_t lastime = 0; // millis() memory
  static bool flasher = 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);
   }
    if (NewRTCd != RTC.dow) 
   {
        Mode == 0;
        getDate(szDate);
        getDay(szMesg);
   }
 }

  NewStateOfbuttonA = digitalRead(3);
  NewStateOfbuttonB = digitalRead(4);
  NewStateOfbuttonC =   digitalRead(5);
  buttonAisPressed();
  buttonBisPressed();
  buttonCisPressed();
      if (buttonA) {
   if (Mode == 0 ) {
    buttonA = false;
    r++;
      if(r == 8) {
        r = 0;
        g++;
        if(g == 8) {
          g = 0;
          b++;
        if(b == 8) {
          b = 0;
        }
        }
      }
    }   
  else if (Mode == 1 ) {
    buttonA = false;
      Mode--;
    getDay(szMesg);
    getDate(szDate);
  } 
  else if (Mode == 2 ) {
    buttonA = false;
    RTC.h++;
    if (RTC.h >= 24 ) {
    RTC.h = 0;
  }
    RTC.writeTime();
    getRTCh(szTime);
  }
  else if (Mode == 3 ) {
    buttonA = false;
    RTC.m++;
    if (RTC.m >= 60 ) {
    RTC.m = 0;
  }
    RTC.writeTime();
    getRTCm(szTime);
  }
  else if (Mode == 4 ) {
    buttonA = false;
      RTC.s = 0;
    RTC.writeTime();
    getTime(szTime);
  }
  else if (Mode == 5 ) {
    buttonA = false;
    RTC.dow++;
    if (RTC.dow >= 8 ) {
    RTC.dow = 1;
  } 
    RTC.writeTime();
    getDay(szMesg);
  }
  else if (Mode == 6 ) {
    buttonA = false;
    RTC.dd++;
    if (RTC.dd >= 32 ) {
    RTC.dd = 1;
  }
    RTC.writeTime();
    getDate(szDate);
  }    
  else if (Mode == 7 ) {
    buttonA = false;
    RTC.mm++;
    if (RTC.mm >= 13 ) {
    RTC.mm = 1;
  }
    RTC.writeTime();
    getDate(szDate);
  }      
  else if (Mode == 8 ) {
    buttonA = false;
    RTC.yyyy++;
    if (RTC.yyyy >= 2036 ) {
    RTC.yyyy = 2010;
  }  
    RTC.writeTime();
    getDate(szDate);
       }
  }

   else if (buttonB) {
    buttonB = false;
      Mode++;
    if (Mode >= 9 ) {
      Mode = 0;
    getDay(szMesg);
    getDate(szDate);
   } 
   else if (Mode == 2 ) {
  matrix.drawRect(2, 0, 21, 14, matrix.Color333(7, 7, 0));
    getDay(szMesg);
    getDate(szDate);
   }
   else if (Mode == 3 ) {
  matrix.drawRect(2, 0, 21, 14, matrix.Color333(0, 0, 0));
  matrix.drawRect(27, 0, 21, 14, matrix.Color333(7, 7, 0));
   }
   else if (Mode == 4 ) {
  matrix.drawRect(27, 0, 21, 14, matrix.Color333(0, 0, 0));
  matrix.drawRect(48, 6, 15, 8, matrix.Color333(7, 7, 0));
   } 
   else if (Mode == 5 ) {
  matrix.drawRect(48, 6, 15, 8, matrix.Color333(0, 0, 0));
   } 
   else if (Mode == 6 ) {
    getDay(szMesg);
   } 
   else if (Mode == 7 ) {
    getDate(szDate);
   } 
   else if (Mode == 8 ) {
    getDate(szDate);
   } 
}


    
   else if (buttonC) {
   if (Mode == 0 ) {
    buttonC = false;
      Mode++;
    getDat(szDat);
     
    }   
  else if (Mode == 1 ) {
    buttonC = false;
      Mode--;
    getDay(szMesg);
    getDate(szDate);
  }  
  else if (Mode == 2 ) {
    buttonC = false;
    RTC.h--;
    if (RTC.h < 0 ) {
    RTC.h = 23;
  } 
    RTC.writeTime();
    getRTCh(szTime);
  }
  else if (Mode == 3 ) {
    buttonC = false;
    RTC.m--;
    if (RTC.m < 0 ) {
    RTC.m = 59;
  } 
    RTC.writeTime();
    getRTCm(szTime);
  }
  else if (Mode == 4 ) {
    buttonC = false;
      RTC.s = 0;
    RTC.writeTime();
    getTime(szTime);
  }
  else if (Mode == 5 ) {
    buttonC = false;
    RTC.dow--;
    if (RTC.dow <= 0 ) {
    RTC.dow = 7;
  } 
    RTC.writeTime();
    getDay(szMesg);
  }
  else if (Mode == 6 ) {
    buttonC = false;
    RTC.dd--;
    if (RTC.dd <= 0 ) {
    RTC.dd = 31;
  } 
    RTC.writeTime();
    getDate(szDate);
  }    
  else if (Mode == 7 ) {
    buttonC = false;
    RTC.mm--;
    if (RTC.mm <= 0 ) {
    RTC.mm = 12;
  } 
    RTC.writeTime();
    getDate(szDate);
  }      
  else if (Mode == 8 ) {
    buttonC = false;
    RTC.yyyy--;
    if (RTC.yyyy <= 2009 ) {
    RTC.yyyy = 2035;
  } 
    RTC.writeTime();
    getDate(szDate);
  }  

   } 
  
  

   if (Mode == 1 ) {
    getDat(szDat);
   } 
   else if (Mode == 5 ) {
  matrix.drawRect(0, 14, 64, 9, matrix.Color333(7, 7, 0));
   } 
   else if (Mode == 6 ) {
  matrix.drawLine(3, 29, 12, 29, matrix.Color333(7, 0, 0));
   } 
   else if (Mode == 7 ) {
  matrix.drawLine(16, 30, 33, 30, matrix.Color333(0, 7, 7));
   } 
   else if (Mode == 8 ) {
  matrix.drawLine(36, 29, 64, 29, matrix.Color333(7, 0, 0));
   } 
 
}


  void buttonAisPressed()
  {
    if (NewStateOfbuttonA != StateOfbuttonA) 
  {
    if (NewStateOfbuttonA == 0) 
    {
      buttonA=true;
    }
    delay(50);
  }
   StateOfbuttonA = NewStateOfbuttonA;
  }

void buttonBisPressed()
{
  if (NewStateOfbuttonB != StateOfbuttonB) 
  {
    if (NewStateOfbuttonB == 0) {
      buttonB=true;
    }
    delay(50);
  }
   StateOfbuttonB = NewStateOfbuttonB;
}

void buttonCisPressed()
{
   if (NewStateOfbuttonC != StateOfbuttonC) 
  {
    if (NewStateOfbuttonC == 0) {
      buttonC=true;
    }
    delay(50);
  }
   StateOfbuttonC = NewStateOfbuttonC;
}


// 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);
  }
}