So I have a MAX 7219 up and running lights beautifully, but I am having a programming problem. I want to run a loop continually until the reed switch is closed, then run a different loop continually until it is open. Each loop has an intro animation as well, and I'm indifferent as to whether the reed switch interrupts the animations to change loops or waits until it finishes.
I think the problem is just my conditional statements, but I'm not sure. Here's the code:
#include "binary.h"
#include "LedControl.h"
#define switch 3
LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=100;
int mode = 0;
int val=0;
int holdtime = 1000;
int holdtime2 = 1;
int LEDPin = 13;
byte spin[3]={B11011011,B10110110,B01101101};
byte load[9]={B00000000,B00000001,B00000011,B00000111,B00001111,B00011111,B00111111,B01111111,B11111111};
void setup() {
pinMode(switch, INPUT);
// Wake up
lc.shutdown(0,false);
// Brightness 0-15
lc.setIntensity(0,15);
lc.clearDisplay(0);
pinMode(LEDPin, OUTPUT);
}
void loop() {
val = digitalRead(switch);
if (val == LOW) {
mode=0;
}
if (val == HIGH) {
mode=1;
}
if (mode == 0) {
lc.setIntensity(0,15);
holdtime2 = 1;
if (holdtime > 0) {
lc.setRow(0,0,spin[0]);
delay (holdtime);
lc.setRow(0,0,spin[1]);
delay (holdtime);
lc.setRow(0,0,spin[2]);
delay (holdtime);
holdtime = (holdtime - 100);
}
if (holdtime == 0) {
lc.setRow(0,0,spin[0]);
delay (20);
lc.setRow(0,0,spin[1]);
delay (20);
lc.setRow(0,0,spin[2]);
delay (20);
}
}
if (mode == 1) {
holdtime = 1000;
if (holdtime2 == 1) {
lc.setRow(0,0,load[1]);
delay (200);
lc.setRow(0,0,load[2]);
delay (200);
lc.setRow(0,0,load[3]);
delay (200);
lc.setRow(0,0,load[4]);
delay (200);
lc.setRow(0,0,load[5]);
delay (200);
lc.setRow(0,0,load[6]);
delay (200);
lc.setRow(0,0,load[7]);
delay (200);
lc.setRow(0,0,load[8]);
delay (200);
lc.setRow(0,0,load[9]);
delay (200);
lc.setIntensity(0,14);
delay (200);
lc.setIntensity(0,13);
delay (200);
lc.setIntensity(0,12);
delay (200);
lc.setIntensity(0,11);
delay (200);
lc.setIntensity(0,10);
delay (200);
lc.setIntensity(0,9);
delay (200);
lc.setIntensity(0,8);
delay (200);
lc.setIntensity(0,7);
delay (200);
lc.setIntensity(0,6);
delay (200);
lc.setIntensity(0,5);
delay (200);
lc.setIntensity(0,4);
delay (200);
lc.setIntensity(0,3);
holdtime2 = 0;
}
if (holdtime2 == 0) {
lc.setRow(0,0,load[8]);
}
}
}