Pages: [1]   Go Down
Author Topic: Help with a reed switch controlling a MAX7219  (Read 773 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]);
}
}
}
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Its only going to look at the condition of the  switch each time round the loop and since the loop will take in excess of 4 seconds due to all the delays, it isn't going to be particularly responsive to the what the reed switch says..........

The time honoured solutions to many problems - take a look at 'Blink without delay'.   
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would using a switch case work?  I have four conditions: switch off, intro animation not ran yet; switch off, intro animation ran; switch on, intro animation not ran yet; switch on, intro animation ran.

Thanks for the tip that it runs the whole loop - didn't realize it!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I figured out what I needed to do - I wasn't even asking the right question I think.  I needed to compare the state of the switch with the state in the previous loop.  If it changed, I needed to run the intro first.
#include "binary.h"
#include "LedControl.h"
#define switch 3
LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=100;
int mode = 0;
int oldmode = 0;
int val=0;
int holdtime = 1000;
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 spinup(){
    lc.setIntensity(0,15);
  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);
}
}

void spinning(){
  lc.setRow(0,0,spin[0]);
  delay (20);
  lc.setRow(0,0,spin[1]);
  delay (20);
  lc.setRow(0,0,spin[2]);
  delay (20);
}

void loading(){
    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);
}

void waiting(){
  lc.setIntensity(0,3);
  lc.setRow(0,0,load[8]);
}

void loop() {

val = digitalRead(switch);
if (val == LOW) {
mode=0;
}
if (val == HIGH) {
mode=1;
}
 
if (mode == 0 && oldmode == 1) {
spinup();

}
else if (mode == 0 && oldmode == 0) {
spinning();
}

else if (mode == 1 && oldmode == 0) {
loading();

}

else if (mode == 1 && oldmode == 1) {
waiting();
}
oldmode = mode;
}

Logged

Pages: [1]   Go Up
Jump to: