Go Down

Topic: 20x4 LCD lib conflict (Read 366 times) previous topic - next topic

json

Feb 20, 2012, 02:57 pm Last Edit: Feb 20, 2012, 03:04 pm by json Reason: 1
Im using 20x4 lcd wid my uno. I get 2 analog values from temp sensor and current transformer (virtually im giving input wid a pot) and also read accelerometer via I2C (lsm303) all these values need to be logged in a micro SD (SFE Shield).

i initi lcd as LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

problem are
1. sometimes arduino resets randomly (rather i say its not entering loop())  this happens only when i run this prog
2.or i get a running(i donn how to say tis)  like its printing too fast on 1st & 2nd line  (its not the blacked out boxes)

please help i ran a eg program from lib  and also some i wrote(that inclueds only lcd lib) they run w/o any probs ....
my guess is its conflicting wid sd or i2c
i also tried external power 12/700mA, thinking that reseting ll b due to insuffice power

Quote


#include <SD.h>
#include <Wire.h>
#include <LSM303.h>
#include <LiquidCrystal.h>

LSM303 compass;
File myFile;

const int chipSelect = 8;
static int h,m,s,ms;
int sensor,ct,ON_OFF=9;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void dc(float d=3)
 {
   delay(d*1000);
   lcd.clear();   
 }
 
void lp(String s , int x=0, int y=0)
{
  lcd.setCursor(x,y);
  lcd.print(s);
}

void setup()
{
 
   lcd.begin(20, 4); // lcd init
      
   lp("Initializing microSD",0,0);
   pinMode(10, OUTPUT);
   if (!SD.begin(chipSelect))
    {
       lp("Card not present...",0,1);
       return;
    }
  lp("Card Initialized...",0,1);
  
  Wire.begin();
  compass.init();
  compass.enableDefault();
  lp("Compass Initialized ",0,2);
  
  dc();//clr
  
  if(SD.exists("data"))
   {
     lp("Data folder exists  ",0,0);
   }
  else
   {
     lp("Data doesn't exists ",0,0);
     lp("Creating data folder",0,1);
     SD.mkdir("data");
//     myFile = SD.open("data");
//     myFile.close();
   } 
  
  if (SD.exists("data/log.lvm"))
   {
     lp("data/log.lvm exists.",0,2);
   }
  else
   {
     lp("log.lvm doesn't exist",0,2);
     lp("Creating data/log.lvm",0,3);
      myFile = SD.open("data/log.lvm", FILE_WRITE);
      myFile.close();
   }
   
   dc(); 
   pinMode(ON_OFF,INPUT); 
   

}


void loop()
{
  
   if(digitalRead(ON_OFF))
   {
       
       lp("Data Being Logged   ",0,0);
       lp("Temp: ",0,1);
       lp("Iph:",7,1);
       compass.read();
      
       ms = (millis()%1000)/100;
       s = (millis()/1000)%60;
       m = (millis()/60000)%60;
       h = (millis()/3600000)%24;
       String dataString = String(h)+':'+String(m)+':'+String(s)+'.'+String(ms);
      //  lp(dataString,0,2);
        
        for (ct = 0; ct < 3; ct++)
        {        
            if(ct==0)
            {
              sensor = (int)compass.a.x;
            }
            else if(ct==1) 
            {
              sensor = (int)compass.a.y;
            }
            else
            {
              sensor = (int)compass.a.z;
            }           
              
            dataString += ",";
            dataString += String(sensor);
        }
      
           
        File myfile = SD.open("data/log.lvm", FILE_WRITE);
         if (myfile)
          {
            myfile.println(dataString);
            myfile.close();
            Serial.println(dataString);
          }     
         else
          { 
            dc();     
            lp(" Error opening file ",0,1);
            dc(10);
            return;
          } 
       
      delay(200);
       
   }
   else if(!digitalRead(ON_OFF)) 
   {
           myFile = SD.open("data");
            
             if(myFile)
              {
                 if(SD.remove("data/log.lvm"))
                  {
                     dc();
                     lp("log.lvm removed...  ");
                     return;
                  }
                 else
                  {
                     dc();
                     lp("File doesn't exists...");
                     return;
                  }                  
              }
            myFile.close(); 
        delay(500);     
   }
   
}

 

any help is appreciated...
json

jfhaugh

Sounds like a heap-stack collision and the Uno is being rebooted or hanging because its memory is getting corrupted.

You're asking that little box to do a lot.  It has all of 2K SRAM.

Re-check all of your memory allocations and "flatten" out any allocations you're doing.  For example, don't have nested function calls.  That is, don't do this --

void readCurrent() {
  int value = 0;
  setupCurrentSensor();
  value = getCurrent();
  writeData(value);
}

Do this --

int value;

value = readCurrent();
writeData(value);

What's been so fun about using Arduinos is that all the problems we had to solve 35 years ago are being discovered all over again.  My first computer had 1K SRAM.  AND I WAS HAPPY WITH IT!  :smiley-mr-green:

liudr

Please use the code button when posting code, not the quote button.

Is this what you have?

http://www.sparkfun.com/products/9802

Consider posting links to all your hardware next time.

To test jfhaugh's theory, replace all nice texts such as "Initializing microSD" with much shorter things like "01". You may be running too many things together, serial port, sd card, lcd, and accelerometer.

Some info on optimizing memory usage:

http://liudr.wordpress.com/2011/02/04/how-to-optimize-your-arduino-memory-usage/

json

thank you both of u ... i hav workd wid AVR but am new to arduino fame work ...
thanks i hav solved it ...
just tell me wether i can run this code in mega with out any pobs...

json

liudr

Mega has more memory so you can afford to have those strings again. Do consider PROGMEM approach to remove those strings from SRAM. On MEGA, I2C bus is not on analog 4 and 5 any more so make sure you connect it right (20,21?)

Go Up