Neopixels interfering with Piezo Sensor

Using the following code I get normal sensor readings from the Piezo Sensor when tapped however as soon as I uncomment out the LED related code the Piezo Sensors stop getting correct readings.

If I run the code to turn on the LEDs then run it again with the LED code commented out leaving the LEDs illuminated I still get interference issues so I have ruled out some sort of electrical interference. Not sure if it is maybe an issue with pins.

#include "FastLED.h"

#define NUM_LEDS 18
#define BRIGHTNESS 5
#define NUM_TARGETS 3

struct target {
  int targetID;
  int sensorPin;
  int sensorReading;
  int threshold;
  bool isHit;
};

target target1;

CRGB target1LEDS[NUM_LEDS];

CLEDController *targetLEDS[NUM_TARGETS];

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  Serial.println("START");
  target1.targetID = 1;
  target1.threshold = 10;
  target1.sensorPin = A5;

//  targetLEDS[0] = &FastLED.addLeds<NEOPIXEL, 11>(target1LEDS, NUM_LEDS);  // Far left

//   for (int l = 0; l <= NUM_LEDS; l += 2) {
//     target1LEDS[l] = CRGB(0, 0, 0);
//   }
//   targetLEDS[0]->showLeds(BRIGHTNESS);
  
//   for (int l = 0; l <= NUM_LEDS; l += 2) {
//     target1LEDS[l] = CRGB(0, 0, 255);
//   }
//   targetLEDS[0]->showLeds(BRIGHTNESS);


  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  //read the sensor and store it in the variable sensorReading:

  target1.sensorReading = analogRead(target1.sensorPin);

  // if the sensor reading is greater than the threshold:
  if (target1.sensorReading > target1.threshold) {

    String stringOne = "Target 1 reading: ";
    String stringThree = stringOne + target1.sensorReading;
    Serial.println(stringThree);
  }  
}

I might be able to help but I need an annotated schematic showing exactly how it is wired. Be sure to include all connections, power, ground and power sources. Note any leads over 25cm/10".

I could be interference or power supply noise or it could be that your sketch is spending too much time updating the LEDs and of course it only reads the piezo for an instant every time through the loop.

Sorry not sure how to create a schematic to upload one

The LED update code only occurs during the Setup

Updating as I have solved the issue for (int l = 0; l <= NUM_LEDS; l += 2) was causing an out of bounds issue overwriting the memory location of target1.sensorPin

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.