Having trouble operating sensors and corresponding LED strips independently

Hello everyone! I’m working on a project where I’m installing addressable LED strips inside a drum set for live performance use, but I’m having trouble getting the code to function exactly as I would like.

The LED strips are triggered by piezo sensors attached to each drum head (which are functioning perfectly). For purposes of simplicity here, I have attached the code necessary to control only two drums, as adding more would just require more reading and not provide any new information.

I have already built the entire system using Arduino UNO, and technically it works. However, I’m having a hard time figuring out how to structure multiple segments of code so that they appear to run simultaneously. Basically, what happens right now is that if I hit two drums too closely together in time, only the first one will light up because the code is still inside that IF conditional and not looking for input.

I apologize if my code is a bit convoluted, but I’ve tried my best to label everything and provide explanations. My goal is that multiple drums can be lit up simultaneously—I’m just not quite sure how to do that.

Thanks for your time!

2DrumsV5.ino (2.29 KB)

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;
  }
}

FYI... FastLED has a function fadeToBlackBy( leds, NUM_LEDS, 20); which basically does what you are trying to do

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!

ccoris:
Thank you for your help! The code you attached worked (I did adapt a few things), and now the system is working perfectly!

Glad it worked out for you!