// Program to exercise the MD_MAX72XX library
//
// Uses most of the functions in the library
#include <MD_MAX72xx.h>
//#include <SPI.h>
// Turn on debug statements to the serial output
#define DEBUG 1
#define input1 3
#define input2 4
#define input3 5
#define pir 7
#define touch 8
#define sound 9
#define motor 6
#define led 2
#if DEBUG
#define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
#define PRINTS(x) Serial.print(F(x))
#define PRINTD(x) Serial.println(x, DEC)
#else
#define PRINT(s, x)
#define PRINTS(x)
#define PRINTD(x)
#endif
// 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 11
#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);
// We always wait a bit between updates of the display
#define DELAYTIME 100 // in milliseconds
void scrollText(char *p)
{
uint8_t charWidth;
uint8_t cBuf[8]; // this should be ok for all built-in fonts
mx.clear();
while (*p != '\0')
{
charWidth = mx.getChar(*p++, sizeof(cBuf) / sizeof(cBuf[0]), cBuf);
for (uint8_t i=0; i<=charWidth; i++) // allow space between characters
{
mx.transform(MD_MAX72XX::TSL);
if (i < charWidth)
mx.setColumn(0, cBuf[i]);
delay(DELAYTIME);
}
}
}
void spiral()
// setPoint() used to draw a spiral across the whole display
{
PRINTS("\nSpiral in");
int rmin = 0, rmax = ROW_SIZE-1;
int cmin = 0, cmax = (COL_SIZE*MAX_DEVICES)-1;
mx.clear();
while ((rmax > rmin) && (cmax > cmin))
{
// do row
for (int i=cmin; i<=cmax; i++)
{
mx.setPoint(rmin, i, true);
delay(DELAYTIME/MAX_DEVICES);
}
rmin++;
// do column
for (uint8_t i=rmin; i<=rmax; i++)
{
mx.setPoint(i, cmax, true);
delay(DELAYTIME/MAX_DEVICES);
}
cmax--;
// do row
for (int i=cmax; i>=cmin; i--)
{
mx.setPoint(rmax, i, true);
delay(DELAYTIME/MAX_DEVICES);
}
rmax--;
// do column
for (uint8_t i=rmax; i>=rmin; i--)
{
mx.setPoint(i, cmin, true);
delay(DELAYTIME/MAX_DEVICES);
}
cmin++;
}
}
void bounce()
// Animation of a bouncing ball
{
const int minC = 0;
const int maxC = mx.getColumnCount()-1;
const int minR = 0;
const int maxR = ROW_SIZE-1;
int nCounter = 0;
int r = 0, c = 2;
int8_t dR = 1, dC = 1; // delta row and column
PRINTS("\nBouncing ball");
mx.clear();
while (nCounter++ < 200)
{
mx.setPoint(r, c, false);
r += dR;
c += dC;
mx.setPoint(r, c, true);
delay(DELAYTIME/2);
if ((r == minR) || (r == maxR))
dR = -dR;
if ((c == minC) || (c == maxC))
dC = -dC;
}
}
void intensity()
// Demonstrates the control of display intensity (brightness) across
// the full range.
{
uint8_t row;
PRINTS("\nVary intensity ");
mx.clear();
// Grow and get brighter
row = 0;
for (int8_t i=0; i<=MAX_INTENSITY; i++)
{
mx.control(MD_MAX72XX::INTENSITY, i);
if (i%2 == 0)
mx.setRow(row++, 0xff);
delay(DELAYTIME*3);
}
mx.control(MD_MAX72XX::INTENSITY, 8);
}
void blinking()
// Uses the test function of the MAX72xx to blink the display on and off.
{
int nDelay = 500;
PRINTS("\nBlinking");
mx.clear();
while (nDelay > 0)
{
mx.control(MD_MAX72XX::TEST, MD_MAX72XX::ON);
delay(nDelay);
mx.control(MD_MAX72XX::TEST, MD_MAX72XX::OFF);
delay(nDelay);
nDelay -= DELAYTIME;
}
}
void scanLimit(void)
// Uses scan limit function to restrict the number of rows displayed.
{
PRINTS("\nScan Limit");
mx.clear();
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
for (uint8_t row=0; row<ROW_SIZE; row++)
mx.setRow(row, 0xff);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
for (int8_t s=MAX_SCANLIMIT; s>=0; s--)
{
mx.control(MD_MAX72XX::SCANLIMIT, s);
delay(DELAYTIME*5);
}
mx.control(MD_MAX72XX::SCANLIMIT, MAX_SCANLIMIT);
}
void transformation1()
// Demonstrates the use of transform() to move bitmaps on the display
// In this case a user defined bitmap is created and animated.
{
uint8_t arrow[COL_SIZE] =
{
0b00001000,
0b00011100,
0b00111110,
0b01111111,
0b00011100,
0b00011100,
0b00111110,
0b00000000
};
MD_MAX72XX::transformType_t t[] =
{
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TFLR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TRC,
MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD,
MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD,
MD_MAX72XX::TFUD,
MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU,
MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU,
MD_MAX72XX::TINV,
MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC,
MD_MAX72XX::TINV
};
PRINTS("\nTransformation1");
mx.clear();
// use the arrow bitmap
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
for (uint8_t j=0; j<mx.getDeviceCount(); j++)
mx.setBuffer(((j+1)*COL_SIZE)-1, COL_SIZE, arrow);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
delay(DELAYTIME);
// run through the transformations
mx.control(MD_MAX72XX::WRAPAROUND, MD_MAX72XX::ON);
for (uint8_t i=0; i<(sizeof(t)/sizeof(t[0])); i++)
{
mx.transform(t[i]);
delay(DELAYTIME*4);
}
mx.control(MD_MAX72XX::WRAPAROUND, MD_MAX72XX::OFF);
}
void transformation2()
// Demonstrates the use of transform() to move bitmaps on the display
// In this case font characters are loaded into the display for animation.
{
MD_MAX72XX::transformType_t t[] =
{
MD_MAX72XX::TINV,
MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC,
MD_MAX72XX::TINV,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSD, MD_MAX72XX::TSU, MD_MAX72XX::TSD, MD_MAX72XX::TSU,
MD_MAX72XX::TFLR, MD_MAX72XX::TFLR, MD_MAX72XX::TFUD, MD_MAX72XX::TFUD
};
PRINTS("\nTransformation2");
mx.clear();
mx.control(MD_MAX72XX::WRAPAROUND, MD_MAX72XX::OFF);
// draw something that will show changes
for (uint8_t j=0; j<mx.getDeviceCount(); j++)
{
mx.setChar(((j+1)*COL_SIZE)-1, '0'+j);
}
delay(DELAYTIME*5);
// run thru transformations
for (uint8_t i=0; i<(sizeof(t)/sizeof(t[0])); i++)
{
mx.transform(t[i]);
delay(DELAYTIME*3);
}
}
void wrapText()
// Display text and animate scrolling using auto wraparound of the buffer
{
PRINTS("\nwrapText");
mx.clear();
mx.wraparound(MD_MAX72XX::ON);
// draw something that will show changes
for (uint16_t j=0; j<mx.getDeviceCount(); j++)
{
mx.setChar(((j+1)*COL_SIZE)-1, (j&1 ? 'M' : 'W'));
}
delay(DELAYTIME*5);
// run thru transformations
for (uint16_t i=0; i<3*COL_SIZE*MAX_DEVICES; i++)
{
mx.transform(MD_MAX72XX::TSL);
delay(DELAYTIME/2);
}
for (uint16_t i=0; i<3*COL_SIZE*MAX_DEVICES; i++)
{
mx.transform(MD_MAX72XX::TSR);
delay(DELAYTIME/2);
}
for (uint8_t i=0; i<ROW_SIZE; i++)
{
mx.transform(MD_MAX72XX::TSU);
delay(DELAYTIME*2);
}
for (uint8_t i=0; i<ROW_SIZE; i++)
{
mx.transform(MD_MAX72XX::TSD);
delay(DELAYTIME*2);
}
mx.wraparound(MD_MAX72XX::OFF);
}
void showCharset(void)
// Run through display of the the entire font characters set
{
mx.clear();
mx.update(MD_MAX72XX::OFF);
for (uint16_t i=0; i<256; i++)
{
mx.clear(0);
mx.setChar(COL_SIZE-1, i);
if (MAX_DEVICES >= 3)
{
char hex[3];
sprintf(hex, "%02X", i);
mx.clear(1);
mx.setChar((2*COL_SIZE)-1,hex[1]);
mx.clear(2);
mx.setChar((3*COL_SIZE)-1,hex[0]);
}
mx.update();
delay(DELAYTIME*2);
}
mx.update(MD_MAX72XX::ON);
}
void ssound(){
int in1 = digitalRead(input1);
int in2 = digitalRead(input2);
int in3 = digitalRead(input3);
int s_sound = digitalRead(sound);
if(s_sound == HIGH){
if(in1 == LOW){
scrollText("STAND BY");
}
else if(in2 == LOW){
scrollText("RAPAT");
}
else if(in3 == LOW){
scrollText("PRATIKUM");
}
}
else if(s_sound == LOW){
scrollText(" ");
}
}
void spir(){
int s_pir = digitalRead(pir);
if(s_pir == HIGH){
digitalWrite(led, HIGH);
}
else if(s_pir == LOW){
digitalWrite(led, LOW);
}
}
void stouch(){
int s_touch = digitalRead(touch);
if(s_touch == HIGH){
digitalWrite(motor, HIGH);
}
else if(s_touch == LOW){
digitalWrite(motor, LOW);
}
}
void setup()
{
mx.begin();
#if DEBUG
Serial.begin(57600);
#endif
pinMode(input1, INPUT);
pinMode(input2, INPUT);
pinMode(input3, INPUT);
pinMode(pir, INPUT);
pinMode(touch, INPUT);
pinMode(sound, INPUT);
pinMode(motor, OUTPUT);
pinMode(led, OUTPUT);
}
void loop()
{
spir();
stouch();
ssound();
}