Advertisement
MichaelSmith

Teeces V3 Sketch modified for sliding PSIs

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