Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************
- This is a library for the CAP1188 I2C/SPI 8-chan Capacitive Sensor
- Designed specifically to work with the CAP1188 sensor from Adafruit
- ----> https://www.adafruit.com/products/1602
- These sensors use I2C/SPI to communicate, 2+ pins are required to
- interface
- Adafruit invests time and resources providing this open source code,
- please support Adafruit and open-source hardware by purchasing
- products from Adafruit!
- Written by Limor Fried/Ladyada for Adafruit Industries.
- BSD license, all text above must be included in any redistribution
- ****************************************************/
- #include <FastLED.h>
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_CAP1188.h>
- #define NUM_LEDS 20 // this defines how many LEDs are in each strip
- #define LED_PIN 2 // this defines which pin is associated with the name which is LED_PIN
- #define LED2_PIN 3 // this defines which pin is associated with the name which is LED_PIN2
- #define CAP1188_RESET 9
- #define CAP1188_CS 10
- Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_RESET);
- unsigned long lastExecutedMillis = 0;
- //--------------------------------------------------------------------
- //STUFF FOR COMET EFFECT
- //--------------------------------------------------------------------
- CRGB leds [NUM_LEDS]; // this defines our array, names it leds, and calls the number of LEDs defined above
- CRGB leds2 [NUM_LEDS]; // this defines our 2ND array, names it leds2, and calls the number of LEDs defined above
- byte ledh[NUM_LEDS];
- byte ledb[NUM_LEDS];
- const int LEDSpeed = 10;
- int maxLEDSpeed = 50; //Identifies the maximum speed of the LED animation sequence
- 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)
- int oldPosition = 0; .
- byte hue = 0; //Stores the Leading LED's hue value (colour)
- byte sat = 255; //Stores the Leading LED's saturation value
- byte tailHue = 0; //Stores the Comet tail hue value
- int tailLength = 8; //determines the length of the tail.
- byte hueRange = 20; //Colour variation of the tail (greater values have greater variation
- byte intensity = 200; //The default brightness of the leading LED
- byte tailbrightness = intensity / 2; //Affects the brightness and length of the tail
- int animationDelay = 0; //The greater the animation delay, the slower the LED sequence. Calculated from LEDSpeed and MaxSpeed.
- 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
- unsigned long endTime; //The time when we no longer have to wait for the next comet
- int cometNumber = 3; //Used to choose which comet colour to show (***Don't change this variable***)
- void setup() {
- delay (2000);
- 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,
- 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
- selectNextComet(); //Select the next comet colour
- FastLED.setBrightness (50);
- Wire.begin();
- Serial.begin(9600);
- Serial.println("CAP1188 test!");
- // Initialize the sensor, if using i2c you can pass in the i2c address
- // if (!cap.begin(0x28)) {
- if (!cap.begin(0x28)) {
- Serial.println("CAP1188 not found");
- while (1);
- }
- Serial.println("CAP1188 found!");
- }
- void loop() {
- uint8_t touched = cap.touched();
- if (touched == 0) {
- // No touch detected
- EVERY_N_MILLISECONDS(10) {
- fadeToBlackBy(leds, NUM_LEDS, 10);
- fadeToBlackBy(leds2, NUM_LEDS, 10);
- }
- FastLED.show();
- }
- // fades all LEDs to black if no sensors are touched
- if (touched == 0) {
- EVERY_N_MILLISECONDS(10) {
- fadeToBlackBy(leds, NUM_LEDS, 10);
- fadeToBlackBy(leds2, NUM_LEDS, 10);
- }
- FastLED.show();
- }
- // to trigger first LED strip set:
- while (touched == 4) {
- showLED(LEDposition, hue, sat, intensity);
- //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.
- if (hue > (254 - hueRange)) {
- tailHue = random((hue - hueRange), hue);
- } else {
- tailHue = random(hue, (hue + hueRange));
- }
- tailbrightness = random(50, 100); //Randomly select the brightness of the trailing LED (provides sparkling tail)
- leds[LEDposition] = CHSV((tailHue), sat, tailbrightness); //Set the colour, saturation and brightness of the trailing LED
- fadeLEDs1(tailLength); //Fade the tail so that the tail brightness dwindles down to nothingness.
- setDelay(LEDSpeed); //
- LEDposition++;
- if (LEDposition > (NUM_LEDS - 1)) {
- for (int i = 0; i < 50; i++) {
- showLED(LEDposition, hue, sat, intensity);
- fadeLEDs1(tailLength);
- setDelay(LEDSpeed);
- }
- LEDposition = 0;
- selectNextComet(); //Select the next comet colour
- }
- break;
- }
- // to trigger second LED strip set
- while (touched == 1) {
- showLED2(LEDposition, hue, sat, intensity);
- //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.
- if (hue > (254 - hueRange)) {
- tailHue = random((hue - hueRange), hue);
- } else {
- tailHue = random(hue, (hue + hueRange));
- }
- tailbrightness = random(50, 100); //Randomly select the brightness of the trailing LED (provides sparkling tail)
- leds2[LEDposition] = CHSV((tailHue), sat, tailbrightness); //Set the colour, saturation and brightness of the trailing LED
- fadeLEDs2(tailLength); //Fade the tail so that the tail brightness dwindles down to nothingness.
- setDelay(LEDSpeed); //
- LEDposition++;
- if (LEDposition > (NUM_LEDS - 1)) {
- for (int i = 0; i < 50; i++) {
- showLED2(LEDposition, hue, sat, intensity);
- fadeLEDs2(tailLength);
- setDelay(LEDSpeed);
- }
- LEDposition = 0;
- selectNextComet(); //Select the next comet colour
- }
- break;
- }
- for (uint8_t i = 0; i < 100; i++) {
- if (touched & (1 << i)) {
- Serial.print("C"); Serial.print(i + 1); Serial.print("\t");
- }
- }
- Serial.println();
- }
- //===================================================================================================================================================
- // showLED() : is used to illuminate the LED strip sets
- //===================================================================================================================================================
- void showLED(int pos, byte LEDhue, byte LEDsat, byte LEDbright) {
- leds[pos] = CHSV(LEDhue, LEDsat, LEDbright);
- FastLED.show();
- }
- void showLED2(int pos, byte LEDhue, byte LEDsat, byte LEDbright) { // this is for the 2nd strip of LEDs
- leds2[pos] = CHSV(LEDhue, LEDsat, LEDbright);
- FastLED.show();
- }
- //===================================================================================================================================================
- // fadeLEDs(numberhere)(): This function is used to fade the LEDs back to black (OFF) for each strip
- //===================================================================================================================================================
- void fadeLEDs1(int fadeVal) { // fadeLEDs1 should fade the LEDs in strip bank 1
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i].fadeToBlackBy( fadeVal );
- }
- }
- void fadeLEDs2(int fadeVal) { // fadeLEDs1 should fade the LEDs in strip bank 1
- for (int i = 0; i < NUM_LEDS; i++) {
- leds2[i].fadeToBlackBy( fadeVal );
- }
- }
- //===================================================================================================================================================
- // setDelay() : is where the speed of the LED animation sequence is controlled. The speed of the animation is controlled by the LEDSpeed variable.
- // and cannot go faster than the maxLEDSpeed variable.
- //===================================================================================================================================================
- void setDelay(int LSpeed) {
- animationDelay = maxLEDSpeed - abs(LSpeed);
- delay(animationDelay);
- }
- //===================================================================================================================================================
- //selectNextComet() : This is where we select either the Blue, the Pink or the White comet
- //===================================================================================================================================================
- void selectNextComet() {
- cometNumber++;
- if (cometNumber > 3) {
- cometNumber = 1;
- }
- switch (cometNumber) {
- case 1: { //Blue Comet
- hue = 160;
- sat = 255;
- hueRange = 20;
- break;
- }
- case 2: { //Pink Comet
- hue = 224;
- sat = 120;
- hueRange = 10;
- break;
- }
- default: { //White Comet
- hue = 0;
- sat = 0;
- hueRange = 0;
- break;
- }
- }
- }
- //===================================================================================================================================================
- // waitForNextComet() : Is where we either wait for 60 seconds for another comet to come, or initiate another comet with a button press.
- // The button will only be "ACTIVE" while we are waiting for the next comet. It has no effect while a comet is currently running
- //===================================================================================================================================================
- // blocked out this function as it doesnt seem to do anything
- //void waitForNextComet() {
- // endTime = millis() + waitTime;
- // while (millis() < endTime) {
- // break;
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment