Christmas lights to music

Hi everyone!

I hope this is the right place for this, if not, I will move it.

I have done all of the wiring for this project and I have run into an issue.

I am using the Arduino Uno.

All of the 3 digital outputs(6,7,8) are working for the channels.

However, the 5 analog outputs(A1-A5) do not work for the channels, so only 3 out of the 8 work.

I have looked through the code and have not found any issues.

I will attach it here.

Any suggestions or ideas?

Thanks in advance!

Kat

Christmas_Lights_2010.ino (5.31 KB)

What happens when you write a simple little test program that writes to the analog pins?

int channels[]={6,7,8,15,16,17,18,19};

I don't see where your code writes to the analog pinsl...

It looks like you should be connected to pins 15-19 instead of the analog pins. Are you using an Arduino Mega? (My Uno doesn't have those pins.) Or, is that a "trick" that I'm not aware of?

DVDdoug:
is that a "trick" that I'm not aware of?

Yes, you can refer to analog pins A0 to A6 as pins 14 to 19.

Try this change:

  volume = volume / 12;

I never saw these messages.

I apologize!

I will try the volume / 12 change right away.

Thanks, PaulRB!

-Kat

Hi,

However, the 5 analog outputs(A1-A5) do not work for the channels, so only 3 out of the 8 work.

Or do you mean 5 analog inputs?

Tom.... :slight_smile:

Yes, that. Whoops.

:confused:

The analog channels(15-19) will work if the volume is set to volume / 12, but then the digital channels(6-8) do not.

I will try to figure out a solution for this.

Thanks all!

Its not well written. Getting all the leds to work means having to play around with the "/ 30" and the "i +3" in doVuMeter() until you get it just right. It would be better if the sketch automatically adjusted to the levels on A0 and scaled those to use all 8 leds.

/*
 * This example plays every .WAV file it finds on the SD card in a loop
 */
#include "WaveHC.h"
#include "WaveUtil.h"

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the volumes root directory
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads

int channels[]={6,7,8,15,16,17,18,19};
int channelsLength = 8;
long volume;
long v2;

/*
 * Define macro to put error messages in flash memory
 */
#define error(msg) error_P(PSTR(msg))

// Function definitions (we define them here, but the code is below)
void play(FatReader &dir);

//////////////////////////////////// SETUP
void setup() 
{
  for (int i = 0; i < channelsLength; i++) 
  pinMode(channels[i], OUTPUT);

  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  
  putstring_nl("\nWave test!");  // say we woke up!
  
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(FreeRam());

  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)  
    error("Card init. failed!");  // Something went wrong, lets print out why
  }
  
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {   // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                           // we found one, lets bail
  }
  if (part == 5) {                     // if we ended up not finding one  :(
    error("No valid FAT partition!");  // Something went wrong, lets print out why
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    error("Can't open root dir!");      // Something went wrong,
  }
  
  // Whew! We got past the tough parts.
  putstring_nl("Files found (* = fragmented):");

  // Print out all of the files in all the directories.
  root.ls(LS_R | LS_FLAG_FRAGMENTED);
}

//////////////////////////////////// LOOP
void loop() 
{ 
  root.rewind();
  play(root);
  doVuMeter();
}

void doVuMeter(){
  Serial.print("VuMeter");
  volume = 0;
  for (int i=0; i < channelsLength; i++) {
    v2 = analogRead(0) - 350;
    if (v2 < 0) 
      v2 *= -1;
    if (v2 > volume)
      volume = v2;
    delay(5);
  }
  volume = volume / 30;
  //putstring("\n");Serial.print(volume);
  for (int i = 0; i < channelsLength; i++) {
    if (volume > i +3 ){
      digitalWrite(channels[i], HIGH);
    }
    else{
      digitalWrite(channels[i], LOW);
    }  
  }
}

/////////////////////////////////// HELPERS
/*
 * print error message and halt
 */
void error_P(const char *str)
{
  PgmPrint("Error: ");
  SerialPrint_P(str);
  sdErrorCheck();
  while(1);
}
/*
 * print error message and halt if SD I/O error, great for debugging!
 */
void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  PgmPrint("\r\nSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  PgmPrint(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}
/*
 * play recursively - possible stack overflow if subdirectories too nested
 */
void play(FatReader &dir)
{
  FatReader file;
  while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
  
    // Skip it if not a subdirectory and not a .WAV file
    if (!DIR_IS_SUBDIR(dirBuf)
         && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
      continue;
    }

    Serial.println();            // clear out a new line
    
    for (uint8_t i = 0; i < dirLevel; i++) {
       Serial.print(' ');       // this is for prettyprinting, put spaces in front
    }
    if (!file.open(vol, dirBuf)) {        // open the file in the directory
      error("file.open failed");          // something went wrong
    }
    
    if (file.isDir()) {                   // check if we opened a new directory
      putstring("Subdir: ");
      printEntryName(dirBuf);
      dirLevel += 2;                      // add more spaces
      // play files in subdirectory
      play(file);                         // recursive!
      dirLevel -= 2;    
    }
    else {
      // Aha! we found a file that isnt a directory
      putstring("Playing ");
      printEntryName(dirBuf);              // print it out
      if (!wave.create(file)) {            // Figure out, is it a WAV proper?
        putstring(" Not a valid WAV");     // ok skip it
      } else {
        Serial.println();                  // Hooray it IS a WAV proper!
        wave.play();                       // make some noise!

        uint8_t n = 0;
        while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
          doVuMeter();
//          delay(100);
        }       
        sdErrorCheck();                    // everything OK?
        // if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
      }
    }
  }
}