Giter Club home page Giter Club logo

Comments (1)

CreativeRobotics avatar CreativeRobotics commented on July 17, 2024

I'm not entirely clear what the issue is here.

myString is declared inside a handler, and is local to that handler - i.e. it isn't visible or useable to anything outside it.

If you declare it at the start of the sketch then you are making it a global variable, visible to everything, so you can't also declare the same variable in the handler -- If you want to use it outside the handler then comment out the declaration inside the handler otherwise the compiler sees two variable with the same name existing in the same 'name space' and it gets confused.

In the code you posted, the main loop repeatedly checks the variable myString, and it will contain whatever was in the last 'set strings' command.

If you want to access the payload that is in commander from the last command (not just the set strings command) then your code in loop should look like this:

String lastCommand=cmd.getPayloadString();

if(lastCommand=="stop"){
  Serial.println("Succes string");
}
else if(lastCommand=="go") {
  Serial.println("Succes go");
}
cmd.update();

But this isn't a good way to do things because if either of those strings are in the payload then it will print out the message over and over again as fast as it can until you send a command that doesn't have either of those words in the payload - Remember: the payload from the last command stays there in memory until it is overwritten when a new command arrives.

If your intention is to have an actual command called 'stop' and 'go' - so all you have to do is type stop or go - then make a new pair of handlers and commands. If you want to do something with the result in the main loop, use global flags to communicate the fact that a command has been handled:

First, add these lines to the command list:

{"stop",     stopHandler,    "Stop"},
{"go",     goHandler,    "Go"},

Next, add these two handlers:

bool stopHandler(Commander &Cmdr){
  Cmdr.println("STOP Command");
  stopCommand = true;
  return 0;
}

bool goHandler(Commander &Cmdr){
  Cmdr.println("GO Command");
  goCommand = true;
  return 0;
}

Now add these two lines above the setup() function (These are global variables so they are visible to everything in your whole sketch):

bool stopCommand = false;
bool goCommand = false;

Finally use this main loop (and put whatever else you need in there):

void loop() {
  cmd.update();
  if(stopCommand){
    stopCommand = false; //reset the flag so this only happens once for every command event
    Serial.println("Succes stop");
    //do other things that should happen when the stop command comes in ...
  }
  if(goCommand){
    goCommand = false; //reset the flag so this only happens once for every command event
    Serial.println("Succes go");
    //do other things that should happen when the go command comes in ...
  }
}

Remember, you can also put all the things you want to happen on a stop or go command inside the handler itself, and also call other functions from within the handlers.

from commander.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.