Serial.begin seems to have issues?

Hi Spark Team,
Using Serial.begin seems to have some issues. The moment I comment out Serial.begin(), code works as expected.
Can you please let me know if this is some known issue or am I doing something wrong ?

In detail:
I want to use Serial port to print out debugging messages. So I do Serial.begin(9600) in setUp()
After this, spark list command in spark client does not show any functions listed eventhough I have registered 2 functions using Spark.function()

I comment out the line Serial.begin(9600) and code works fine.
spark list is able to list the functions which I am registering via Spark.function()

I tested this behavior on WebIDE as well as on my local machine setup. Observed same behavior.

Below is my code:


/* Includes ------------------------------------------------------------------*/  
#include "application.h"

/* Function prototypes -------------------------------------------------------*/
int drive_dir(String Cmd); //This function will be exported to Spark cloud
int tilt_dir(String Cmd); // This function will be exported to Spark cloud
int MoveFront(void);
int MoveBack(void);
int MoveLeft(int degree);
int MoveRight(int degree);

//Pin naming
uint16_t M11 = A0;
uint16_t M12 = A2;
uint16_t M21 = A1;
uint16_t M22 = A3;

SYSTEM_MODE(AUTOMATIC);

/* This function is called once at start up ----------------------------------*/
void setup()
{
    //Serial setup for debugging
    //Serial.begin(9600);
    
    //Setup Pin modes
    pinMode(M11, OUTPUT);
    pinMode(M12, OUTPUT);
    pinMode(M21, OUTPUT);
    pinMode(M22, OUTPUT);
    
    //Initialize pins with default values.
    /*
     *      A0  A2  Result
     *      0   0   Motor Off
     *      0   1   direction 
     *      1   0   opposite direction
     *      1   1   Motor Off
     */
    analogWrite(M11, 0);
    digitalWrite(M12, 0);
    analogWrite(M21, 0);
    digitalWrite(M22, 0);
    
    //Register all the Spark cloud functions.
    Spark.function("drive", drive_dir);
    Spark.function("tilt", tilt_dir);
    
    Serial.print("\nsetup complete.");
 }

/* This function loops forever --------------------------------------------*/
void loop()
{
    //Nothing here in loop() for now. 
}

int drive_dir(String Cmd){
    Serial.print(Cmd);
    return 111;
}

int tilt_dir(String Cmd){
    Serial.print(Cmd);
    return 222;
}

That’s super weird. I’ll test it out and report what I see. :wink:

@AnupKavari, the short term solution now is to call the Serial.begin() last.

so shift it down after the Spark.function().


I’m going to ping @satishgn and @mdma with regards to this.

1.) Calling Serial.begin() before Spark.function() somehow does not register the functions

2.) Calling Serial.begin() after Spark.function() for now works fine.

2 Likes

Thank you for the information @kennethlimcp.
This helps :slight_smile:

1 Like