0

Arduino + Wave Shield + LCD + RTC
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arduino + Wave Shield + LCD + RTC

by raphaelfc on Fri Sep 17, 2010 1:44 pm

Hi,

I already have the arduino, wave shield and lcd all conected and working just fine, but when i connect the RTC breakout board to it i get a bunch of garbage. So, i tried the arduino, lcd and rtc approach, which worked just fine. This problem only shows up when i add the wave shield. Has anyone experienced this before?

FYI:
LCD: Digital pins 6, 7, 8, 9, 14 (analog 0) and 15 (analog 1)
Wave Shield: No change (defined by original library code).
RTC pins: Analog pins 4 and 5

Here goes a link that may help you guys visualize the whole situation.
http://arduitter.blogspot.com/2010/09/w ... art-1.html
It's my project page.
raphaelfc
 
Posts: 7
Joined: Fri Sep 17, 2010 1:32 pm

Re: Arduino + Wave Shield + LCD + RTC

by adafruit on Fri Sep 17, 2010 4:34 pm

so you're def seeing the RTC and waveshield collide, right? does the waveshield still play with the RTC?

adafruit
 
Posts: 12135
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Arduino + Wave Shield + LCD + RTC

by raphaelfc on Mon Sep 20, 2010 12:57 pm

Yes, it does. I have just performed a test and the waveshield worked just fine.
raphaelfc
 
Posts: 7
Joined: Fri Sep 17, 2010 1:32 pm

Re: Arduino + Wave Shield + LCD + RTC

by adafruit on Mon Sep 20, 2010 2:50 pm

so, essentially, its the RTC that doesnt play nice with the LCD- not vice versa?

adafruit
 
Posts: 12135
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Arduino + Wave Shield + LCD + RTC

by raphaelfc on Mon Sep 20, 2010 4:39 pm

No. RTC and LCD are fine.
Here's the code i used to test this conbination:

Code: Select all | TOGGLE FULL SIZE
#include
#include "RTClib.h"
#include

RTC_DS1307 RTC;

LiquidCrystal lcd(6, 7, 8, 9, 14, 15);

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();

  if (!RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
 
  lcd.begin(16, 2);
}

void loop () {
    DateTime now = RTC.now();
   
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print(format(now.hour()));
    lcd.print(':');
    lcd.print(format(now.minute()));
    lcd.print(':');
    lcd.print(format(now.second()));
   
    lcd.setCursor(3, 1);
    lcd.print(format(now.day()));
    lcd.print('/');
    lcd.print(format(now.month()));
    lcd.print('/');
    lcd.print(now.year(), DEC);   
   
    delay(1000);
}

String format(int num) {
  String addZero = "0";
  String d = "";
 
  if (String(num).length() < 2){
    return addZero + String(num);
  }
 
  return String(num);
}

Available here: http://arduitter.blogspot.com/2010/09/w ... art-2.html

