Guest User

Sensor-Triggered Comet LEDs

a guest
Mar 6th, 2022
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.39 KB | None | 0 0
  1. /***************************************************
  2.   This is a library for the CAP1188 I2C/SPI 8-chan Capacitive Sensor
  3.  
  4.   Designed specifically to work with the CAP1188 sensor from Adafruit
  5.   ----> https://www.adafruit.com/products/1602
  6.  
  7.   These sensors use I2C/SPI to communicate, 2+ pins are required to
  8.   interface
  9.   Adafruit invests time and resources providing this open source code,
  10.   please support Adafruit and open-source hardware by purchasing
  11.   products from Adafruit!
  12.  
  13.   Written by Limor Fried/Ladyada for Adafruit Industries.
  14.   BSD license, all text above must be included in any redistribution
  15.  ****************************************************/
  16. #include <FastLED.h>
  17. #include <Wire.h>
  18. #include <SPI.h>
  19. #include <Adafruit_CAP1188.h>
  20.  
  21. #define NUM_LEDS  20 // this defines how many LEDs are in each strip
  22. #define LED_PIN 2 // this defines which pin is associated with the name which is LED_PIN
  23. #define LED2_PIN 3 // this defines which pin is associated with the name which is LED_PIN2
  24.  
  25. #define CAP1188_RESET  9
  26.  
  27. #define CAP1188_CS  10
  28.  
  29. Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_RESET);
  30.  
  31. unsigned long lastExecutedMillis = 0;
  32.  
  33.  
  34. //--------------------------------------------------------------------
  35. //STUFF FOR COMET EFFECT
  36. //--------------------------------------------------------------------
  37. CRGB leds [NUM_LEDS]; // this defines our array, names it leds, and calls the number of LEDs defined above
  38. CRGB leds2 [NUM_LEDS]; // this defines our 2ND array, names it leds2, and calls the number of LEDs defined above
  39. byte ledh[NUM_LEDS];
  40. byte ledb[NUM_LEDS];
  41.  
  42.  
  43.  
  44. const int LEDSpeed = 10;
  45. int maxLEDSpeed = 50;    //Identifies the maximum speed of the LED animation sequence
  46. int LEDposition = 0;     //Identifies the LED position in the strip that the comet is currently at. The maximum position is NUM_LEDS-1  (eg. 143)
  47. int oldPosition = 0;     .
  48.  
  49. byte hue = 0;            //Stores the Leading LED's hue value (colour)
  50. byte sat = 255;          //Stores the Leading LED's saturation value
  51. byte tailHue = 0;        //Stores the Comet tail hue value
  52. int tailLength = 8;      //determines the length of the tail.
  53. byte hueRange = 20;      //Colour variation of the tail (greater values have greater variation
  54. byte intensity = 200;    //The default brightness of the leading LED
  55. byte tailbrightness = intensity / 2; //Affects the brightness and length of the tail
  56. int animationDelay = 0;  //The greater the animation delay, the slower the LED sequence. Calculated from LEDSpeed and MaxSpeed.
  57.  
  58. unsigned long waitTime = 60000;  //The wait time for each comet is currently set for 1 minute (or 60000 milliseconds). changing this doesn't appear to affect anything
  59. unsigned long endTime;   //The time when we no longer have to wait for the next comet
  60.  
  61. int cometNumber = 3;     //Used to choose which comet colour to show (***Don't change this variable***)
  62.  
  63.  
  64. void setup() {
  65.   delay (2000);
  66.   FastLED.addLeds <WS2812B, LED_PIN, GRB> (leds, NUM_LEDS); // this adds leds to our program at setup, defines the strip type, the pin associated with it,
  67.   FastLED.addLeds <WS2812B, LED2_PIN, GRB> (leds2, NUM_LEDS); // this adds leds2, THE SECOND STRIP to our program at setup, defines the strip type, the pin associated with it
  68.   selectNextComet();                                                                  //Select the next comet colour
  69.   FastLED.setBrightness (50);
  70.   Wire.begin();
  71.   Serial.begin(9600);
  72.   Serial.println("CAP1188 test!");
  73.  
  74.   // Initialize the sensor, if using i2c you can pass in the i2c address
  75.   // if (!cap.begin(0x28)) {
  76.   if (!cap.begin(0x28)) {
  77.     Serial.println("CAP1188 not found");
  78.     while (1);
  79.   }
  80.   Serial.println("CAP1188 found!");
  81. }
  82.  
  83.  
  84. void loop() {
  85.   uint8_t touched = cap.touched();
  86.  
  87.   if (touched == 0) {
  88.     // No touch detected
  89.     EVERY_N_MILLISECONDS(10) {
  90.       fadeToBlackBy(leds, NUM_LEDS, 10);
  91.       fadeToBlackBy(leds2, NUM_LEDS, 10);
  92.     }
  93.     FastLED.show();
  94.   }
  95.  
  96. // fades all LEDs to black if no sensors are touched
  97.    if (touched == 0) {
  98.   EVERY_N_MILLISECONDS(10) {
  99.    fadeToBlackBy(leds, NUM_LEDS, 10);
  100.   fadeToBlackBy(leds2, NUM_LEDS, 10);
  101.    }
  102.    FastLED.show();
  103.    }
  104.  
  105. // to trigger first LED strip set:
  106.  
  107.   while (touched == 4) {
  108.     showLED(LEDposition, hue, sat, intensity);
  109.  
  110.     //Adjust the hue of the tail so that it is a different colour from the leading LED. To reduce variation in tail colour, reduce the hueRange.
  111.     if (hue > (254 - hueRange)) {
  112.       tailHue = random((hue - hueRange), hue);
  113.     } else {
  114.       tailHue = random(hue, (hue + hueRange));
  115.     }
  116.  
  117.     tailbrightness = random(50, 100);                      //Randomly select the brightness of the trailing LED (provides sparkling tail)
  118.  
  119.  
  120.     leds[LEDposition] = CHSV((tailHue), sat, tailbrightness); //Set the colour, saturation and brightness of the trailing LED
  121.     fadeLEDs1(tailLength);                                  //Fade the tail so that the tail brightness dwindles down to nothingness.
  122.     setDelay(LEDSpeed);                                    //
  123.  
  124.     LEDposition++;
  125.     if (LEDposition > (NUM_LEDS - 1)) {
  126.  
  127.       for (int i = 0; i < 50; i++) {
  128.         showLED(LEDposition, hue, sat, intensity);
  129.         fadeLEDs1(tailLength);
  130.         setDelay(LEDSpeed);
  131.       }
  132.       LEDposition = 0;
  133.       selectNextComet();                                  //Select the next comet colour
  134.     }
  135.     break;
  136.   }
  137.  
  138. // to trigger second LED strip set
  139.  
  140.   while (touched == 1) {
  141.     showLED2(LEDposition, hue, sat, intensity);
  142.  
  143.     //Adjust the hue of the tail so that it is a different colour from the leading LED. To reduce variation in tail colour, reduce the hueRange.
  144.     if (hue > (254 - hueRange)) {
  145.       tailHue = random((hue - hueRange), hue);
  146.     } else {
  147.       tailHue = random(hue, (hue + hueRange));
  148.     }
  149.  
  150.     tailbrightness = random(50, 100);                      //Randomly select the brightness of the trailing LED (provides sparkling tail)
  151.  
  152.  
  153.     leds2[LEDposition] = CHSV((tailHue), sat, tailbrightness); //Set the colour, saturation and brightness of the trailing LED
  154.     fadeLEDs2(tailLength);                                  //Fade the tail so that the tail brightness dwindles down to nothingness.
  155.     setDelay(LEDSpeed);                                    //
  156.  
  157.     LEDposition++;
  158.     if (LEDposition > (NUM_LEDS - 1)) {
  159.  
  160.       for (int i = 0; i < 50; i++) {
  161.         showLED2(LEDposition, hue, sat, intensity);
  162.         fadeLEDs2(tailLength);
  163.         setDelay(LEDSpeed);
  164.       }
  165.       LEDposition = 0;
  166.       selectNextComet();                                  //Select the next comet colour
  167.     }
  168.     break;
  169.   }
  170.  
  171.   for (uint8_t i = 0; i < 100; i++) {
  172.     if (touched & (1 << i)) {
  173.       Serial.print("C"); Serial.print(i + 1); Serial.print("\t");
  174.     }
  175.   }
  176.   Serial.println();
  177. }
  178.  
  179. //===================================================================================================================================================
  180. // showLED() : is used to illuminate the LED strip sets
  181. //===================================================================================================================================================
  182. void showLED(int pos, byte LEDhue, byte LEDsat, byte LEDbright) {
  183.   leds[pos] = CHSV(LEDhue, LEDsat, LEDbright);
  184.   FastLED.show();
  185. }
  186.  
  187. void showLED2(int pos, byte LEDhue, byte LEDsat, byte LEDbright) { // this is for the 2nd strip of LEDs
  188.   leds2[pos] = CHSV(LEDhue, LEDsat, LEDbright);
  189.   FastLED.show();
  190. }
  191.  
  192.  
  193.  
  194. //===================================================================================================================================================
  195. // fadeLEDs(numberhere)(): This function is used to fade the LEDs back to black (OFF) for each strip
  196. //===================================================================================================================================================
  197. void fadeLEDs1(int fadeVal) { // fadeLEDs1 should fade the LEDs in strip bank 1
  198.   for (int i = 0; i < NUM_LEDS; i++) {
  199.     leds[i].fadeToBlackBy( fadeVal );
  200.   }
  201. }
  202.  
  203. void fadeLEDs2(int fadeVal) { // fadeLEDs1 should fade the LEDs in strip bank 1
  204.   for (int i = 0; i < NUM_LEDS; i++) {
  205.     leds2[i].fadeToBlackBy( fadeVal );
  206.   }
  207. }
  208.  
  209.  
  210. //===================================================================================================================================================
  211. // setDelay() : is where the speed of the LED animation sequence is controlled. The speed of the animation is controlled by the LEDSpeed variable.
  212. //              and cannot go faster than the maxLEDSpeed variable.
  213. //===================================================================================================================================================
  214. void setDelay(int LSpeed) {
  215.   animationDelay = maxLEDSpeed - abs(LSpeed);
  216.   delay(animationDelay);
  217. }
  218.  
  219.  
  220. //===================================================================================================================================================
  221. //selectNextComet() : This is where we select either the Blue, the Pink or the White comet
  222. //===================================================================================================================================================
  223. void selectNextComet() {
  224.   cometNumber++;
  225.   if (cometNumber > 3) {
  226.     cometNumber = 1;
  227.   }
  228.  
  229.   switch (cometNumber) {
  230.     case 1:  {    //Blue Comet
  231.         hue = 160;
  232.         sat = 255;
  233.         hueRange = 20;
  234.         break;
  235.       }
  236.  
  237.     case 2:  {   //Pink Comet
  238.         hue = 224;
  239.         sat = 120;
  240.         hueRange = 10;
  241.         break;
  242.       }
  243.  
  244.     default: {   //White Comet
  245.         hue = 0;
  246.         sat = 0;
  247.         hueRange = 0;
  248.         break;
  249.       }
  250.   }
  251. }
  252.  
  253.  
  254. //===================================================================================================================================================
  255. // waitForNextComet() : Is where we either wait for 60 seconds for another comet to come, or initiate another comet with a button press.
  256. //                      The button will only be "ACTIVE" while we are waiting for the next comet. It has no effect while a comet is currently running
  257. //===================================================================================================================================================
  258.  
  259. // blocked out this function as it doesnt seem to do anything
  260.  
  261. //void waitForNextComet() {
  262. //  endTime = millis() + waitTime;
  263.  
  264. //  while (millis() < endTime) {
  265. //    break;
  266. //  }
  267. //}
Advertisement
Add Comment
Please, Sign In to add comment