Advertisement
joymonkey

Teeces PSI Sketch (no logics)

Feb 9th, 2017
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.60 KB | None | 0 0
  1. // =======================================================================================
  2. // ============================== Teeces PSI Lighting Sketch =============================
  3. // =======================================================================================
  4. //
  5. // A sketch for running front and rear Teeces PSI's only (no logics!)
  6. //
  7. //   Arduino Pins 9,8,7 -> Front PSI IN D,C,L -> Rear PSI
  8.  
  9. // set brightness levels here (a value of 0-15)...
  10. int RPSIbright=12; //rear PSI
  11. int FPSIbright=12; //front PSI
  12.  
  13. //set the type of our front and rear PSI's
  14. #define PSItype 4
  15. // 1 = Teeces original (6 LEDs of each color, arranged side by side)
  16. // 2 = Teeces original checkerboard (6 LEDs of each color arranged in a checkerboard pattern)
  17. // 3 = Teeces V3.2 PSI by John V (13 LEDs of each color, arranged side by side)
  18. // 4 = Teeces V3.2 PSI checkerboard by John V  (13 LEDs of each color, in a checkerboard pattern)
  19.  
  20. //set timing of the PSI's here (in milliseconds)...
  21. int psiRed=2500;    //how long front PSI stays red
  22. int psiBlue=1700;   //how long front PSI stays blue
  23. int psiYellow=1700; //how long rear PSI stays yellow
  24. int psiGreen=2500;  //how long rear PSI stays green
  25. int rbSlide=125; // mts - time to transition between red and blue in slide mode
  26. int ygSlide=300; // mts - time to transition between yellow and green in slide mode
  27.  
  28. //pulse the PSI Brightness (0=off, 1=Front, 2=Rear, 3=Both)
  29. #define PSIPULSER 0
  30.  
  31. //Use CuriousMarc's Holo Light boards in PWM mode
  32. //(this means they'll need to be connected to pins 3 and 6)
  33. #define CURIOUS
  34.  
  35. // =======================================================================================
  36. // =======================================================================================
  37.  
  38. #define FPSIDEV 0 //front PSI is device #0 in the chain
  39. #define RPSIDEV 1 // rear PSI is device #1 in the chain
  40.  
  41. #include <LedControl.h>
  42. #undef round
  43.  
  44. //START UP LEDCONTROL...
  45. LedControl lcChain=LedControl(9,8,7,2); //
  46.  
  47. // =======================================================================================
  48. #if (PSItype==4)  // slide animation code for Teeces V2 PSI boards (code by John V)
  49. #define HPROW 5
  50. class PSI {
  51.   int stage; //0 thru 6
  52.   int inc;
  53.   int stageDelay[7];
  54.   int cols[7][5];
  55.   int randNumber; //a random number to decide the fate of the last stage
  56.  
  57.   unsigned long timeLast;
  58.   int device;
  59.  
  60.   public:
  61.  
  62.   PSI(int _delay1, int _delay2, int _delay3, int _device)
  63.   {
  64.     device=_device;
  65.    
  66.     stage=0;
  67.     timeLast=0;
  68.     inc=1;
  69.    
  70.     cols[0][0] = B10101000;
  71.     cols[0][1] = B01010100;
  72.     cols[0][2] = B10101000;
  73.     cols[0][3] = B01010100;
  74.     cols[0][4] = B10101000;
  75.    
  76.     cols[1][0] = B00101000; //R B R B R B
  77.     cols[1][1] = B11010100; //B R B R B R
  78.     cols[1][2] = B00101000; //R B R B R B
  79.     cols[1][3] = B11010100; //B R B R B R
  80.     cols[1][4] = B00101000; //R B R B R B
  81.  
  82.     cols[2][0] = B01101000;
  83.     cols[2][1] = B10010100;
  84.     cols[2][2] = B01101000;
  85.     cols[2][3] = B10010100;
  86.     cols[2][4] = B01101000;
  87.    
  88.     cols[3][0] = B01001000;
  89.     cols[3][1] = B10110100;
  90.     cols[3][2] = B01001000;
  91.     cols[3][3] = B10110100;
  92.     cols[3][4] = B01001000;
  93.    
  94.     cols[4][0] = B01011000;
  95.     cols[4][1] = B10100100;
  96.     cols[4][2] = B01011000;
  97.     cols[4][3] = B10100100;
  98.     cols[4][4] = B01011000;
  99.    
  100.     cols[5][0] = B01010000;
  101.     cols[5][1] = B10101100;
  102.     cols[5][2] = B01010000;
  103.     cols[5][3] = B10101100;
  104.     cols[5][4] = B01010000;
  105.    
  106.     cols[6][0] = B01010100;
  107.     cols[6][1] = B10101000;
  108.     cols[6][2] = B01010100;
  109.     cols[6][3] = B10101000;
  110.     cols[6][4] = B01010100;
  111.    
  112.     stageDelay[0] = _delay1 - _delay3;
  113.     stageDelay[1] = _delay3/5;
  114.     stageDelay[2] = _delay3/5;
  115.     stageDelay[3] = _delay3/5;
  116.     stageDelay[4] = _delay3/5;
  117.     stageDelay[5] = _delay3/5;
  118.     stageDelay[6] = _delay2 - _delay3;
  119.   }
  120.  
  121.   void Animate(unsigned long elapsed, LedControl control)
  122.   {
  123.     if ((elapsed - timeLast) < stageDelay[stage]) return;
  124.    
  125.     timeLast = elapsed;
  126.     stage+=inc;
  127.  
  128.     if (stage>6 || stage<0 )
  129.     {
  130.       inc *= -1;
  131.       stage+=inc*2;
  132.     }
  133.    
  134.     if (stage==6) //randomly choose whether or not to go 'stuck'
  135.       {
  136.         randNumber = random(9);
  137.         if (randNumber<5) { //set the last stage to 'stuck'
  138.           cols[6][0] = B01010000;
  139.           cols[6][1] = B10101100;
  140.           cols[6][2] = B01010000;
  141.           cols[6][3] = B10101100;
  142.           cols[6][4] = B01010000;
  143.         }
  144.         else //reset the last stage to a solid color
  145.         {
  146.           cols[6][0] = B01010100;
  147.           cols[6][1] = B10101000;
  148.           cols[6][2] = B01010100;
  149.           cols[6][3] = B10101000;
  150.           cols[6][4] = B01010100;
  151.         }
  152.       }
  153.      if (stage==0) //randomly choose whether or not to go 'stuck'
  154.       {
  155.         randNumber = random(9);
  156.         if (randNumber<5) { //set the first stage to 'stuck'
  157.           cols[0][0] = B00101000; //R B R B R B
  158.           cols[0][1] = B11010100; //B R B R B R
  159.           cols[0][2] = B00101000; //R B R B R B
  160.           cols[0][3] = B11010100; //B R B R B R
  161.           cols[0][4] = B00101000; //R B R B R B
  162.         }
  163.         else //reset the first stage to a solid color
  164.         {
  165.           cols[0][0] = B10101000;
  166.           cols[0][1] = B01010100;
  167.           cols[0][2] = B10101000;
  168.           cols[0][3] = B01010100;
  169.           cols[0][4] = B10101000;
  170.         }
  171.       }
  172.  
  173.     for (int row=0; row<5; row++)
  174.       control.setRow(device,row,cols[stage][row]);
  175.   }
  176. };
  177.  
  178. #endif  
  179.  
  180. // =======================================================================================
  181. #if (PSItype==3)  // slide animation code for Teeces V2 PSI boards (code by John V)
  182. #define HPROW 5
  183.   class PSI {    
  184.     int stage; //0 thru 6
  185.   int inc;
  186.   int stageDelay[7];
  187.   int cols[7];
  188.  
  189.   unsigned long timeLast;
  190.   int device;
  191.  
  192.   public:
  193.  
  194.   PSI(int _delay1, int _delay2, int _delay3, int _device)
  195.   {
  196.     device=_device;
  197.    
  198.     stage=0;
  199.     timeLast=0;
  200.     inc=1;
  201.    
  202.     cols[0] = B11100000;
  203.     cols[1] = B11110000;
  204.     cols[2] = B01110000;
  205.     cols[3] = B01111000;
  206.     cols[4] = B00111000;
  207.     cols[5] = B00111100;
  208.     cols[6] = B00011100;
  209.    
  210.     stageDelay[0] = _delay1 - _delay3;
  211.     stageDelay[1] = _delay3/5;
  212.     stageDelay[2] = _delay3/5;
  213.     stageDelay[3] = _delay3/5;
  214.     stageDelay[4] = _delay3/5;
  215.     stageDelay[5] = _delay3/5;
  216.     stageDelay[6] = _delay2 - _delay3;
  217.     }  
  218.     void Animate(unsigned long elapsed, LedControl control)
  219.     {
  220.     if ((elapsed - timeLast) < stageDelay[stage]) return;
  221.    
  222.     timeLast = elapsed;
  223.     stage+=inc;
  224.  
  225.     if (stage>6 || stage<0 )
  226.     {
  227.       inc *= -1;
  228.       stage+=inc*2;
  229.     }
  230.    
  231.     for (int row=0; row<5; row++)
  232.       control.setRow(device,row,cols[stage]);
  233.     }
  234.   };
  235. #endif
  236. // =======================================================================================
  237. //michael smith's checkerboard PSI method for Teeces original PSI boards
  238. // Michael's original sketch is here : http://pastebin.com/hXeZb7Gd
  239. #if (PSItype==2)
  240. #define HPROW 4
  241. static const int patternAtStage[] = { B01010000, B11010000, B10010000, B10110000, B10100000, B00100000, B01100000, B01000000, B01010000 };
  242. class PSI
  243. {    
  244.     bool state;
  245.     int stage;
  246.     unsigned long timeLast;
  247.     int delay1, delay2, delay3;
  248.     int device;    
  249.     int delayAtStage[9];
  250.     int slideDirection;  // is either 1 or -1
  251.     int maxStage;  // for PSIslide it's either 5 or 9 stages, for traditional PSI it's just back and forth between 2    
  252. public:        
  253.     PSI(int _delay1, int _delay2, int _delay3, int _device)
  254.     {
  255.         delayAtStage[0] = _delay1;
  256.         delayAtStage[1] = _delay3/3;        // delay3 is total transition time - divide it by the 3 stages of transition
  257.         delayAtStage[2] = delayAtStage[1];
  258.         delayAtStage[3] = delayAtStage[1];
  259.         delayAtStage[4] = _delay2;
  260.         delayAtStage[5] = delayAtStage[1];
  261.         delayAtStage[6] = delayAtStage[1];
  262.         delayAtStage[7] = delayAtStage[1];
  263.         delayAtStage[8] = _delay1;          // repeated because it's not a loop it cycles back and forth across the pattern.        
  264.         stage=0;
  265.         slideDirection=1;
  266.         maxStage=8;         // change to 5 would skip the LtoR from blue to red.
  267.         timeLast=0;
  268.         device=_device;        
  269.         // legacy for traditional PSI animation
  270.         delay1=_delay1;
  271.         delay2=_delay2;
  272.         delay3=_delay3;
  273.         state=false;
  274.     }    
  275.     void Animate(unsigned long timeNow, LedControl control)
  276.     {
  277.             if ((timeNow - timeLast) < delayAtStage[stage]) return;
  278.             //Serial.begin(9600);
  279.             //Serial.println(stage);
  280.             //Serial.println(patternAtStage[stage]);            
  281.             timeLast = timeNow;
  282.             stage+=slideDirection; //move to the next stage, which could be up or down in the array
  283.             if (stage >= maxStage)
  284.             {
  285.                 // limit the stage to the maxStage and reverse the direction of the slide
  286.                 stage=maxStage;
  287.                 slideDirection = -1;
  288.             }
  289.             else if (stage <= 0)
  290.             {
  291.                 stage=0;
  292.                 slideDirection = 1;
  293.             }
  294.             // set the patterns for this stage
  295.             control.setRow(device,0,patternAtStage[stage]);
  296.             control.setRow(device,1,~patternAtStage[stage]);
  297.             control.setRow(device,2,patternAtStage[stage]);
  298.             control.setRow(device,3,~patternAtStage[stage]);
  299.     }
  300. };
  301. #endif
  302. // =======================================================================================
  303. // slide animation code for Teeces original PSI boards
  304. #if (PSItype==1)
  305. #define HPROW 4
  306.   class PSI {
  307.     int stage; //0 thru 4
  308.     int inc;
  309.     int stageDelay[5];
  310.     int cols[5];
  311.     unsigned long timeLast;
  312.     int device;
  313.     public:  
  314.     PSI(int _delay1, int _delay2, int _device)  {
  315.       device=_device;    
  316.       stage=0;
  317.       timeLast=0;
  318.       inc=1;    
  319.       cols[0] = B11000000;
  320.       cols[1] = B11100000;
  321.       cols[2] = B01100000;
  322.       cols[3] = B01110000;
  323.       cols[4] = B00110000;    
  324.       stageDelay[0] = _delay1 - 300;
  325.       stageDelay[1] = 100;
  326.       stageDelay[2] = 100;
  327.       stageDelay[3] = 100;
  328.       stageDelay[4] = _delay2 - 300;
  329.     }  
  330.     void Animate(unsigned long elapsed, LedControl control)  {
  331.       if ((elapsed - timeLast) < stageDelay[stage]) return;
  332.       timeLast = elapsed;
  333.       stage+=inc;
  334.       if (stage>4 || stage<0 ) {
  335.         inc *= -1;
  336.         stage+=inc*2;
  337.       }    
  338.       for (int row=0; row<4; row++) control.setRow(device,row,cols[stage]);
  339.     }
  340.   };
  341. #endif  
  342. // =======================================================================================
  343. #if (PSItype==1)
  344.   PSI psiFront=PSI(psiRed, psiBlue, FPSIDEV); // device is FPSIDEV (#2 or #4 for an R7 dome)
  345.   PSI psiRear =PSI(psiYellow, psiGreen, RPSIDEV); // device is #3
  346. //#endif
  347. //#if (PSItype==2) || (PSItype==3)
  348. #else
  349.   PSI psiFront=PSI(psiRed, psiBlue, rbSlide, FPSIDEV); //2000 ms on red, 1000 ms on blue.
  350.   PSI psiRear =PSI(psiYellow, psiGreen, ygSlide, RPSIDEV); //1000 ms on yellow, 2000 ms on green.
  351. #endif
  352. // =======================================================================================
  353. void setup() {
  354.   Serial.begin(9600); //used for debugging
  355.  
  356.   lcChain.shutdown(0, false); //take the device out of shutdown (power save) mode
  357.   lcChain.clearDisplay(0);
  358.   lcChain.shutdown(1, false); //take the device out of shutdown (power save) mode
  359.   lcChain.clearDisplay(1);
  360.  
  361.   lcChain.setIntensity(FPSIDEV, FPSIbright); //Front PSI
  362.   lcChain.setIntensity(RPSIDEV, RPSIbright); //Rear PSI
  363.  
  364.   //HP lights on constantly...
  365.   lcChain.setRow(RPSIDEV,HPROW,255); //rear psi
  366.   lcChain.setRow(FPSIDEV,HPROW,255); //front psi
  367.  
  368.   #if (BOARDtype==2)
  369.   pinMode(17, OUTPUT);  // Set RX LED of Pro Micro as an output
  370.   #endif
  371.  
  372.   #if defined(CURIOUS)
  373.   pinMode(3, OUTPUT);
  374.   pinMode(6, OUTPUT);
  375.   #endif
  376.  
  377. }
  378. // =======================================================================================
  379. void loop() {
  380.   unsigned long timeNew= millis();
  381.   psiFront.Animate(timeNew, lcChain);
  382.   psiRear.Animate(timeNew, lcChain);
  383. #if PSIPULSER>0
  384.   PSIpulse(timeNew);
  385. #endif
  386. #if defined(CURIOUS)
  387.   CuriousFade(timeNew);
  388. #endif
  389. }
  390.  
  391. // =======================================================================================
  392. // PULSING PSI LED BRIGHTNESS/INTENSITY
  393. #if PSIPULSER>0
  394. int pulseState = LOW; //initial state of our PSI (high intensity, going down)
  395. int pulseVal = 15; //initial value of our PSI (full intensity)
  396. void PSIpulse(unsigned long elapsed) {
  397.   static unsigned long timeLast=0;
  398.   if ((elapsed - timeLast) < 100) return; //proceed if 100 milliseconds have passed
  399.   timeLast = elapsed;
  400.   if (pulseState == HIGH) { //increase intensity
  401.     pulseVal++; //increase value by 1
  402.     if (pulseVal == 16) { //if we've gone beyond full intensity, start going down again
  403.       pulseVal = 15;
  404.       pulseState = LOW;
  405.     }
  406.   }
  407.   else { //decrease intensity
  408.     pulseVal--; //increase value by 1
  409.     if (pulseVal == 0) { //if we've gone beyond full intensity, start going down again
  410.       pulseVal = 1;
  411.       pulseState = HIGH;
  412.     }
  413.   }
  414.   #if PSIPULSER==1 || PSIPULSER==3
  415.   lcChain.setIntensity(0,pulseVal); //set the front intensity
  416.   #endif  
  417.   #if PSIPULSER==2 || PSIPULSER==3
  418.   lcChain.setIntensity(1,pulseVal); //set the rear intensity
  419.   #endif  
  420. }
  421. #endif
  422. ////////////////////////////////////
  423.  
  424. // =======================================================================================
  425. // PULSE THE BRIGHTNESS OF THE CURIOUS MARC HOLO LIGHTS
  426. #if defined(CURIOUS)
  427. int Cbrightness = 0;    // how bright the LED starts at
  428. int CfadeAmount = 1;    // how many points to fade the LED by
  429. int holdtime=1000; //default 'pause' is set to 5 seconds
  430. int fadespeed=1; //milliseconds to wait between actions (lower number speeds things up)
  431. void CuriousFade(unsigned long elapsed) {
  432.   analogWrite(3, Cbrightness);  
  433.   analogWrite(6, Cbrightness);  
  434.   static unsigned long timeLast=0;
  435.   if ((elapsed - timeLast) < (fadespeed + holdtime)) return; //proceed if some fadespeed+holdtime milliseconds have passed
  436.   timeLast = elapsed;
  437.   holdtime = 0;
  438.   Cbrightness = Cbrightness + CfadeAmount;
  439.   if (Cbrightness == 0 || Cbrightness == 255) {
  440.     CfadeAmount = -CfadeAmount ; //reverse fade direction
  441.     holdtime = random(9)*1000; //generate a new 'pause' time of 0 to 9 seconds
  442.     fadespeed = random(1,20); //generate a new fadespeed time of 1 to 20 milliseconds
  443.   }
  444. }
  445. #endif
  446. ////////////////////////////////////
  447.  
  448.  
  449.  
  450. // =======================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement