Advertisement
Guest User

arduino stumped

a guest
Feb 7th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.93 KB | None | 0 0
  1. //// LIBRARIES /////////////////////////////////////////////////////
  2.  
  3. #include <Adafruit_NeoPixel.h>
  4. #include <Wire.h>
  5. #include <Adafruit_PWMServoDriver.h>
  6.  
  7. //// NEO PATTERNS CLASS /////////////////////////////////////////////////////
  8. //// https://learn.adafruit.com/multi-tasking-the-arduino-part-3/utility-functions ////
  9.  
  10. // Pattern types supported:
  11. enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
  12. // Patern directions supported:
  13. enum  direction { FORWARD, REVERSE };
  14.  
  15. // NeoPattern Class - derived from the Adafruit_NeoPixel class
  16. class NeoPatterns : public Adafruit_NeoPixel
  17. {
  18.     public:
  19.  
  20.     // Member Variables:  
  21.     pattern  ActivePattern;  // which pattern is running
  22.     direction Direction;     // direction to run the pattern
  23.    
  24.     unsigned long Interval;   // milliseconds between updates
  25.     unsigned long lastUpdate; // last update of position
  26.    
  27.     uint32_t Color1, Color2;  // What colors are in use
  28.     uint16_t TotalSteps;  // total number of steps in the pattern
  29.     uint16_t Index;  // current step within the pattern
  30.    
  31.     void (*OnComplete)();  // Callback on completion of pattern
  32.    
  33.     // Constructor - calls base-class constructor to initialize strip
  34.     NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
  35.     :Adafruit_NeoPixel(pixels, pin, type)
  36.     {
  37.         OnComplete = callback;
  38.     }
  39.    
  40.     // Update the pattern
  41.     void Update()
  42.     {
  43.         if((millis() - lastUpdate) > Interval) // time to update
  44.         {
  45.             lastUpdate = millis();
  46.             switch(ActivePattern)
  47.             {
  48.                 case RAINBOW_CYCLE:
  49.                     RainbowCycleUpdate();
  50.                     break;
  51.                 case THEATER_CHASE:
  52.                     TheaterChaseUpdate();
  53.                     break;
  54.                 case COLOR_WIPE:
  55.                     ColorWipeUpdate();
  56.                     break;
  57.                 case SCANNER:
  58.                     ScannerUpdate();
  59.                     break;
  60.                 case FADE:
  61.                     FadeUpdate();
  62.                     break;
  63.                 default:
  64.                     break;
  65.             }
  66.         }
  67.     }
  68.  
  69.     // Increment the Index and reset at the end
  70.     void Increment()
  71.     {
  72.         if (Direction == FORWARD)
  73.         {
  74.            Index++;
  75.            if (Index >= TotalSteps)
  76.             {
  77.                 Index = 0;
  78.                 if (OnComplete != NULL)
  79.                 {
  80.                     OnComplete(); // call the comlpetion callback
  81.                 }
  82.             }
  83.         }
  84.         else // Direction == REVERSE
  85.         {
  86.             --Index;
  87.             if (Index <= 0)
  88.             {
  89.                 Index = TotalSteps-1;
  90.                 if (OnComplete != NULL)
  91.                 {
  92.                     OnComplete(); // call the comlpetion callback
  93.                 }
  94.             }
  95.         }
  96.     }
  97.    
  98.     // Reverse pattern direction
  99.     void Reverse()
  100.     {
  101.         if (Direction == FORWARD)
  102.         {
  103.             Direction = REVERSE;
  104.             Index = TotalSteps-1;
  105.         }
  106.         else
  107.         {
  108.             Direction = FORWARD;
  109.             Index = 0;
  110.         }
  111.     }
  112.    
  113.     // Initialize for a RainbowCycle
  114.     void RainbowCycle(uint8_t interval, direction dir = FORWARD)
  115.     {
  116.         ActivePattern = RAINBOW_CYCLE;
  117.         Interval = interval;
  118.         TotalSteps = 255;
  119.         Index = 0;
  120.         Direction = dir;
  121.     }
  122.    
  123.     // Update the Rainbow Cycle Pattern
  124.     void RainbowCycleUpdate()
  125.     {
  126.         for(int i=0; i< numPixels(); i++)
  127.         {
  128.             setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
  129.         }
  130.         show();
  131.         Increment();
  132.     }
  133.  
  134.     // Initialize for a Theater Chase
  135.     void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
  136.     {
  137.         ActivePattern = THEATER_CHASE;
  138.         Interval = interval;
  139.         TotalSteps = numPixels();
  140.         Color1 = color1;
  141.         Color2 = color2;
  142.         Index = 0;
  143.         Direction = dir;
  144.    }
  145.    
  146.     // Update the Theater Chase Pattern
  147.     void TheaterChaseUpdate()
  148.     {
  149.         for(int i=0; i< numPixels(); i++)
  150.         {
  151.             if ((i + Index) % 3 == 0)
  152.             {
  153.                 setPixelColor(i, Color1);
  154.             }
  155.             else
  156.             {
  157.                 setPixelColor(i, Color2);
  158.             }
  159.         }
  160.         show();
  161.         Increment();
  162.     }
  163.  
  164.     // Initialize for a ColorWipe
  165.     void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
  166.     {
  167.         ActivePattern = COLOR_WIPE;
  168.         Interval = interval;
  169.         TotalSteps = numPixels();
  170.         Color1 = color;
  171.         Index = 0;
  172.         Direction = dir;
  173.     }
  174.    
  175.     // Update the Color Wipe Pattern
  176.     void ColorWipeUpdate()
  177.     {
  178.         setPixelColor(Index, Color1);
  179.         show();
  180.         Increment();
  181.     }
  182.    
  183.     // Initialize for a SCANNNER
  184.     void Scanner(uint32_t color1, uint8_t interval)
  185.     {
  186.         ActivePattern = SCANNER;
  187.         Interval = interval;
  188.         TotalSteps = (numPixels() - 1) * 2;
  189.         Color1 = color1;
  190.         Index = 0;
  191.     }
  192.  
  193.     // Update the Scanner Pattern
  194.     void ScannerUpdate()
  195.     {
  196.         for (int i = 0; i < numPixels(); i++)
  197.         {
  198.             if (i == Index)  // Scan Pixel to the right
  199.             {
  200.                  setPixelColor(i, Color1);
  201.             }
  202.             else if (i == TotalSteps - Index) // Scan Pixel to the left
  203.             {
  204.                  setPixelColor(i, Color1);
  205.             }
  206.             else // Fading tail
  207.             {
  208.                  setPixelColor(i, DimColor(getPixelColor(i)));
  209.             }
  210.         }
  211.         show();
  212.         Increment();
  213.     }
  214.    
  215.     // Initialize for a Fade
  216.     void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
  217.     {
  218.         ActivePattern = FADE;
  219.         Interval = interval;
  220.         TotalSteps = steps;
  221.         Color1 = color1;
  222.         Color2 = color2;
  223.         Index = 0;
  224.         Direction = dir;
  225.     }
  226.    
  227.     // Update the Fade Pattern
  228.     void FadeUpdate()
  229.     {
  230.         // Calculate linear interpolation between Color1 and Color2
  231.         // Optimise order of operations to minimize truncation error
  232.         uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
  233.         uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
  234.         uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
  235.        
  236.         ColorSet(Color(red, green, blue));
  237.         show();
  238.         Increment();
  239.     }
  240.    
  241.     // Calculate 50% dimmed version of a color (used by ScannerUpdate)
  242.     uint32_t DimColor(uint32_t color)
  243.     {
  244.         // Shift R, G and B components one bit to the right
  245.         uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
  246.         return dimColor;
  247.     }
  248.  
  249.     // Set all pixels to a color (synchronously)
  250.     void ColorSet(uint32_t color)
  251.     {
  252.         for (int i = 0; i < numPixels(); i++)
  253.         {
  254.             setPixelColor(i, color);
  255.         }
  256.         show();
  257.     }
  258.  
  259.     // Returns the Red component of a 32-bit color
  260.     uint8_t Red(uint32_t color)
  261.     {
  262.         return (color >> 16) & 0xFF;
  263.     }
  264.  
  265.     // Returns the Green component of a 32-bit color
  266.     uint8_t Green(uint32_t color)
  267.     {
  268.         return (color >> 8) & 0xFF;
  269.     }
  270.  
  271.     // Returns the Blue component of a 32-bit color
  272.     uint8_t Blue(uint32_t color)
  273.     {
  274.         return color & 0xFF;
  275.     }
  276.    
  277.     // Input a value 0 to 255 to get a color value.
  278.     // The colours are a transition r - g - b - back to r.
  279.     uint32_t Wheel(byte WheelPos)
  280.     {
  281.         WheelPos = 255 - WheelPos;
  282.         if(WheelPos < 85)
  283.         {
  284.             return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  285.         }
  286.         else if(WheelPos < 170)
  287.         {
  288.             WheelPos -= 85;
  289.             return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  290.         }
  291.         else
  292.         {
  293.             WheelPos -= 170;
  294.             return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  295.         }
  296.     }
  297. };
  298.  
  299.  
  300.  
  301. //// SERIAL AND PYTHON //////////////////////////////////////////////
  302. byte incomingByte; // from python
  303. int command = 0; // command (1 = open, 2 = close)
  304.  
  305.  
  306. //// SERVO //////////////////////////////////////////////////////////
  307. Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
  308. #define servoMin  170 // this is the 'minimum' pulse length count (out of 4096)
  309. #define servoMax  800 // this is the 'maximum' pulse length count (out of 4096)
  310.  
  311. static int servoPins[] = {15,14,13};
  312. const size_t n = sizeof(servoPins) / sizeof(servoPins[0]);
  313.  
  314. int startServo = 0; // the incoming command shit
  315.  
  316. //// NEO PATTERNS ///////////////////////////////////////////////////////
  317.  
  318. static int neoPixelPins[3] = {}; //
  319. const size_t q = sizeof(neoPixelPins) / sizeof(neoPixelPins[0]);
  320.  
  321. NeoPatterns Strip1(32, neoPixelPins[0], NEO_GRB + NEO_KHZ800, &StripComplete); // flower_1
  322. NeoPatterns Strip2(32, neoPixelPins[1], NEO_GRB + NEO_KHZ800, &StripComplete); // flower_2
  323. NeoPatterns Strip3(32, neoPixelPins[2], NEO_GRB + NEO_KHZ800, &StripComplete); // flower_3
  324.  
  325. //// INITIALIZE //////////////////////////////////////////////////////////
  326.  
  327. bool endOfGame = false;
  328.  
  329. void setup()
  330. {
  331.   // serial
  332.     Serial.begin(9600);
  333.     Serial.setTimeout(20); // set the timeout, the default is 1 second which is nuts.
  334.  
  335.     ShufflePins();
  336.  
  337.     Serial.println("------------servo pins--------------");
  338.     for(size_t x = 0; x < n; x++)
  339.     {
  340.         Serial.println(servoPins[x]);
  341.     }
  342.  
  343.     Serial.println("------------pixel pins--------------");
  344.     for(size_t y = 0; y < q; y++)
  345.     {
  346.         Serial.println(neoPixelPins[y]);
  347.     }
  348.  
  349.     Strip1.begin();
  350.     Strip2.begin();
  351.     Strip3.begin();
  352.     Strip1.ColorWipe(Strip1.Color(0,0,0),50); // red
  353.     Strip2.ColorWipe(Strip2.Color(0,0,0),50); // red
  354.     Strip3.ColorWipe(Strip3.Color(0,0,0),50); // red
  355.    
  356.     pwm.begin();
  357.     pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
  358.     yield();
  359.  
  360.     Serial.println("set up");
  361.  
  362. }
  363.  
  364. //// GO /////////////////////////////////////////////////////
  365.  
  366. void loop()
  367. {
  368.  
  369.     Strip1.Update();
  370.  
  371.     if (Serial.available() > 0)
  372.     {
  373.         incomingByte = Serial.parseInt(); // use if testing from arduino input
  374.         //incomingByte = Serial.read(); // use if live
  375.         command = incomingByte;
  376.         startServo = ServoGo(command);
  377.        
  378.          
  379.     }
  380.  
  381.     // FLOWER 1 ////////////
  382.  
  383.     if (!endOfGame){
  384.         // if the game is in play
  385.         if (startServo == 11)
  386.         {
  387.             pwm.setPWM(servoPins[0], 0, servoMax);
  388.             Serial.println(servoPins[0]);
  389.             Serial.println(neoPixelPins[0]);
  390.  
  391.             Strip1.ActivePattern = THEATER_CHASE;
  392.             Strip1.Interval = 100;
  393.             Strip1.Color1 = Strip1.Color(100,0,0);        
  394.             Strip1.Color2 = Strip1.Color(100,0,100);
  395.  
  396.            
  397.         }
  398.         else if (startServo == 12)
  399.         {
  400.             pwm.setPWM(servoPins[0], 0, servoMin);
  401.             //Strip1.ActivePattern = COLOR_WIPE;
  402.             //Strip1.Interval = 20;
  403.  
  404.  
  405.         }
  406.  
  407.           else if (startServo == 13)
  408.         {
  409.             pwm.setPWM(servoPins[0], 0, servoMin);
  410.             //Strip1.ActivePattern = RAINBOW_CYCLE;
  411.             //Strip1.Interval = 10;
  412.            
  413.  
  414.         }
  415.  
  416.         // FLOWER 2 ////////////
  417.          
  418.         if (startServo == 21)
  419.         {  
  420.             pwm.setPWM(servoPins[1], 0, servoMax);
  421.             Serial.println(servoPins[1]);
  422.             Serial.println(neoPixelPins[1]);
  423.             Strip2.ActivePattern = THEATER_CHASE;
  424.             Strip2.Interval = 100;
  425.             Strip2.Color1 = Strip1.Color(100,100,0);        
  426.             Strip2.Color2 = Strip1.Color(100,0,0);
  427.            
  428.            
  429.         }
  430.         else if (startServo == 22){
  431.             pwm.setPWM(servoPins[1], 0, servoMin);
  432.            
  433.         }
  434.  
  435.         else if(startServo == 23){
  436.            
  437.             pwm.setPWM(servoPins[1], 0, servoMin);
  438.         }
  439.  
  440.         // FLOWER 3 ////////////////////
  441.  
  442.         if (startServo == 31)
  443.         {
  444.             //Serial.println(startServo);
  445.             pwm.setPWM(servoPins[2], 0, servoMax);
  446.             Serial.println(servoPins[2]);
  447.             Serial.println(neoPixelPins[2]);
  448.             Strip3.ActivePattern = THEATER_CHASE;
  449.             Strip3.Interval = 100;
  450.             Strip3.Color1 = Strip1.Color(100,0,100);        
  451.             Strip3.Color2 = Strip1.Color(0,0,100);
  452.         }
  453.  
  454.         else if (startServo == 32)
  455.         {
  456.             //Serial.println(startServo);
  457.             pwm.setPWM(servoPins[2], 0, servoMin);
  458.         }
  459.  
  460.         else if (startServo == 33)
  461.         {
  462.             //Serial.println(startServo);
  463.             pwm.setPWM(servoPins[2], 0, servoMin);
  464.         }
  465.  
  466.  
  467.     } // end of if endOfGame is false.
  468.  
  469.     if (startServo == 70) {
  470.         endOfGame = true;
  471.         endGame();
  472.        
  473.     }
  474.    
  475.    
  476. } // end of loop
  477.  
  478. //// FUNCTIONS /////////////////////////////
  479.  
  480. int ServoGo(int com)
  481. {
  482.     Serial.println("!inServoGo");
  483.     Serial.println(com);
  484.     return com;
  485. }
  486.  
  487.  
  488.  
  489. void endGame(){
  490.    
  491.     // reset all the pins
  492.     pwm.setPWM(servoPins[0], 0, servoMin);
  493.     pwm.setPWM(servoPins[1], 0, servoMin);
  494.     pwm.setPWM(servoPins[2], 0, servoMin);
  495.    
  496.     // reset all the strips
  497.     Strip1.ActivePattern = COLOR_WIPE;
  498.     Strip1.Interval = 20;
  499.  
  500.     // shuffle the pins
  501.     ShufflePins();
  502.    
  503.     // reset the shit out of everything
  504.     Serial.println("game end, resetting");
  505.     endOfGame = false;
  506.     startServo = 0;
  507.     delay(100);
  508.    
  509. }
  510.  
  511. void ShufflePins(){
  512.   Serial.print("Shuffling pins");
  513.   for (size_t i = 0; i < n - 1; i++)
  514.     {
  515.         //size_t j = random(1, n - i);
  516.         size_t j = i + rand() / (RAND_MAX / (n - i) + 1); // ?? look up how this actually works
  517.         int t = servoPins[i];
  518.         servoPins[i] = servoPins[j];
  519.         servoPins[j] = t;
  520.        
  521.     }
  522.     int wantedpos;
  523.  
  524.     for(size_t x = 0; x < n; x++)
  525.     {
  526.         Serial.println(servoPins[x]);
  527.  
  528.         // find index of servo pin value.
  529.         //pair servo pin to neopixel pin.
  530.  
  531.         if (15 == servoPins[x])
  532.         {
  533.             wantedpos = x;
  534.             neoPixelPins[x] = 11;
  535.         }
  536.         else if(14 == servoPins[x])
  537.         {
  538.             wantedpos = x;
  539.             neoPixelPins[x] = 10;
  540.         }
  541.         else if(13 == servoPins[x])
  542.         {
  543.             wantedpos = x;
  544.             neoPixelPins[x] = 9;
  545.         }
  546.        
  547.  
  548.     }
  549.  
  550. }
  551.  
  552. void StripComplete(){
  553.     //nothing for now
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement