// original code found here:
// https://projecthub.arduino.cc/mdraber/controlling-8x8-dot-matrix-with-max7219-and-arduino-0c417a
//
// Extended by: John Craven 2024
//
// My son started playing with an UNO and a single 8x8 led matrix. He asked me how he would animate an
// example he found at the link above. We extended the single symbol demo a bit and then moved to showing ascii
// characters and string messages. My son then wanted to try drawing "graphical animations" rather then using
// symbols/sprites.
//
// This project an effort to show him howto build a library of reusuable drawing primitives.
// The drawing functions are found in Drawing.h
//
// Ideally the main sketch is not altered.
//
// To create a new demo animation, create an new myDemo.h file, and implement DemoInit() and DemoWork()
// You can copy the contents of DemoTemplate.h to you new myDemo.h file.
// You must add an #include directive to Demos.h, ie #include myDemo.h
// Only uncomment one #include in Samples.h at one time.
//
// The DemoWork() function, must do one cycle or step of your animation.
// Ideally you dont add any delay statements in your demo code.
// The default cycle interval is 1 second.
// To control animation speed, set the global interval variable in your demo file to control animation speed.
//
// If you need Serial debugging, add it to your DemoInit(). See Debugging.h
//
// Currently:
// only a single 8x8 display is supported.
// no error checking is done on drawing function parameters.
// if necessary, demos can directly manipulate the global matrix buffer variable found in Drawing.h
#include <SPI.h>
#define CS 12
#include "Demos.h"
// MAX7219 Control registers
#define DECODE_MODE 9
#define INTENSITY 0x0A
#define SCAN_LIMIT 0x0B
#define SHUTDOWN 0x0C
#define DISPLAY_TEST 0x0F
void SendData(uint8_t address, uint8_t value) {
digitalWrite(CS, LOW); // Start transfer, select chip
SPI.transfer(address); // Send address.
SPI.transfer(value); // Send the value.
digitalWrite(CS, HIGH); // Finish transfer, unselect chip
}
void setup() {
pinMode(CS, OUTPUT);
SPI.setBitOrder(MSBFIRST); // Most significant bit first
SPI.begin(); // Start SPI
SendData(DISPLAY_TEST, 0x01); // Run test - All LED segments lit.
delay(1000);
SendData(DISPLAY_TEST, 0x00); // Finish test mode.
SendData(DECODE_MODE, 0x00); // Disable BCD mode.
SendData(INTENSITY, 0x0e); // Use lowest intensity.
SendData(SCAN_LIMIT, 0x0f); // Scan all digits.
SendData(SHUTDOWN, 0x01); // Turn on chip.
DemoInit();
}
void UpdateSreen()
{
for (int i=1;i<9;i++)
SendData(i, buffer[i-1]);
delay(interval);
}
void loop() {
DemoWork();
UpdateSreen();
}