Several "begin.serial" statments in setup?

Hi group, I am developing a sketch on a MEGA2560 that is using an IIc LCD, SDcard, 1302 RTC, and Soon NRFL420..
Im noticing all of the samples of code for each, have their own begin.serial(xxxx) statements with different bauds.. ect..
i also noticed i have commented out all but one, and all is working so far.. can anybody tell me how/why, and an overview
of what happens at what level when begin.serial is declared/called, that all of these can be satisfied with one call..

The baud rate is set for the arduino by setting some registers to appropiate value

Code in - C:\Program Files (x86)\arduino-0022\hardware\arduino\cores\arduino => harwareSerial.cpp explains it all

So changing the baudrate while in a sketch is perfectly possible, if it makes sense is another issue - OK you could make a scrambled serial communication -

Do you mean Serial.begin? Not begin.serial? Perhaps post the problem code.

If you have two devices that talk to Arduino via a serial interface you have to initialize two Serial objects, and pass begin() the correct baud rate required by each device. The MEGA has 4 hardware serial ports, plus one can have many others via SoftwareSerial library.

I’m not sure what you mean when you say the sketch works even if you comment out all but one Serial ports… Stated this way it doesn’t make much sense. Perhaps something more could be said after seeing the actual code. :slight_smile:

@Nick sorry… “Serial.begin(xxxxx)”

@else, the first Serial.begin was installed with the IIc lcd, the second one down by the SD initializer was in fact supposed to be for the SDcard, but, i commented it out sometime ago and have not had any problems writing to the SDcard or the LCD.

