Code modification by Gary for LED Effects AIO;
I modified your code to use on an Adafruit Trinket MO microcontroller where I use capacitive touch to toggle between the different effects (code below).
One key point to note is the value used for the internal resister setting. (RESISTOR_20K). Set too low and ambient light can toggle the switch. Set too high and it struggles to detect your touch.
Hope this compliments your awesome work in some way.
End result here:
https://www.youtube.com/watch?v=0WxDgiRM9g8
#include <Adafruit_NeoPixel.h>
#include <Adafruit_FreeTouch.h>
#include <Adafruit_DotStar.h>
#define PIN 3
#define PIN2 4
#define NUM_LEDS 111
#define NUM_LEDS2 28
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic ‘v1’ (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)TrinketMO1976!@3
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel innerring = Adafruit_NeoPixel(NUM_LEDS2, PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_DotStar dotStar = Adafruit_DotStar(1, INTERNAL_DS_DATA, INTERNAL_DS_CLK, DOTSTAR_BGR);
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A0, OVERSAMPLE_4, RESISTOR_20K, FREQ_MODE_NONE);
int x = 0;
void setup() {
//Serial.begin(115200);
//while (!Serial);
// Serial.println(“FreeTouch test”);
// if (! qt_1.begin())
//Serial.println(“Failed to begin qt on pin A0”);
dotStar.begin(); // Initialize pins for output
dotStar.show(); // Turn dotstar off ASAP
strip.begin();
innerring.begin();
strip.show(); // Initialize all pixels to ‘off’
innerring.show(); // Initialize all pixels to ‘off’
// Initialize A0 as a touch sensor
//Serial.begin(115200);
qt_1.begin();
//while (!Serial);
//Serial.println(“FreeTouch test”);
// initialize digital pin LED_BUILTIN as an output.
//pinMode(LED_BUILTIN, OUTPUT);
//if (! qt_1.begin());
//Serial.println(“Failed to begin qt on pin A0”);
}
// REPLACE FROM HERE
void loop() {
// —> here we call the effect function <—
int counter, result = 0;
// DIY
//Serial.println(“\n*************************************”);
counter = millis();
result = qt_1.measure();
//Serial.print(“QT 1: “); Serial.print(result);
//Serial.print(” (“); Serial.print(millis() – counter); Serial.println(” ms)”);
if (result > 1000) {
x++;
strip.begin();
strip.show();
delay(1000);
}
//Serial.print(“x value: “); Serial.print(x);
if (x == 0) {
colorWipe(100, 100, 100, 70);
} else if (x == 1) {
//theaterChase(0,165,255,40);
meteorRain(0, 165, 255, 10, 64, true, 30);
//Twinkle(100, 100, 100, 10, 100, false);
} else if (x == 2) {
theaterChase(100, 100, 100, 10);
} else if (x == 3) {
strip.begin();
strip.show();
colorWipe(255, 255, 0, 70);
} else if (x == 4) {
theaterChase(255, 185, 100, 40);
} else if (x == 5) {
theaterChase(255, 185, 100, 10);
} else if (x == 6) {
strip.begin();
strip.show();
colorWipe(255, 0, 0, 70);
} else if (x == 7) {
theaterChase(255, 0, 0, 40);
} else if (x == 8) {
theaterChase(255, 0, 0, 10);
} else if (x == 9) {
strip.begin();
strip.show();
colorWipe(0, 0, 0, 0);
} else if (x > 9) {
x = 0;
}
}
void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
setAll(0, 0, 0);
for (int i = 0; i < Count; i++) {
setPixel(random(NUM_LEDS), red, green, blue);
showStrip();
delay(SpeedDelay);
if (OnlyOne) {
setAll(0, 0, 0);
}
}
delay(SpeedDelay);
}
void theaterChase(byte red, byte green, byte blue, int SpeedDelay) {
for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 4) {
setPixel(i + q, red, green, blue); //turn every third pixel on
}
showStrip();
delay(SpeedDelay);
for (int i = 0; i < NUM_LEDS; i = i + 4) {
setPixel(i + q, 0, 0, 0); //turn every third pixel off
}
}
}
}
void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
for (uint16_t i = 0; i < NUM_LEDS2; i++) {
setPixelRing(i, red, green, blue);
showRing();
delay(SpeedDelay);
}
}
void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
setAll(0, 0, 0);
for (int i = 0; i < NUM_LEDS + NUM_LEDS; i++) {
// fade brightness all LEDs one step
for (int j = 0; j < NUM_LEDS; j++) {
if ((!meteorRandomDecay) || (random(10) > 5)) {
fadeToBlack(j, meteorTrailDecay);
}
}
// draw meteor
for (int j = 0; j < meteorSize; j++) {
if ((i - j < NUM_LEDS) && (i - j >= 0)) {
setPixel(i - j, red, green, blue);
}
}
showStrip();
delay(SpeedDelay);
}
}
void fadeToBlack(int ledNo, byte fadeValue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
uint32_t oldColor;
uint8_t r, g, b;
int value;
oldColor = strip.getPixelColor(ledNo);
r = (oldColor & 0x00ff0000 UL) >> 16;
g = (oldColor & 0x0000ff00 UL) >> 8;
b = (oldColor & 0x000000ff UL);
r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);
strip.setPixelColor(ledNo, r, g, b);
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[ledNo].fadeToBlackBy(fadeValue);
#endif
}
// —> here we define the effect function <—
// REPLACE TO HERE
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void showRing() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
innerring.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setPixelRing(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
innerring.setPixelColor(Pixel, innerring.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
}
showStrip();
}
void setAllRing(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++) {
setPixelRing(i, red, green, blue);
}
showRing();
}