// *** Receive *** // This 1st example will make the PC toggle the integrated led on the arduino board. // It demonstrates how to: // - Define commands // - Set up a serial connection // - Receive a command with a parameter from the PC #include <CmdMessenger.h> // CmdMessenger // Blinking led variables bool ledState = 0; // Current state of Led const int kBlinkLed = 13; // Pin of internal Led // Attach a new CmdMessenger object to the default Serial port CmdMessenger cmdMessenger = CmdMessenger(Serial); // We can define up to a default of 50 cmds total, including both directions (send + receive) // and including also the first 4 default command codes for the generic error handling. // If you run out of message slots, then just increase the value of MAXCALLBACKS in CmdMessenger.h // This is the list of recognized commands. These can be commands that can either be sent or received. // In order to receive, attach a callback function to these events // // Note that commands work both directions: // - All commands can be sent // - Commands that have callbacks attached can be received // // This means that both sides should have an identical command list: // both sides can either send it or receive it (or even both) // Commands enum { kSetLed, // Command to request led to be set in specific state }; // Callbacks define on which received commands we take action void attachCommandCallbacks() { cmdMessenger.attach(kSetLed, OnSetLed); } // Callback function that sets led on or off void OnSetLed() { // Read led state argument, interpret string as boolean ledState = cmdMessenger.readBoolArg(); // Set led digitalWrite(kBlinkLed, ledState?HIGH:LOW); } // Setup function void setup() { // Listen on serial connection for messages from the PC // 115200 is the max speed on Arduino Uno, Mega, with AT8u2 USB // Use 57600 for the Arduino Duemilanove and others with FTDI Serial Serial.begin(115200); // Adds newline to every command cmdMessenger.printLfCr(); // Attach my application's user-defined callback methods attachCommandCallbacks(); // set pin for blink LED pinMode(kBlinkLed, OUTPUT); } // Loop function void loop() { // Process incoming serial data, and perform callbacks cmdMessenger.feedinSerialData(); }