Lotta trimming to do, alot more questions about serial instances, not only wireless coming soon, but also RS232/485 ports, I will read through anything you point me to that may help me grasp how to handle these things before it gets out of hand… :slight_smile: :slight_smile:

      #include <SD.h>
      #include <ModbusMaster.h>
      #include <DS1302.h>
      #include <LiquidCrystal_I2C.h>
      #include <Wire.h>
      //detect arduino
      #if defined(ARDUINO) && ARDUINO >= 100
      #define printByte(args)  write(args);
      #define printByte(args)  print(args,BYTE);
      int PageNum = 1;           //Current Page
      int MenuItem = 0;          //Currnet Selected Menu
      int ModDeviceNumber = 0;   //Current Modbus Device
      String dataString = "";    //String holder for SDcard Data 
      char* WorkDay = "";        //name holder for DateStamp
      char* WorkFile = "";       //name holder for SDcard File Writes 
      long Dbounce;              //Debounce Timer
      long InactiveTime;         //Inactive Timer
      long LastWrite;            //last SD write in millis 
      long Periodic = 10000;     //frequency constant for SD writes
      int ADevice0 = A0;         //
      int ADevice0Value = 0;
      double Mult = .0000;             //offset of static temp value for calculations
      //setup input buttons
      const int LButton = 24;          //setup "hot pin" for LEFT 
      const int RButton = 25;          //setup "hot pin" for RIGHT 
      const int UButton = 26;          //setup "hot pin" for UP
      const int DButton = 27;          //setup "hot pin" for DOWN
      const int EButton = 28;          //setup "hot pin" for ENTER
      const int ARDLED = 13;
      //SDcard CS Pin
      const int chipSelect = 53;       //must, even if you dont use CS
      //setup LCD and associated variables  
      LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
      const int LightPin = 22;          //setup "hot pin" for lcd backlight   
      int backlightState = 0;           //flag for current backlight state  ?? may not be necessary
      //long backlightTimer = 50000;      //value for backlight timer
      long RefreshDisplay = 1000;       //value for refreshing display  
      //describe special characters
      uint8_t bell[8]  = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
      uint8_t note[8]  = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
      uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
      uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
      uint8_t duck[8]  = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
      uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
      uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
      uint8_t retarrow[8] = {0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
      uint8_t frame[8]  = {0x1f,0x0,0x0,0x0,0x0,0x0,0x1f};
      uint8_t bar1[8] = {0x1f,0x10,0x10,0x10,0x10,0x10,0x1f};
      uint8_t bar2[8] = {0x1f,0x18,0x18,0x18,0x18,0x18,0x1f};
      uint8_t bar3[8] = {0x1f,0x1c,0x1c,0x1c,0x1c,0x1c,0x1f};
      uint8_t bar4[8] = {0x1f,0x1e,0x1e,0x1e,0x1e,0x1e,0x1f};
      uint8_t bar5[8] = {0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f};
      uint8_t bar6[8] = {0x1f,0x11,0x1b,0x1f,0x1b,0x11,0x1f};
      uint8_t framer[8] = {0x1f,0x01,0x01,0x01,0x01,0x01,0x1f};
      // Init the DS1302 Real Time Clock
      DS1302 rtc(5, 6, 7);
      //Declare types for data //create object arrays for items with multiple redings  
      double Va1 = 2.05;
      double Va2 = 2.02;
      double Ia1 = 16.07;
      double Ia2 = 12.08;
      double Wa1 = 0.00;
      double Wa2 = 0.00;
      double Vb1 = 24.00;
      double Vb2 = 24.00;
      double Ib1 = 10.00;
      double Ib2 = 20.00;
      //int interval = 1;       //minutes between samples from Classic

 void setup()
        Serial.begin(57600);             //begin talking to the LCD
        pinMode(LightPin, INPUT);       //set i/o mode for backlight switch hotpin
        pinMode(LButton, INPUT);
        pinMode(RButton, INPUT);
        pinMode(UButton, INPUT);
        pinMode(DButton, INPUT);
        pinMode(EButton, INPUT);
        //pinmode(ADevice, INPUT); 
        lcd.init();                      // initialize the lcd 
        lcd.createChar(0, bell);         //declare special characters for LCD
        lcd.createChar(1, note);
        lcd.createChar(2, clock);
        lcd.createChar(3, heart);
        lcd.createChar(4, duck);
        lcd.createChar(5, check);
        lcd.createChar(6, cross);
        lcd.createChar(7, retarrow);
        lcd.createChar(0, bar1);         //declare special characters for LCD
        lcd.createChar(1, bar2);
        lcd.createChar(2, bar3);
        lcd.createChar(3, bar4);
        lcd.createChar(4, bar5);
        lcd.createChar(5, frame);
        lcd.createChar(6, bar6);
        lcd.createChar(7, framer);
        // Set the RealTimeClock to run-mode, and disable the write protection
          // The following should be commented out to use the values already stored in the DS1302
          //rtc.setDOW(MONDAY);        // Set Day-of-Week to FRIDAY
          //rtc.setTime(22, 56, 10);     // Set the time to 12:00:00 (24hr format)
          //rtc.setDate(05,03, 2012);   // DD,MM,YYYY Set the date to ...
        //Setup SD Card
        lcd.print("Initializing SD..");
        // make sure that the default chip select pin is set to
        // output, even if you don't use it:
        pinMode(53, OUTPUT);
        pinMode(10, OUTPUT);
        // see if the card is present and can be initialized:
        if (!SD.begin(chipSelect)) {
          lcd.print("Card failed, or not present");
          // don't do anything more:
        lcd.print("card initialized.");

Just a quick reply:

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display

The LCD is controlled via I2C, therefore the following comment:

Serial.begin(57600);             //begin talking to the LCD

is wrong.

Also, the SD cart is controlled via SPI, not via a serial port as in Serial. Don’t confuse the adjective “serial” applied to interface or protocol, of which there are many, with “Serial” Arduino class, which controls USART (hardware serial peripheral). Then there’s SoftwareSerial, which is a library which emulates a hardware serial (USART) using normal digital pins.

Hmmmmm… Very Good, so actually, altho im calling an instance of serial… there is no serial going on… I wonder why it was in the sample code for the libraries i DL’d for the hardware? I have more to learn, this is great… Thanks All!

ahhh, yes.. in the examples i used to guide my code/learning.. those folk were using "serial.print" to output status/debugging.. so, thats where i picked it up from.. since i was using IIC lcd, i should have just deleted those calls.. Im about to embark into serial for use in wireless and rs-232. Im sure ill have some valid questions about serial, and IRQ's, Ill of course read up as much as i can..

Many Thanks All