#include <LedControl.h>
#define DIN 12
#define CLK 11
#define CS 10
#define COUNT 1 //number of matrices attached
LedControl lc = LedControl(DIN, CLK, CS, COUNT); // create an object to control
// lc.setLed(4, 3, 2, 1); // (object) lc (panel=4, x=3, y=2, state=1)
// variables
unsigned long previousTime = 0, interval = 500;
bool state = 0;
long a, b;
// interrupt variables
volatile bool button; // type volatile for interrupt handler
int buttonPin = 2; // interrupt pin INT0
int count = 0; // indicates the button was pressed
int cases = 7; // number of functions available
void setup() {
Serial.begin(115200);
randomSeed(analogRead(A0)); // increase randomness
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, FALLING); // INT#, ISR, TRIGGER
Serial.print("Press the button to start animations.\nAnimation: ");
Serial.print(count);
}
void loop() { // add a function, comment-out unused functions
if (button) { // check button flag after returning from ISR
delay(150); // debounce the button
button = 0; // reset ISR flag for the next interrupt/button press
count++; // increment selected pattern/case
if (count > cases) { // if counter reached last case...
count = 1; // ... reset counter to first case
}
Serial.print(count);
}
switch (count) {
case 0: blink(); break;
case 1: blinkDelay(); break;
case 2: rain(); break;
case 3: down(); break;
case 4: flash(); break;
case 5: life(); break;
case 6: scan(); break;
case 7: wax(); break;
}
}
int buttonISR() {
noInterrupts(); // disable interrupts while in the ISR
button = 1; // set the flag that the button was pressed
interrupts(); // enable interrupts when leaving the ISR
}
void blink() {
unsigned long currentTime = millis();
if (currentTime - previousTime >= interval) {
previousTime = currentTime; // update timer
state = !state;
}
lc.setLed(0, 0, 0, state); // panel 0, x = 0, y = 0, ON/OFF
}
void blinkDelay() {
state = !state;
lc.setLed(0, 0, 0, state);
delay(500);
}
void rain() {
if (!state) {
a = random(8), b = random(8);
state = 1;
}
long c = random(-1, 2), d = random(-1, 2); // long random (min, max-1);
a += c;
b += d;
if (a > 7) a = 0;
if (a < 0) a = 7;
if (b > 7) b = 0;
if (b < 0) b = 7;
lc.setLed(0, a, b, 1);
delay(100);
}
void down() {
if (b == 7) {
b = -1;
if (a == 7) {
a = -1;
}
a++;
}
b++;
lc.setLed(0, a, b, 1);
delay(100);
lc.setLed(0, a, b, 0);
}
void flash() {
int wait = 200;
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
lc.setLed(0, i, j, 1);
delay(wait);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
lc.setLed(0, i, j, 0);
delay(wait);
}
void life() {
int x = random(8);
int y = random(8);
int z = random(2);
if (z)
lc.setLed(0, x, y, 0);
else
lc.setLed(0, x, y, 1);
delay(10);
}
void scan() {
int wait = 150;
for (int i = 7; i > 0; i--) {
for (int j = 0; j < 8; j++) {
lc.setLed(0, i, j, 1);
lc.setLed(0, j, i, 1);
}
delay(wait);
for (int j = 0; j < 8; j++) {
lc.setLed(0, i, j, 0);
lc.setLed(0, j, i, 0);
}
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 8; j++) {
lc.setLed(0, i, j, 1);
lc.setLed(0, j, i, 1);
}
delay(wait);
for (int j = 0; j < 8; j++) {
lc.setLed(0, i, j, 0);
lc.setLed(0, j, i, 0);
}
}
}
void wax() {
lc.setLed(0, a, b, !state);
a++;
if (a > 7) {
a = 0;
b++;
if (b > 7) {
b = 0;
state = !state;
}
}
delay(50);
}