Go Down

Topic: Fix my code? (Read 2 times) previous topic - next topic

techfreek

Feb 20, 2011, 08:14 pm Last Edit: Feb 20, 2011, 10:20 pm by techfreek Reason: 1
Hello!

I am working on code to analyze 6 frequencies of audio, and then turn that info into different brightnesses of led light. my setup involves an arduino uno, http://bliptronics.com/item.aspx?ItemID=116Blips stereo audio analyzer(takes the load off the arduino, and will make coding easier) and finally 2 RGB amplifiers from usLEDsupply to power 2 strips of RGB leds. (so total of 6 total channels) here is my code... I am willing to compensate you for what you think is reasonable

THANK YOU!

P.S. my code currently puts up a nice light bluish color(color doesn't matter, just that it puts up light with no music being played) and doesn't actually analyze the music... I started to add a feature that would fade out the lights if no music was playing... but it doesn't want to work... it compiles.... but the light blue does not fade...
Code: [Select]

int SpectrumLeft[6];
int ledintensity[6];
int ledintensity2[6];
int ledintensity3[6];
int analyze1[6];
int analyze2[6];
int fadespeed;
byte LED;
byte LED2;
byte spect;
byte db;

int led6 = 6;
int led7 = 7;
int led8 = 8;
int led9 = 9;
int led10 = 10;
int led11 = 11;

//have a fade out based on whether or not the music is playing use this for ideas http://arduino.cc/en/Tutorial/Calibration
//maybe something like run analyze levels, wait, analyze again, if they are same, fade (another function)

void setup () {
   pinMode(led6,OUTPUT);
   pinMode(led7,OUTPUT);
   pinMode(led8,OUTPUT);
   pinMode(led9,OUTPUT);
   pinMode(led10,OUTPUT);
   pinMode(led11,OUTPUT);
}
//Add a for loop so animation is smoother http://www.arduino.cc/en/Reference/For (this is all the basic code that the anaylzer needs)
void readSpectrum() {
 // Band 0 = Lowest Frequencies.
 byte Band;
 for(Band=0;Band <6; Band++){
   SpectrumLeft[Band] = analogRead(0); //left
   digitalWrite(4,HIGH);  //Strobe pin on the shield
   digitalWrite(4,LOW);    
 }
}

void same() {
 for(LED2=6; LED2 <12; LED2++){
     ledintensity2[LED2]=analyze1[LED2];
     delay(1);
     ledintensity2[LED2]=analyze2[LED2];
     if (analyze1[LED2]==analyze2[LED2]) {
       fadespeed = 75;
     }
      else {
       fadespeed = 0;
     }
  }
}

void fade() {
 if (fadespeed > 0) {
  for(LED2=6; LED2<12; LED2++)
   ledintensity2[LED2] - fadespeed;
 }
}

void loop () {
 for(LED=6; LED <12; LED++){
   for(spect=0; spect <7; spect++){
       for(LED2=6; LED2 <12; LED2++){
         readSpectrum();`
         ledintensity2[LED2]=(ledintensity[LED]+(25*SpectrumLeft[spect])); //this should in theory add the data of the analyzer to the base value for the lights, then send to the output the correct light level
         fade();
       }
     }
 }

 
   analogWrite(led6, ledintensity2[6]);
   analogWrite(led7, ledintensity2[7]);
   analogWrite(led8, ledintensity2[8]);
   analogWrite(led9, ledintensity2[9]);
   analogWrite(led10, ledintensity2[10]);
   analogWrite(led11, ledintensity2[11]);
}

Simpson_Jr

#1
Feb 20, 2011, 11:51 pm Last Edit: Feb 21, 2011, 12:03 am by Simpson_Jr Reason: 1
I don't know Blips, unfortunately can't help you with that.

There's one thing, Analogwrite uses PWM.
Two of the pins you used,  7 and 8, are not PWM-pins.

Quote
int led7 = 7;
int led8 = 8;


With a mega-board you can use pins 2-13, but the AnalogWrite-function should... only work on pins 3,5,6,9,10 and 11 with an Uno (or other 48/88/168/328-based board).

You can change your program to
int led7 = 3;
int led8 = 5;
and use the right pins on board accordingly.

I don't know whether this will solve the... problem, but I hope it's a step in the right direction ;-)


techfreek

Yes, it was a step in the right direction... still trying to figure out the control via music... Thank you for your help!

brucethehoon

Take a look at sparkfun.com and their latest product post.   It seems to be exactly what you're tying to do.  If nothing else perhaps you will get ideas!

techfreek


Take a look at sparkfun.com and their latest product post.   It seems to be exactly what you're tying to do.  If nothing else perhaps you will get ideas!

I saw that! It is pretty much exactly what I am going for! but... its more of a mobile version. I looked at the source code to see if it could help at all, but its not based on Arduino at all, so no luck :( thank you though!

brucethehoon

It looks to me like you're not initializing the spectrum Analyzer. 

Insert into the beginning:
Code: [Select]
int spectrumReset=5;
int spectrumStrobe=4;
int spectrumAnalog=0;  //0 for left channel, 1 for right.

Insert into your setup:
  pinMode(spectrumReset, OUTPUT);
  pinMode(spectrumStrobe, OUTPUT);

  //Init spectrum analyzer
  digitalWrite(spectrumStrobe,LOW);
    delay(1);
  digitalWrite(spectrumReset,HIGH);
    delay(1);
  digitalWrite(spectrumStrobe,HIGH);
    delay(1);
  digitalWrite(spectrumStrobe,LOW);
    delay(1);
  digitalWrite(spectrumReset,LOW);
    delay(5);


It appears that these pulses will tell the unit to startup and start monitoring the incoming audio signal. 

Let me know if this doesn't do the trick. 

techfreek

Thank you! I think that is closer to the right track, I am now getting an error from the first line of the code saying (no matter what it is) expected constructor, destructor, or type conversion before 'void' Any clue what this means?

Maybe I put your code in wrong. Here is the code at the moment.
Code: [Select]
int SpectrumLeft[6]; //Sets up the 6 frequencies I am analyzing
int ledintensity[6]; //sets up the first of 3 things I am storing the values
int ledintensity2[6]; //I am using three because I am still new at coding and I think in the end it should work
int ledintensity3[6];
int analyze1[6]; //this and the following are used to find if the values are similar
int analyze2[6];
int fadespeed; //sets up the speed at which the lights should fade after music stops playing
byte LED; //LED/LED2 set up how the program sends the data to each seperate ledintensity pin
byte LED2;
byte Band; //this is what the shield spectrum reader uses when it reads each frequency and then plugs it into SpectrumLeft[]

int led6 = 6; //the 6 different channels used for the lights... the order and color do not matter
int led7 = 3;
int led8 = 5;
int led9 = 9;
int led10 = 10;
int led11 = 11;

void setup () {
    pinMode(led6,OUTPUT);
    pinMode(led7,OUTPUT);
    pinMode(led8,OUTPUT);
    pinMode(led9,OUTPUT);
    pinMode(led10,OUTPUT);
    pinMode(led11,OUTPUT);
    pinMode(spectrumReset, OUTPUT);
    pinMode(spectrumStrobe, OUTPUT);
    int spectrumReset=2;
    int spectrumStrobe=4;
    int spectrumAnalog=0;
 
}
//Add a for loop so animation is smoother http://www.arduino.cc/en/Reference/For (this is all the basic code that the anaylzer needs)
void readSpectrum() {
  // Band 0 = Lowest Frequencies.
  for(Band=0;Band <6; Band++){
    SpectrumLeft[Band] = analogRead(0); //left
      digitalWrite(spectrumStrobe,LOW);
    delay(1);
  digitalWrite(spectrumReset,HIGH);
    delay(1);
  digitalWrite(spectrumStrobe,HIGH);
    delay(1);
  digitalWrite(spectrumStrobe,LOW);
    delay(1);
  digitalWrite(spectrumReset,LOW);
    delay(5);
    );     
  }
}

void same() {
  for(LED2=6; LED2 <12; LED2++){
      ledintensity[LED2]=analyze1[LED2]; //sets the values to be compared
      delay(1); //waits so we know if the values changed
      ledintensity[LED2]=analyze2[LED2]; //sets the second set from a later time
      if (analyze1[LED2]==analyze2[LED2]) { //sees if values are the same
        fadespeed = 75; //this will have it fade in the main loop
      }
       else {
        fadespeed = 0; //if not it will not fade
      }
   }
}

void fade() { //this is old code i may or may not use anymore just took it out for the time being
   for(LED2=6; LED2<12; LED2++)
    ledintensity3[LED2] = ledintensity2[LED2] - fadespeed; //sets the final value of the lights to be the value value from the loop minus the fade speed, and since the fadespeed was set in the fade function, if its 0, it will not change the value
  }
f
void loop () {
  for(LED=6; LED <12; LED++){
    for(Band=0;Band <6; Band++){
        for(LED2=6; LED2 <12; LED2++){
          readSpectrum; //runs the readSpectrum function to get the values
          ledintensity2[LED2]=(ledintensity[LED]+(25*SpectrumLeft[Band])); //this should in theory add the data of the analyzer to the base value for the lights, then send to the fade value
          fade; //runs the fade function
        }
      }
  }

    analogWrite(led6, ledintensity3[6]); //writes to the pins
    analogWrite(led7, ledintensity3[7]);
    analogWrite(led8, ledintensity3[8]);
    analogWrite(led9, ledintensity3[9]);
    analogWrite(led10, ledintensity3[10]);
    analogWrite(led11, ledintensity3[11]);
}

PaulS

Code: [Select]
void fade() { //this is old code i may or may not use anymore just took it out for the time being
   for(LED2=6; LED2<12; LED2++)
    ledintensity3[LED2] = ledintensity2[LED2] - fadespeed; //sets the final value of the lights to be the value value from the loop minus the fade speed, and since the fadespeed was set in the fade function, if its 0, it will not change the value
  }
f
void loop () {

f? Where did that come from?

techfreek

#8
Feb 27, 2011, 07:37 pm Last Edit: Feb 28, 2011, 03:33 am by techfreek Reason: 1
The f was a typo.... that is gone now. I got the code to compile... but it is still not analyzing hmmm...
bruce, you suggested that the analyzer was never initialized.. I just want to mention that this code;

Also any idea why the leds light up even though there is no signal being sent to them? (I have the ledintensity3[LED2] set to 0 in the beginning of void loop() and the only thing that is set to ledintensity3() is the values from the analyzer... (and this still happens when nothing is plugged into the audio cable)

Thanks!
Code: [Select]

digitalWrite(4,HIGH);  //Strobe pin on the shield
digitalWrite(4,LOW);    

Was code supplied to me by the guy who made the spectrum analyzer shield...

Well here is my current code in case its changed drastically... I will say I got my first changing of the lights! but it was just more of fading.... so I added at the beginning of every loop that so that the levels will be different (and not just increasing) every time it loops... I think by adding this, I dont need the fading functions so I just temporarily added a // infront of all of their code
Code: [Select]

int SpectrumLeft[6]; //Sets up the 6 frequencies I am analyzing
int ledintensity[6]; //sets up the first of 3 things I am storing the values
int ledintensity2[6]; //I am using three because I am still new at coding and I think in the end it should work
int ledintensity3[6];
int analyze1[6]; //this and the following are used to find if the values are similar
int analyze2[6];
int fadespeed; //sets up the speed at which the lights should fade after music stops playing
byte LED; //LED/LED2 set up how the program sends the data to each seperate ledintensity pin
byte LED2;
byte Band; //this is what the shield spectrum reader uses when it reads each frequency and then plugs it into SpectrumLeft[]

int spectrumReset=2;
int spectrumStrobe=4;
int spectrumAnalog=0;
int led6 = 6; //the 6 different channels used for the lights... the order and color do not matter
int led7 = 3;
int led8 = 5;
int led9 = 9;
int led10 = 10;
int led11 = 11;

void setup () {
   pinMode(led6,OUTPUT);
   pinMode(led7,OUTPUT);
   pinMode(led8,OUTPUT);
   pinMode(led9,OUTPUT);
   pinMode(led10,OUTPUT);
   pinMode(led11,OUTPUT);
   pinMode(spectrumReset, OUTPUT);
   pinMode(spectrumStrobe, OUTPUT);
   int spectrumReset=2;
   int spectrumStrobe=4;
   int spectrumAnalog=0;
 
}
//Add a for loop so animation is smoother http://www.arduino.cc/en/Reference/For (this is all the basic code that the anaylzer needs)
void readSpectrum() {
 // Band 0 = Lowest Frequencies.
 for(Band=0;Band <6; Band++){
   SpectrumLeft[Band] = analogRead(0); //left
 digitalWrite(spectrumStrobe,LOW);
   delay(1);
 digitalWrite(spectrumReset,HIGH);
   delay(1);
 digitalWrite(spectrumStrobe,HIGH);
   delay(1);
 digitalWrite(spectrumStrobe,LOW);
   delay(1);
 digitalWrite(spectrumReset,LOW);
   delay(5);
 }
}

//void same() {
 //for(LED2=6; LED2 <12; LED2++){
     //ledintensity[LED2]=analyze1[LED2]; //sets the values to be compared
    // delay(1); //waits so we know if the values changed
     //ledintensity[LED2]=analyze2[LED2]; //sets the second set from a later time
     //if (analyze1[LED2]==analyze2[LED2]) { //sees if values are the same
      // fadespeed = 75; //this will have it fade in the main loop
    // }
     // else {
     //  fadespeed = 0; //if not it will not fade
    // }
 // }
//}

//void fade() { //this is old code i may or may not use anymore just took it out for the time being
  //for(LED2=6; LED2<12; LED2++)
  // ledintensity3[LED2] = ledintensity2[LED2] - fadespeed; //sets the final value of the lights to be the value value from the loop minus the fade speed, and since the fadespeed was set in the fade function, if its 0, it will not change the value
//}
//}
void loop () {
 for(LED=6; LED <12; LED++){
   for(Band=0;Band <6; Band++){
       for(LED2=6; LED2 <12; LED2++){
         ledintensity3[LED2] = 0;
         readSpectrum; //runs the readSpectrum function to get the values
         ledintensity3[LED2]=(ledintensity[LED]+(25*SpectrumLeft[Band])); //this should in theory add the data of the analyzer to the base value for the lights, then send to the fade value
         //fade; //runs the fade function
       }
     }
 }

   analogWrite(led6, ledintensity3[6]); //writes to the pins
   analogWrite(led7, ledintensity3[7]);
   analogWrite(led8, ledintensity3[8]);
   analogWrite(led9, ledintensity3[9]);
   analogWrite(led10, ledintensity3[10]);
   analogWrite(led11, ledintensity3[11]);


techfreek

Hello all!
I made some updates to my code! I feel it is almost there! please tell me if you see anything that could be causing issues!
Thanks
Code: [Select]
int SpectrumLeft[6]; //Sets up the 6 frequencies I am analyzing
int ledintensity[6]; //sets up the first of 3 things I am storing the values
int analyze1[6]; //this and the following are used to find if the values are similar
int analyze2[6];
int fadespeed; //sets up the speed at which the lights should fade after music stops playing
byte LED; //LED/LED2 set up how the program sends the data to each seperate ledintensity pin
byte LED2;
byte Band; //this is what the shield spectrum reader uses when it reads each frequency and then plugs it into SpectrumLeft[]



int led7 = 3;
int led6 = 6; //the 6 different channels used for the lights... the order and color do not matter
int led9 = 9;
int led10 = 10;
int led11 = 11;
int led8 = 12;

void setup () {
    pinMode(led6,OUTPUT);
    pinMode(led7,OUTPUT);
    pinMode(led8,OUTPUT);
    pinMode(led9,OUTPUT);
    pinMode(led10,OUTPUT);
    pinMode(led11,OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(4, OUTPUT);
    //Init spectrum analyzer
}
void readSpectrum() {
  // Band 0 = Lowest Frequencies.
  for(Band=0;Band <6; Band++){
    SpectrumLeft[Band] = analogRead(0); //left
    digitalWrite(4,HIGH);  //Strobe pin on the shield
    digitalWrite(4,LOW);   
 

  }
}

void loop () {
  for(LED=6; LED <12; LED++){
    for(Band=0;Band <6; Band++){
        for(LED2=6; LED2 <12; LED2++){
          ledintensity[LED2]=0;
          readSpectrum; //runs the readSpectrum function to get the values
          ledintensity[LED2]=SpectrumLeft[Band]; //this should in theory add the data of the analyzer to the base value for the lights, then send to the fade value
        }
      }
  }

    analogWrite(led6, ledintensity[0]); //writes to the pins
    analogWrite(led7, ledintensity[1]);
    analogWrite(led8, ledintensity[2]);
    analogWrite(led9, ledintensity[3]);
    analogWrite(led10, ledintensity[4]);
    analogWrite(led11, ledintensity[5]);
}

Go Up