// Program to exercise the MD_MAX72XX library
// Test the library transformation functions
#include <MD_MAX72xx.h>
#include <SPI.h>
// We always wait a bit between updates of the display
#define DELAYTIME 300 // in milliseconds
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN 13 // or SCK
#define DATA_PIN 11 // or MOSI
#define CS_PIN 10 // or SS
// SPI hardware interface
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary pins
//MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// Global variables
uint32_t lastTime = 0;
int diff = 0 ;
int diffcent = 0 ;
byte left = 28;
byte right = 7;
byte up = 0;
byte characters[][8] = {
{0x00, 0x00, 0x08, 0x1C, 0x2A, 0x49, 0x08, 0x08},
{0x00, 0x08, 0x1C, 0x2A, 0x49, 0x08, 0x08, 0x00},
{0x08, 0x1C, 0x2A, 0x49, 0x08, 0x08, 0x00, 0x00},
{0x1C, 0x2A, 0x49, 0x08, 0x08, 0x00, 0x00, 0x00},
{0x2A, 0x49, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00}
};
//-------------------------------------------------------------
void setup() {
pinMode(A0, INPUT);
mx.begin();
Serial.begin(115200);
lastTime = millis();
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
}
//-------------------------------------------------------------
void showCharset(void) {
mx.clear(1, 2);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
//mx.update(MD_MAX72XX::OFF);
char hex[3];
sprintf(hex, "%02d", abs(diffcent));
mx.clear(1);
mx.setChar((2 * COL_SIZE) - 2, hex[1]);
mx.clear(2);
mx.setChar((3 * COL_SIZE) - 3, hex[0]);
//mx.update();
// delay(DELAYTIME*2);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
//mx.update(MD_MAX72XX::ON);
}
//-------------------------------------------------------------
void runArrows(void) {
if (diffcent > 10 ) {
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
mx.setChar(left, 27);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
mx.transform(3, 3, MD_MAX72XX::TSL);
mx.clear(0, 0);
left++;
if (left > 32)
{
left = 28;
mx.clear(3, 3);
}
}
else if (diffcent < -10) {
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
mx.setChar(right, 26);
mx.transform(0, 0, MD_MAX72XX::TSR);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
mx.clear(3, 3);
right--;
if (right < 3)
{
right = 7;
mx.clear(0, 0);
}
}
else {
for (int i = 0; i < 8; i++) {
mx.setRow(0, i, characters[up][i]);
mx.setRow(3, i, characters[up][i]);
}
up++;
if (up > 4)
{
up = 0;
}
}
}
//-------------------------------------------------------------
void loop() {
int diff = analogRead(A0);
diffcent = (diff - 500) / 10 ;
//Serial.println(diffcent);
if (millis() - lastTime >= DELAYTIME)
{
showCharset();
runArrows();
//delay(200);
lastTime = millis();
}
}