Sound Reactive Lightstrip Code Problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
fOAMYbEER
 
Posts: 4
Joined: Thu Jun 12, 2014 9:02 pm

Sound Reactive Lightstrip Code Problem

Post by fOAMYbEER »

Hello all

Right the introductions first...im brand new to both Arduino programing and c++ in general..but I have some limited programming experience in other launguages.
Im not asking anyone to write code for me as im thoroughly enjoying figuring out how to do things but the code examples I have found elsewhere are far above my level of knowlege so I thought id ask here(fingers crossed) :)

My Project:
Im trying to make a sound reactive strip using an Arduinio Uno and a NeoPixel lightstrip..that sends a pulse of light down the strip whenever the volume threshold is exceeded (Beat detection )

My Problem:
So far so good.. I have my Mic hooked up and the code running and it reacts just fine(when sending single light pulses)...but It wont send another pulse of light down the strip till the last one has gone off the end and the loop resets.

Ive tried to modify the FIREWALKER code to achieve this but I just cant seem to get it to work, I guess I don't understand enough about the code at this time.
I know I have to store the triggered pulses into memory and then update them all as they all move down the strip....Help!!!

What Im after: (TLDR to the point)

Can anyone give me a simple example of sending multiple pulses of light down a strip in response to an analogue input!

i.e if micTrigger>someValue ...send pulse.....(Even if another pulse has been sent already)

Many thanks in advance
Last edited by fOAMYbEER on Mon Jun 16, 2014 11:15 am, edited 1 time in total.

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: Sound Reactive Lightstrip Code Problem

Post by Franklin97355 »

There may be something to help here... https://learn.adafruit.com/led-ampli-tie you need to write your code so the check for input happens more often which will change the way you send the pulse down the string. What code are you using now please post it using code button </> above the reply box.

fOAMYbEER
 
Posts: 4
Joined: Thu Jun 12, 2014 9:02 pm

Re: Sound Reactive Lightstrip Code Problem

Post by fOAMYbEER »

Hiya
Here is the code I have cobbled together so far..

Code: Select all

//Sound reactive led sketch
//Mic Sampling Code By Adafruit Industries
//Pulse tracking code adapted from Firewalker Sketch By Adafruit Industries

#include <Adafruit_NeoPixel.h>

#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);

#define MAXSTEPS 10 // Process (up to) this many concurrent pulses down the lightstrip 
                          //...used for the memory bit

int i=0;  // initial value of position of pulse
int sampleWindow = 50;  // Sample window width in mS (50 mS = 20Hz)
int micAnalogPin = 0; // mic is connected to analog 0
int micReading;      // the analog reading from the microphone
int stripLength = 30; //no of pixels in strip
int micTriggerLevel = 0.4; //trigger level of mic in volts
///////////////////////////////////////////////////////////////////////////////////////
int stepX[MAXSTEPS];    // Position of 'step wave' along strip
int stepFiltered;       // Current filtered pressure reading
int stepMin;            // Minimum reading during current step

uint8_t
  stepNum = 0;       // Current step number in stepX tables
  
boolean
  stepping  = false;  // If set, step was triggered, waiting to release

////////////////////////////////////////////////////////////////////////////////////
void setup() {
 
  Serial.begin(9600); //Used to debug the mic output
 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

///////// Clear pulse position buffers//////
    memset(stepX  , 0, sizeof(stepX));
//////////////////////////////////////////////

}

void loop() {
 //////////////////////////////////////////////////////////////////////////////////////////////////////    
  //Begin Mic Sampleing
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level
 
   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;
 
   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      micReading = analogRead(micAnalogPin);
      if (micReading < 1024)  // toss out spurious readings
      {
         if (micReading > signalMax)
         {
            signalMax = micReading;  // save just the max levels
         }
         else if (micReading < signalMin)
         {
            signalMin = micReading;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 3.3) / 1024;  // convert to volts
 
   Serial.println(volts); //  debugging tool
 ////////////////////////////////////////////////////////////////////////////////////

// add pulses to memory

uint8_t i;   ///
 
  // Read analog input volts in this case from microphone
  stepFiltered = (volts);
 
   //  there could be multiple step animations
  // 'in flight,' so a short list is kept.
  
  // this bit checks to see if step has been triggered
  if(stepping) { 
    // If a step was previously triggered...ie stepping = true
    if(stepFiltered < micTriggerLevel) { // Has sound let up? is it now less presasure than trigger level
      stepping = false;                   // Yep! boolean set to false again
           
      //otherwise 
      // Add new step to the step list (may be multiple in flight)
      
      stepX[stepNum]   = 0; // Position starts at beginning, moves forward position of Stepnum e.g 6
      
      if(++stepNum >= MAXSTEPS) stepNum = 0; // If many, overwrite oldest
} 
  
   } else if(stepFiltered < micTriggerLevel) { // No step yet; watch for trigger 
    
   stepping = true;         // Got one!
   
}
   
////////////////////////////////////////////////////////////////////////////////////  
   
//Get coloured pixel moving
 
  // memset(stepX  , 0, sizeof(stepX));// Clear position  buffer
  for(i=0; i<MAXSTEPS; i++) {     // For each step move one position i++
   
    stepX[stepNum]++; // Update position of every step wave
      
  }
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // send green pixel down
    strip.setPixelColor(i, 0, 55, 0);
    strip.show();
 
  delayMicroseconds(150);

}// end








User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Sound Reactive Lightstrip Code Problem

Post by adafruit_support_mike »

It loosk like you've run into the all-too-common problem of one neat idea creating problems elsewhere in the code.

I this case, the neat idea is the circular buffer for active steps:

Code: Select all

      if(++stepNum >= MAXSTEPS) stepNum = 0; // If many, overwrite oldest
Circular buffers are brain-candy, but you have to write your code carefully to make them behave. I started writing some of that code myself before I stopped to define the problem, and realized there's an easier way to store the information you need.

An LED chase pattern is basically a visible shift buffer, and shift buffers map directly to bits in an integer. You only have 30 pixels in the strip, so you can store everything you need to know in a single 32-bit number:

Code: Select all


#define NUM_PIXELS  30

uint32_t shiftBuffer = 0;

//  add a new pulse to the pattern:
shiftBuffer |= 1;

//  advance the whole pattern one step:
shiftBuffer <<= 1;

//  display the pattern
uint8_t color;

for ( i=0 ; i < NUM_PIXELS ; i++ ) {
    color = (( shiftBuffer >> i ) & 1 ) ? 55 : 0;
    strip.setPixelColor( i, 0, color, 0 );
}
strip.show();
Bits automatically fall off the end of shiftBuffer, so you don't have to worry about keeping track of index values and limits.

Whenever possible, try to reduce your program logic to bit manipulations or integer math. It pushes all the housekeeping down onto the hardware, which has already been designed to Do The Right Thing. Math and logic also provide rich languages for manipulating patterns, so you can get a whole lot of power from very few lines of code.

fOAMYbEER
 
Posts: 4
Joined: Thu Jun 12, 2014 9:02 pm

Re: Sound Reactive Lightstrip Code Problem

Post by fOAMYbEER »

Many thanks for helping me,and for the advice..I think I'm getting somewhere again now :)

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Sound Reactive Lightstrip Code Problem

Post by adafruit_support_mike »

Glad to help. ;-)

Shoving the important behavior of a program down into things like bitmaps and lookup tables is a powerful technique. A dozen bytes of brute-force pattern storage can replace a hundred lines of code.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”