ToddL1962:
Try this. I know it compiles but I can't test it.
For each drum one iteration of the fade is executed IF the drum has been hit (i.e. the sensor reading moves from below the threshold to above the threshold). Once the fade counter decrements to < 0 it stops fading. If the drum is hit while it is fading then the fade starts over.
#include <FastLED.h>
const int LED_PIN12 = 13;
const int NUM_LEDS12 = 28; //number of LEDS inside the 12" tom drum
const int LED_PIN16 = 12;
const int NUM_LEDS16 = 36; //number of LEDS inside the 16" floor tom drum
CRGB leds12[NUM_LEDS12];
CRGB leds16[NUM_LEDS16];
const int Sensor12 = A0; //piezo connected to 12" tom
const int Sensor16 = A1; //piezo connected to 16" floor tom
const int threshold = 430; // threshold value to decide when the detected vibration should trigger lights
int fade12cnt = -1;
int fade16cnt = -1;
void setup() {
FastLED.addLeds<WS2812, LED_PIN12, GRB>(leds12, NUM_LEDS12);
FastLED.addLeds<WS2812, LED_PIN16, GRB>(leds16, NUM_LEDS16);
}
void setPixel12(int Pixel, byte red, byte green, byte blue) {
leds12[Pixel].r = red;
leds12[Pixel].g = green;
leds12[Pixel].b = blue;
}
void setAll12(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS12; i++) {
setPixel12(i, red, green, blue);
}
FastLED.show();
}
void setPixel16(int Pixel, byte red, byte green, byte blue) {
leds16[Pixel].r = red;
leds16[Pixel].g = green;
leds16[Pixel].b = blue;
}
void setAll16(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS16; i++) {
setPixel16(i, red, green, blue);
}
FastLED.show();
}
void loop() {
static int prevSensorReading12 = 0;
static int prevSensorReading16 = 0;
int sensorReading12 = analogRead(Sensor12);
int sensorReading16 = analogRead(Sensor16); // read the sensor and store it in the variable sensorReading:
//
// Restart fade every time sensor reading crosses threshold
//
if (sensorReading12 >= threshold && prevSensorReading12 < threshold) {
fade12cnt = 255;
}
prevSensorReading12 = sensorReading12;
if (sensorReading16 >= threshold && prevSensorReading16 < threshold) {
fade16cnt = 255;
}
prevSensorReading16 = sensorReading16;
//
// Fade iteration
//
if (fade12cnt >= 0)
{
setAll12(0, fade12cnt, 0);
fade12cnt = fade12cnt-3;
}
if (fade16cnt >= 0)
{
setAll16(0, fade16cnt, 0);
fade16cnt = fade16cnt-3;
}
}
Thank you for your help! The code you attached worked (I did adapt a few things), and now the system is working perfectly!