A general LED Matrix for all!

Hey everyone,

I purchased an Arduino about a month ago and from that point I have been obsessed with making a RT LED visualizer equilizer. So I first had to code a general class and shift function to handle the matrix WHATEVER SIZE IT MAY BE.

The source can be downloaded here: https://github.com/BirdBare in the folder ArduinoLEDMatrix/LEDMatrix/

It was designed around the shift register s74hc595n.

To use please read the Readme and copy the template. Very easy to use and easy to learn.

I am also working on Matrix Modifiers for a more general visualizer to place on the wall.

Let me know what you think! :)

I hope everyone likes it as much as I like designing it! BirdBare

I did not want to make people look for the code so here it is!

General Matrix

// Designer: Bradley Bare 
//
// Description: This is a class that creates a dynamic grid to your size. All
// dimensions can be changed if the constructor is changed.
//
// Use: Used as a base class from which the LEDs will be controlled.
//

#include "Arduino.h"

#ifndef MatrixD_H
#define MatrixD_H

class DMatrix
{
 protected:
  int width;
  int height;
  int inPins;

 public:
  int*** Matrix;
  //Matrix member variable 
  
  DMatrix(const int x, const int y, const int z) :
          width(x),    height(y),   inPins(z)
  {
    Matrix= new int**[width];
    for(int i=0; i<width; i++)
    {
      Matrix[i]= new int*[height];
      for(int j=0; j<height; j++)
      {
        Matrix[i][j]= new int[inPins];
      };
    };
  };
  //Constructor sets up bool Matrix
};
#endif

LED portion of Matrix

// Designer: Bradley Bare
//
// Description: This class represents all of the LEDs on the matrix. Each LED is
// controlled by the matrix made by the DMatrix class. Comes with function
// shiftMatrix() that shifts Matrix to all Registers and leaves Matrix unchanged
//
// Use: Origin=(0,0) To use, start at Origin and move Right and Up. Each point
// is an LED that has an On(!0) and Off(0) state.

#include "Arduino.h"
#include "DMatrix.h"

#ifndef LEDsMatrix_H
#define LEDsMatrix_H

class LEDs: public DMatrix
{
 protected:
  int dataPin;
  int clockPin;
  int latchPin;
  int LEDperReg;
  int* outPins; 
  //which outpins to use on each register, 1=in-use 0=not
  //Reads Least-to-Greatest pins on. 
  //MUST use a multiple of LEDperReg. MUST be same for all registers
  
 public:  
  LEDs(const int x, const int y, const int z,
       const int d, const int c, const int l, const int r, int *p) : 
       DMatrix(x,y,z),
       dataPin(d),  clockPin(c), latchPin(l), LEDperReg(r), outPins(p) {};
  //Constructor
  
  // Description: Main function of whole Matrix. Shifts the matrix to the 
  // Shift Registers controlling LEDs.
  // 
  // Post: Matrix is set and ready to be latched to pins.
  void shiftMatrix()
  {
    byte data=0;
    
    for(int LED=0, outPin=0; LED<width*height; LED++)
    {
      for(int i=0; i<inPins; i++)
      {
        while(outPins[outPin]==0)
          outPin++;
        bitWrite(data,outPin,outPins[outPin] &
                 !!Matrix[width-(LED/height)-1][height-(LED%height)-1][i]);
        outPin++;
        //While loop skips pins that are not in use so they are not printed
        
      };
      //for loop to set colors on pins if they are enabled inPins=3
      
      if((LED+1)%LEDperReg==0)
      {
        shiftOut(dataPin,clockPin,MSBFIRST,data);
        outPin=0;
        data=0;
      }
      //if statement to shiftOut when all pins per register are set
      
    };
    return;
  }
};

#endif

Readme for above code with template

LEDMatrix
Designed by: Bradley Bare
ANYONE CAN USE THIS FOR ANYTHING
=========

Designed for shift register s74hc595n

******************Rough dataPin wiring diagram*****************
| r=Shift Register | d=dataPin | Ds=dataPin Start(1) | De=dataPin End | 
**************************
        5x5 grid
    
    rd4  rd9  rd14 rDe  r
    rd3  rd8  rd13 rd18 r
    rd2  rd7  rd12 rd17 r
    rd1  rd6  rd11 rd16 r
    rDs  rd5  rd10 rd15 r
    
--------------------------
Explanation: The dataPin wiring comes from the Arduino to Origin(Ds) and follows from 
the serial out pin to the next dataPin(d). 

In short the wiring goes from origin(Ds) to max-height(d4) to origin+1(d5) to 
max-height(d9) and so on.

NOTE: The Matrix controls each LED connected to the Register, NOT the Register
**************************END ROUGH DIAGRAM*********************