The problem is when i add the Wave Shield. Assuming it was just a typo (i don't know if this is the best word for this), that's exactly what is happening, the Wave Shield plays nice even with the RTC connected. But things don't go so well the other way around.

What just occurred to me is that i can test just the Wave Shield and the RTC and see how it goes. I'll be back soon with the results.
raphaelfc
 
Posts: 7
Joined: Fri Sep 17, 2010 1:32 pm

Re: Arduino + Wave Shield + LCD + RTC

by raphaelfc on Mon Sep 20, 2010 4:52 pm

Ok. It's getting complicated. I've just tested the RTC and Wave Shield combination and everything went just fine.

LCD + Wave Shield - OK
LCD + RTC - OK
RTC + Wave Shield - OK
LCD + Wave Shield + RTC - NOK

Here's the code:
Code: Select all | TOGGLE FULL SIZE
/*
 * This example plays every .WAV file it finds on the SD card in a loop
 */
#include "WaveHC.h"
#include "WaveUtil.h"

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

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

/*
 * 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()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging

  Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  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);

  DateTime now = RTC.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
}

/////////////////////////////////// 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
          putstring(".");
          if (!(++n % 32))Serial.println();
          delay(100);
        }       
        sdErrorCheck();                    // everything OK?
        // if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
      }
    }
  }
}
raphaelfc
 
Posts: 7
Joined: Fri Sep 17, 2010 1:32 pm

Re: Arduino + Wave Shield + LCD + RTC

by adafruit on Mon Sep 20, 2010 5:16 pm

right! OK. what does Free RAM report? what happens if you comment out just the println()'s in the main loop??

adafruit
 
Posts: 12135
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Arduino + Wave Shield + LCD + RTC

by raphaelfc on Tue Sep 21, 2010 10:45 am

So,

Running this last posted sketch FreeRam() gives me 431, running the sketch i'm getting garbage it gives me 54.

About commenting the println()'s i don't think it will help me since i'm getting garbage when i print on the lcd. If i comment them when not using the lcd how i'm going to know if i'm getting correct values?

Today i tested a bunch of random things such as moving code from one place to another, not initialize the card and so on. Results? None.
raphaelfc
 
Posts: 7
Joined: Fri Sep 17, 2010 1:32 pm

Re: Arduino + Wave Shield + LCD + RTC

by adafruit_support_bill on Tue Sep 21, 2010 11:09 am

You might be running out of memory - which could be causing the garbage on your display. I'd try commenting out the serial output as suggested to gain back a little space.

adafruit_support_bill
 
Posts: 62905
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino + Wave Shield + LCD + RTC

by adafruit on Tue Sep 21, 2010 12:38 pm

are you using a '328? you need to have a '328 :)

adafruit
 
Posts: 12135
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Arduino + Wave Shield + LCD + RTC

by raphaelfc on Tue Sep 21, 2010 4:31 pm

arduwino wrote:You might be running out of memory - which could be causing the garbage on your display. I'd try commenting out the serial output as suggested to gain back a little space.


That's the problem, i'm not using Serial.print() to output data, I print it on the LCD. I used print()'s in some codes i used for testing but it was the only way to get to know if it was retrieving the correct value or not.


adafruit wrote:are you using a '328? you need to have a '328 :)


328 it is.
raphaelfc
 
Posts: 7
Joined: Fri Sep 17, 2010 1:32 pm

Re: Arduino + Wave Shield + LCD + RTC

by adafruit_support_bill on Tue Sep 21, 2010 5:10 pm

That's the problem, i'm not using Serial.print() to output data, I print it on the LCD. I used print()'s in some codes i used for testing but it was the only way to get to know if it was retrieving the correct value or not.


But you have already demonstrated that the LCD code works with both the RTC and the Wave shield individually. So the diagnostic prints are now superfluous. And when you put them all together, you are running out of memory. So start getting rid of what you don't need anymore!

adafruit_support_bill
 
Posts: 62905
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino + Wave Shield + LCD + RTC

by raphaelfc on Wed Sep 22, 2010 9:23 am

You've got a point. But instead of starting getting rid of things i'll try to optimize my code and see if i gain some memory in return. BTW, isn't 54 enough to allow everything to work properly?
raphaelfc
 
Posts: 7
Joined: Fri Sep 17, 2010 1:32 pm

Re: Arduino + Wave Shield + LCD + RTC

by adafruit_support_bill on Wed Sep 22, 2010 9:51 am

54 bytes of RAM? That is the amount free at instant you call FreeRam(), and that is before your program starts doing any real work.

Unlike flash memory, RAM usage is dynamic. Subroutine calls require stack space, and the deeper they are nested, the more stack is required. 54 bytes is not a lot of headroom for the stack. If your stack overflows into your other volatile storage (such as strings), the results are unpredictable.

Here is a good description of memory usage and optimization: http://itp.nyu.edu/~gpv206/2008/04/making_the_most_of_arduino_mem.html

adafruit_support_bill
 
Posts: 62905
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino + Wave Shield + LCD + RTC

by cbonsig on Sun Mar 17, 2013 12:56 pm

4 years later, so this isn't a very timely suggestion. But the Internets never forget, so if somebody else finds this thread, here's what I did to solve a similar problem...

WaveHC.h sets the buffer size to 256kB if it thinks its running on a an ATmega168 chip (as with an older Arduino), or 512kB if its running on a ATmega328 or anything else (UNO, MEGA, or anything else. I added an override in WaveHC.h to force it to use the smaller buffer size, and that gave me enough freeRam to get everything working. I didn't notice a difference in performance or audio quality. Somewhere after line 44 or so, just add:

#define PLAYBUFFLEN 256UL

My version is here:
https://github.com/cbonsig/openclock/bl ... C/WaveHC.h

cbonsig
 
Posts: 13
Joined: Sat Jan 28, 2012 11:20 am

Please be positive and constructive with your questions and comments.