Serial communications in user-defined libraries

Hello,

Does anyone know how to use Serial.print("XXXX"); in their own libraries? I am having a hard time getting this to work, I've gone back & forth a number of time to no avail.

I can use softwareSerial to accomplish this task, but I would like to know why I cant use the standard Serial commands in my library.

I know its a mess... but heres my cpp file

include "HardwareSerial.h"

include "WProgram.h"

include "WConstants.h"

include "LCD.h"

include "wiring.h"

//#include "SoftwareSerial.h"

include "print.h"

LCD_2x16::LCD_2x16() {

Serial.begin(9600); }

void LCD_2x16::test() { digitalWrite(13,HIGH); delay(1000); digitalWrite(13,LOW); delay(1000);

Serial.print(0xFE,BYTE); Serial.print(0x01,BYTE); Serial.print("TESTING");

}

If you provide a brief description of the problem it would be most helpful.

Coding Badly,

Sorry for the garbage description, I am pressed for time here!!

Essentially, what I want to do is to make a library for my Sparkfun LCD screen with my own functions.

I want to include the standard Serial commands in the library, including:

Serial.print(x) Serial.println(x) Serial.being(9600) etc etc

How can I do this? What libraries to I have to include in my library?

I have tried including Wconstants, HardwareSerial, etc to no avail, the program craps out on me. No errors reports, just craps out.

Thanks,

#include “HardwareSerial.h”
#include “WProgram.h”
#include “WConstants.h”
#include “LCD.h”
#include “wiring.h”
//#include “SoftwareSerial.h”
#include “print.h”

LCD_2x16::LCD_2x16()
{

Serial.begin(9600);

/*
malaka.begin(9600);
Serial.print(0x7C);
/*
//delay(100);
Serial.print(0x7C,BYTE);
Serial.print(157,BYTE); //brighten LCD screen
delay(500);
_delayTime = 0;
*/
}

void LCD_2x16::test()
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);

Serial.print(0xFE,BYTE);
Serial.print(0x01,BYTE);
/*
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
malaka.print(0xFE,BYTE);
malaka.print(0x01,BYTE);
delay(1000);
malaka.print(“TESTING”);*/

}

/*
int LCD_2x16::start()
{
_delayTime = 0;

Serial.print(0xFE,BYTE);
Serial.print(0x01,BYTE);
Serial.print(“Testing”);

return 1;
}

/*

int LCD_2x16::start(long delayTime)
{
if(delayTime < 0)
{
_delayTime = 0;
return 0;
}
if(delayTime > 5000)
{
_delayTime = 5000;
}
else
{
_delayTime = delayTime;
}
return 1;

}

int LCD_2x16::clearLCD()
{
Serial.print(0xFE,BYTE); //Command String
Serial.print(0x01,BYTE); //Clear screen and go to col=1, row=1
delay(_delayTime);
return 1;
}

*/
LCD_2x16 LCD = LCD_2x16();

the program craps out on me.

That's a real shame. I guess. I have no idea what that means.

Does the program not compile? Does it compile but not do any thing? Does it make the LCD do weird things? If so, what weird things.

Being pressed for time is no reason to omit crucial details.

the program craps out on me. No errors reports, just craps out.

In these situations I've found a diaper to be an invaluable tool.

Maybe some questions will help you understand what we need to help you...

Does the Sketch compile? Are you able to upload the Sketch? When the Sketch runs, does it work at first then stop working? If yes, please define what you mean by "stop working". What makes you suspect "serial communications in user-defined libraries" is the problem?

Hmmm… I guess the best way to frame this question would be, how do I implement Serial commands in a library that I create.

I want to use functions like Serial.print( ) and Serial.begin(baud) in a library that I am writing. What is the best way to do this?

I ideally would like to only have to #include my library file and no other files. Let’s say my library is called LCD.h, I would like to have something like this:

#include <LCD.h>

unsigned long t = 0;

void setup()
{
LCD.start(baudrate);

}

