My good friend Tom is helping me with the coding aspect. He thought he responded already, but he can explain it better than me. Mee, I can give you debug info, but from the code your seeing something just isn’t clicking. I have simulated my system with 2 RGB LED pins, and with the code I’m getting continuous white light. No pulsation or different brightnesses. Why? If we were trying to get the top 40% of the magnitudes, wouldn’t logMode simulate that? Or the numbers are too similiar that that explains why it’s almost white light. Here’s some code from our last test and debug output.
And hey right now I feel ya on those issues. Your car issues, my ledstrip issues. It’s been over 16 days, they have be “rejected” from airport security twice. The company lost my tracking number, username and password, and no one will respond to my last ticket. Sounds like I got scammed off http://www.dx.com
#include <math.h>
#include <SoftPWM.h>
enum audio
{
MONO,
RIGHT,
LEFT
};
// Constants
const boolean logMode = true; // enable or disable log mode display
const boolean debug = true; // enable or disable debug measurements and printouts
const int audio = MONO; // audio mode
const int noise[] =
{0, 0, 0, 0, 0, 0, 0}; // set this to magnitude of noise from shield
const unsigned long loop_delay = 200; // loop delay to slow down display update rate
// Pin assignments
const byte analogPinL = 0; // left channel analog data from shield
const byte analogPinR = 1; // right channel analog data from shield
const byte strobePin = 4; // data strobe for shield
const byte resetPin = 5; // reset strobe for shield
const byte ledPins[6] = {2,3,6,9,10,11}; // LEDs
int spectrumReadR; // R magnitude from shield
int spectrumReadL; // L magnitude from shield
int mag[] =
{0.0,0.0,0.0,0.0,0.0,0.0,0.0}; // magnitudes of 7 freq band
int logMag; // magnitude for logMode
int logNoise[] =
{0,0,0,0,0,0,0}; // noise values for logMode
int brightnessVal; // 8 bit PWM value used to control brightness of LED
void setup() {
// See http://arduino.cc/en/reference/serial for information about reading/writing through the serial port on the Arduino. Used for debugging purposes.
Serial.begin(9600);
// Initialize the pins
pinMode(analogPinR, INPUT);
pinMode(analogPinL, INPUT);
pinMode(resetPin, OUTPUT);
pinMode(strobePin, OUTPUT);
for (int i = 0; i < 6; i++)
{
pinMode(ledPins[i], OUTPUT);
}
// Set the initial states
digitalWrite(resetPin, LOW);
digitalWrite(strobePin, LOW);
for (int i = 0; i < 6; i++)
{
digitalWrite(ledPins[i], LOW);
}
// Initialize SoftPWM
SoftPWMBegin();
// Set analogPin's reference voltage (not sure if necessary)
analogReference(DEFAULT); // 5V
// Calculate noise levels for log mode
if (logMode)
{
for (int i = 0; i <= 6; ++i)
{
logNoise[i] = 20.0*log10(noise[i]);
}
}
// Initialize spectrum analyzer
digitalWrite(resetPin,HIGH);
delayMicroseconds(5);
digitalWrite(strobePin,HIGH);
delayMicroseconds(50); // strobe PW > 18 usec min
digitalWrite(strobePin,LOW);
delayMicroseconds(5); // reset PW > 100 nsec min
digitalWrite(resetPin,LOW);
delayMicroseconds(5);
digitalWrite(strobePin,HIGH);
delayMicroseconds(100); // reset to strobe falling > 72 usec min
}
void loop() {
for(byte band = 1; band <= 7; band++){
// Bring strobe low to switch shield output to next band
digitalWrite(strobePin,LOW);
delayMicroseconds(40); // 36 usec min for output to settle
// Data is now available on analog pins 0 and 1
spectrumReadR = analogRead(analogPinR);
spectrumReadL = analogRead(analogPinL);
if(debug){
Serial.print("Right chan reads ");
Serial.print(spectrumReadR);
Serial.print(" for band ");
Serial.println(band);
Serial.print("Left chan reads ");
Serial.print(spectrumReadL);
Serial.print(" for band ");
Serial.println(band);
}
// Now set strobe back to high
digitalWrite(strobePin, HIGH);
// Combine L/R data as dictated by setting
switch (audio) {
case MONO:
// this averages the L & R readings
mag[band-1] = (spectrumReadL + spectrumReadR)/2;
break;
case RIGHT:
// this sets the magnitude = R data
mag[band-1] = spectrumReadR;
break;
case LEFT:
// this sets the magnitude = L data
mag[band-1] = spectrumReadL;
break;
default:
// this sets the magnitude = L data
mag[band-1] = spectrumReadL;
break;
}
if(debug){
Serial.print("Magnitude is ");
Serial.println(mag[band-1]);
}
// logMode - determines whether the volume-to-brightness ratio follows a linear or logarithmic scale
if(logMode){
// Convert to 20log of input
logMag = 20.0*log10(mag[band-1]);
if(debug){
Serial.print("Using log mode. New magnitude is ");
Serial.print(logMag);
Serial.print(" with noise value of ");
Serial.println(logNoise[band-1]);
}
// Map range of [noise]-60 to brightness value of 0-255
brightnessVal = map(logMag, logNoise[band-1], 60, 0, 255);
}
else{
// Map range of [noise]-1023 to brightness value of 0-255
brightnessVal = map(mag[band-1], noise[band-1], 1023, 0, 255);
}
// Ensure brightnessVal is 8 bit value
brightnessVal = constrain(brightnessVal,0,255);
if(debug){
Serial.print("Brightness value is ");
Serial.print(brightnessVal);
Serial.print(" for band ");
Serial.println(band);
}
SoftPWMSet(ledPins[band-1], brightnessVal);
// Output PWM to LED
// if (band < 7) // ignore 7th band
// {
// switch (ledPins[band-1]) {
// case 2:
// case 4:
// case 7:
// case 8:
// case 12: // SoftPWM pins
// SoftPWMSet(ledPins[band-1],brightnessVal);
// break;
// case 3:
// case 5:
// case 6:
// case 9:
// case 10:
// case 11: // Normal PWM pins
// analogWrite(ledPins[band-1],brightnessVal);
// break;
// default: // Invalid pin
// if (debug)
// {
// Serial.print("Pin value ");
// Serial.print(ledPins[band-1]);
// Serial.println(" is invalid.");
// }
// break;
// }
// }
}
if(debug){
Serial.println();
}
// loop_delay - adjust this variable so that the time it takes to execute this loop() code matches up with the delay of updating the LED brightness (done via trial and error)
delay(loop_delay);
}