================================TEMPLATE===================================
#include <LEDs.h>

const int width=0, height=0, inPins=0;
const int dataPin=0, clockPin=0, latchPin=0;
int LEDperReg=0, outPins[]={0,0,0,0,0,0,0,0};

LEDs LED(width,height,inPins,dataPin,clockPin,latchPin,LEDperReg,outPins);

void setup()
{
  pinMode(dataPin,OUTPUT);
  pinMode(clockPin,OUTPUT);
  pinMode(latchPin,OUTPUT);
  return;
}

void loop()
{
  return;
}

---------------------------------------------------------------------------
Explanation: Anything in the Template initialized as 0 SHOULD be changed
before running code.

width: Width of Matrix
height: Height of Matrix
inPins: The number of Anode connections per LED (1 for Solid LED OR 3 for RGB LED)
dataPin: Desired dataPin
clockPin: Desired clockPin
latchPin: Desired latchPin
LEDperReg: Number of LEDs each register will run
outPins[]: The pins the will have the LED connected (1=Connected OR 0=NotConnected)
==============================END TEMPLATE=================================

//////////////////////Usable Object and Function///////////////////////////

Object Matrix[width][height][inPin(color)]
  Explanation: Each point in the matrix represents an LED in your real Matrix  Origin(0,0)

Function: setMatrix() USE: object_name.setMatrix();
**********Remember to flash latchPin from LOW to HIGH for this function call************

///////////////////////////////////END\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

:slight_smile:

I have been playing around with the matrix and made general shapes you can use! A line from Point A to Point B and a rectangle from a point with a width and height.

I made some code and so did my girlfriend to post to show the simplicity :slight_smile:

They are pretty lazily done But enjoy!!

ps. I updated the github and the use of the Matrix is Delightful!

// Hannah
// Blink love in setup
// Bradley
// Scroll line in loop

#include <LEDs.h>
#include <MatrixModifiers.h>

const int width=5, height=6, inPins=3;
const int dataPin=8, clockPin=7, latchPin=6;
int LEDperReg=2, outPins[]={0,1,1,1,0,1,1,1};

LEDs LED(width,height,inPins,dataPin,clockPin,latchPin,LEDperReg,outPins);
Point sP, eP;

void setup()
{
 pinMode(dataPin,OUTPUT);
 pinMode(clockPin,OUTPUT);
 pinMode(latchPin,OUTPUT);
 digitalWrite(latchPin,LOW);
for(int i=0; i<4; i++)
 {
    digitalWrite(latchPin,LOW);
     Rectangle(LED,Point(0,0),width,height,true,true,BLUE);
   if(i==0)
   {
     Line(LED,Point(0,0),Point(0,5),true,RED);
     Line(LED,Point(0,0),Point(2,0),true,RED);
   }
   if(i==1)
     Rectangle(LED,Point(0,0),3,6,false,true,RED);
   if(i==2)
   {
   Line(LED,Point(0,5),Point(1,0),true,RED);
   Line(LED,Point(1,0),Point(2,5),true,RED);
   }
   if(i==3)
   {
     Line(LED,Point(0,0),Point(0,5),true,RED);
     Line(LED,Point(0,5),Point(1,5),true,RED);
     Line(LED,Point(0,2),Point(1,2),true,RED);
     Line(LED,Point(0,0),Point(1,0),true,RED);
   }
    LED.shiftMatrix();
 digitalWrite(latchPin,HIGH);
 delay(1000);
 if(i!=3)
 {
  digitalWrite(latchPin,LOW);
 Rectangle(LED,Point(0,0),width,height,true,true,WHITE);
 LED.shiftMatrix();
  digitalWrite(latchPin,HIGH);
 }
 };
return;
}

void loop()
{
int w=1, wc=1, h=2;
  int sw=0, swc=0;
  
  Rectangle(LED,Point(0,0),width,height,true,false,WHITE);
  for(int i=0; i+swc<width+1; i++)
  {
    while(w+i+sw>width)
      w--;
    while(i+sw<0)
    {
      sw++;
      w--;
    }
    digitalWrite(latchPin,LOW);
    Rectangle(LED,Point(sw+i,2),w,h,true,true,CYAN); 
    LED.shiftMatrix();
    digitalWrite(latchPin,HIGH);
    
    if(i+swc<width+1)
      delay(200);
    
    digitalWrite(latchPin,LOW);
    Rectangle(LED,Point(sw+i,2),w,h,true,false,CYAN);
    LED.shiftMatrix();
    digitalWrite(latchPin,HIGH); 
    if(sw!=swc)
    {
      w=wc;
      sw=swc;
    }
  };
return;
}