void loop()
{

Sorry, I accidentally hit send.

What i’d like is

#include <LCD.h>

unsigned long t = 0;

void setup()
{
LCD.start(9600);
}

void loop()
{
t = millis();
LCD.clearScreen();
LCD.writeToScreen(t);
}

I am making up the “writeToScreen” function here… just as an example.

Thanks,

CodingBadly & PaulS,

I think the Serial communication is the culprit because I make the LED on pin13 (Duemilanove/328) blink before and after I tell the LCD screen to clear itself. The LED performs as expected, the screen on the otherhand… does nothing. The junk that was on the screen stays there. Here is the pde and library files:

Thanks again.

///////////////////////////////////////////////////////////////////////////
////////////////////////////
#include <LCD.h>

void setup()
{

}

void loop()
{
LCD.test();
}
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//LCD.cpp
#include “HardwareSerial.h”
#include “WProgram.h”
#include “WConstants.h”
#include “LCD.h”
#include “wiring.h”
//#include “SoftwareSerial.h”
#include “print.h”

LCD_2x16::LCD_2x16()
{
Serial.begin(9600);
}

void LCD_2x16::test()
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);

Serial.print(0xFE,BYTE);
Serial.print(0x01,BYTE);

digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}

LCD_2x16 LCD = LCD_2x16();

//////////////////////////////////////////////////////////////////
////// LCD.h
#ifndef LCD_h
#define LCD_h

#include “WProgram.h”

class LCD_2x16
{
public:
LCD_2x16();
void test();

private:
unsigned int _delayTime;

};

#endif

extern LCD_2x16 LCD;

In you sketch, you need to declare an instance of the LCD class.

LCD_2x16 myLCD;

Then, you can call methods on that object.

myLCD.test();

I don't know what this is doing:

LCD_2x16 LCD = LCD_2x16();

But, it probably is not doing what you think it is.

@PaulS:

I don't know what this is doing:

LCD_2x16 LCD = LCD_2x16();

I don't know if the same code is generated but it's functionally equivalent to this...

LCD_2x16 LCD;

@dimitri:

I suspect the problem has to do with initialization. Constructors for global objects are called BEFORE the hardware has been initialized. Try explicitly initializating LCD in setup...

Two changes to LCD.h...

class LCD_2x16
{
     public:
           [glow]// [/glow]LCD_2x16();
           [glow]void begin();[/glow]
           void test();
           
     private:
           unsigned int _delayTime;

};

A few changes to LCD.cpp...

[glow]/*[/glow]
LCD_2x16::LCD_2x16()
{
     Serial.begin(9600);
}
[glow]*/[/glow]

[glow]LCD_2x16::begin()
{
     Serial.begin(9600);
}[/glow]

Sketch...

void setup()
{
  [glow]LCD.begin();[/glow]
}

Please let us know if that helps.

When developing libraries - debugging is much easier if you put the “.cpp” and “.h” files in your sketch directory. The library source files will then show up as tabs in the IDE and compile time errors will be highlighted in the source. In your main sketch you will reference your library using the #include “MyLib.h” using quotes notation. When you’re done with developing, you can move the library into the library folder to make it accessible for other sketches.

To use Serial in a library, you just need to include a reference to the wiring (Arduino core definitions) file at the top of your “.cpp” file (#include <WProgram.h>). This will in turn include the hardware serial header file and a reference to the “Serial” class instance. You can call Serial.begin either from the main sketch itself or from within your library class constructor.

CodingBadly,

Your suggestion to put Serial.begin(9600) within a function other than the class constructor worked!! Thank You very much!!

BenF,

Thanks for your suggestion, I will do so in further development.

PaulS, The reason for:

LCD_2x16 LCD = LCD_2x16();

Is that I saw it in the file Wire.cpp (at the bottom of the file). I just copied what I saw and applied it to my library.

// Preinstantiate Objects //////////////////////////////////////////////////

TwoWire Wire = TwoWire();

Much Thanks to everyone!!