Print to serial console from subroutine?

Hi, I am unable to print to serial console from a subroutine.
Is it possible ?

here is an example:

This program starts the serial console and listens for commands.
commands are:
reboot (reboots ESP32)
testl (prints a line from loop() )
tests (prints a line for subroutine)

Simulator:

here is the test code:

void TestPrintln(){
  Serial.println("Test from subroutine success.");
  }

void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200);
    Serial.println("Boot started.");
    Serial.println("----------------");
}

void loop() {
  //Check commands over serial  
  if (Serial.available())
  {
    String command = Serial.readStringUntil(10);

    if (command == "reboot"){ESP.restart();}
    else if (command == "testl") {Serial.println("Test from loop success.");}
    else if (command == "tests") {TestPrintln;}
    
  }
}

It is possible
Calling a function is done with parenthesis though… TestPrintln();

1 Like

I guess that the semicolon at the end of the testl line ends the command interpretation and attaches the following "else if..." to the outer if (Serial.available()).

Serial.readStringUntil() - Arduino Reference

Another way:

void loop()
{
  if (verbose) { Serial.print(F("Enter Instruction: ")); }
  Serial.readBytes( Inst, 4);
  Operation = Inst; Operation.trim();

Full code:
Don't Cross The Streams (FP scientific calculator serial co-processor) - Community / Exhibition / Gallery - Arduino Forum

Thanks, adding () did the trick ! :slight_smile:

Have fun

I would recommend to call strip() as in the simulator so that you don’t get hidden white space characters in your string

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.