View difference between Paste ID: yBSe2JWU and hXeZb7Gd
SHOW: | | - or go back to the newest paste.
1
#include <LedControl.h>
2
#undef round
3
4-
// Teeces V3 Basic Sketch
4+
// Teeces V3 Basic Sketch (**UNTESTED**)
5
// joymonkey added: With dumbed-down easily configured options at the start,
6
// so you can easily adjust chain order, brightness and speeds.
7
// Michael Smith (michael@badweasel.com) converted it to do a sliding pattern on the PSI's
8
// (PSI's LEDs should be in a checkerboard color pattern)
9
10
// Define devices here. Change these numbers depending on
11
// what order you have everything connected in the chain.
12
int rld1dev=0; //first LED driver on RLD
13
int rld2dev=1; //second LED driver on RLD
14
int rld3dev=2; //third LED driver on RLD
15
//
16
int psi1dev=3; //LED driver on Rear (Green/Yellow) PSI
17
int psi2dev=4; //LED driver on Front (Red/Blue) PSI
18
//
19
int fld1dev=5; //LED driver on FLD 1
20
int fld2dev=6; //LED driver on FLD 2
21
int fld3dev=7; //LED driver on FLD 3
22
int fld4dev=8; //LED driver on FLD 4
23
//
24
// Logic Display settings...
25
int rldbright=12;  // RLD brightness (0 to 15)
26
int fldbright=7;   // FLD brightness (0 to 15)
27
int logicdelay=175; // adjust this number to change the speed of the logic patterns (lower is faster)
28
//
29
// PSI settings...
30
int psibright=15; // PSI brightness (0 to 15)
31
int red=2000;  // time (in milliseconds) to stay red (default is 2000)
32
int blue=1000; // time (in milliseconds) to stay blue (default is 1000)
33
int rbSlide=200; // mts - time to transition between red and blue in slide mode
34
int yellow=1000; // time (in milliseconds) to stay yellow (default is 1000)
35
int green=2000; // time (in milliseconds) to stay green (default is 2000)
36
int ygSlide=200; // mts - time to transition between yellow and green in slide mode
37
38
bool PSISlide=true;  // mts - if FALSE will flash back and forth the old way
39
40
// this pattern is the secret sauce for the PSI...  I had to declare global here cause for some reason you can't assign it inside a class.
41
static const int patternAtStage[] = { B01010000, B11010000, B10010000, B10110000, B10100000, B00100000, B01100000, B01000000, B01010000 };
42
43
//
44
// We're done with setting numbers.
45
// We shouldn't have to change anything below here.
46
//
47
48
class PSI
49
{    
50
    bool state;
51
    int stage;
52
    unsigned long timeLast;
53
    int delay1, delay2, delay3;
54
    int device;
55
    
56
    int delayAtStage[9];
57
    int slideDirection;  // is either 1 or -1
58
    int maxStage;  // for PSIslide it's either 5 or 9 stages, for traditional PSI it's just back and forth between 2
59
    
60
public:
61
    
62
    // Michael Smith - modified this to have a 3rd delay for the transition time between red and blue
63
    // the slide cycle is RED, LtoR transition to blue (3 steps), BLUE, LtoR transition to red (3 steps), RED.
64
    // then it reverses direction and does that cycle in reverse.
65
        
66
    // the LED pattern on a traditional PSI are
67
    // x R B x
68
    // R R B B
69
    // R R B B
70
    // x R B x
71
    
72
    // the LED pattern on my sliding PSI is checkerboarded:
73
    // pattern:      red on:       blue on:
74
    // x R B x       0101----      1010----
75
    // R B R B       1010----      0101----
76
    // B R B R       0101----      1010----
77
    // x B R x       1010----      0101----
78
    
79
    // even lines are the inverse of the odd lines.  So even = ~odd;  (bitwise NOT)
80
        
81
    PSI(int _delay1, int _delay2, int _delay3, int _device)
82
    {
83
        delayAtStage[0] = _delay1;
84
        delayAtStage[1] = _delay3/3;        // delay3 is total transition time - divide it by the 3 stages of transition
85
        delayAtStage[2] = delayAtStage[1];
86
        delayAtStage[3] = delayAtStage[1];
87
        delayAtStage[4] = _delay2;
88
        delayAtStage[5] = delayAtStage[1];
89
        delayAtStage[6] = delayAtStage[1];
90
        delayAtStage[7] = delayAtStage[1];
91
        delayAtStage[8] = _delay1;          // repeated because it's not a loop it cycles back and forth across the pattern.
92
        
93
        stage=0;
94
        slideDirection=1;
95
        maxStage=8;         // change to 5 would skip the LtoR from blue to red.
96
97
        timeLast=0;
98
        device=_device;
99
        
100
        // legacy for traditional PSI animation
101
        delay1=_delay1;
102
        delay2=_delay2;
103
        delay3=_delay3;
104
        state=false;
105
    }
106
    
107
    
108
    void Animate(unsigned long timeNow, LedControl control)
109
    {
110
        if (PSISlide)
111
        {
112
            if ((timeNow - timeLast) < delayAtStage[stage]) return;
113
114
            //Serial.begin(9600);
115
            //Serial.println(stage);
116
            //Serial.println(patternAtStage[stage]);
117
            
118
            timeLast = timeNow;
119
            stage+=slideDirection; //move to the next stage, which could be up or down in the array
120
            if (stage >= maxStage)
121
            {
122
                // limit the stage to the maxStage and reverse the direction of the slide
123
                stage=maxStage;
124
                slideDirection = -1;
125
            }
126
            else if (stage <= 0)
127
            {
128
                stage=0;
129
                slideDirection = 1;
130
            }
131
            // set the patterns for this stage
132
            control.setRow(device,0,patternAtStage[stage]);
133
            control.setRow(device,1,~patternAtStage[stage]);
134
            control.setRow(device,2,patternAtStage[stage]);
135
            control.setRow(device,3,~patternAtStage[stage]);
136
            
137
        }
138
        else
139
        {
140
            // this is the original flip flop and assumes the original LED pattern
141
            if (state && (timeNow - timeLast)  < delay1) return;
142
            if (!state && (timeNow - timeLast) < delay2) return;
143
            
144
            timeLast = timeNow;
145
            state=!state;
146
            
147
            int cols=B11000000;
148
            if (state) cols=B00110000;
149
            for (int row=0; row<4; row++)
150
                control.setRow(device,row,cols);
151
        }
152
    }
153
};
154
155
PSI psiFront=PSI(red, blue, rbSlide, psi1dev); //2000 ms on red, 1000 ms on blue.
156
PSI psiRear =PSI(yellow, green, ygSlide, psi2dev); //1000 ms on yellow, 2000 ms on green.
157
LedControl lc=LedControl(12,11,10,7);
158
159
void setup()
160
{
161
    for(int dev=0;dev<lc.getDeviceCount();dev++)
162
    {
163
        lc.shutdown(dev, false); //take the device out of shutdown (power save) mode
164
        lc.clearDisplay(dev);
165
    }
166
    
167
    lc.setIntensity(rld1dev, rldbright); //RLD
168
    lc.setIntensity(rld2dev, rldbright); //RLD
169
    lc.setIntensity(rld3dev, rldbright); //RLD
170
    lc.setIntensity(fld1dev, fldbright);  //FLD
171
    lc.setIntensity(fld2dev, fldbright);  //FLD
172
    lc.setIntensity(fld3dev, fldbright);  //FLD
173
    lc.setIntensity(fld4dev, fldbright);  //FLD
174
    lc.setIntensity(psi1dev, psibright); //PSI
175
    lc.setIntensity(psi2dev, psibright); //PSI
176
    
177
    //  pinMode(13, OUTPUT);
178
    //  digitalWrite(13, HIGH);
179
    
180
    //HP lights on constant
181
    lc.setRow(psi1dev,4,255);
182
    lc.setRow(psi2dev,4,255);
183
    
184
}
185
186
void loop()
187
{
188
    unsigned long timeNew= millis();
189
    psiFront.Animate(timeNew, lc);
190
    psiRear.Animate(timeNew, lc);
191
    animateLogic(timeNew);
192
}
193
194
void animateLogic(unsigned long timeNow)
195
{
196
    static unsigned long timeLast=0;
197
    if ((timeNow - timeLast) < logicdelay) return;
198
    timeLast = timeNow;
199
    
200
    for (int row=0; row<6; row++)
201
    {
202
        lc.setRow(rld1dev,row,random(0,256)); 
203
        lc.setRow(rld2dev,row,random(0,256)); 
204
        lc.setRow(rld3dev,row,random(0,256)); 
205
        lc.setRow(fld1dev,row,random(0,256)); 
206
        lc.setRow(fld2dev,row,random(0,256));
207
	lc.setRow(fld3dev,row,random(0,256)); 
208
        lc.setRow(fld4dev,row,random(0,256));
209
    }
210
}