Receive.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // *** Receive ***
  2. // This 1st example will make the PC toggle the integrated led on the arduino board.
  3. // It demonstrates how to:
  4. // - Define commands
  5. // - Set up a serial connection
  6. // - Receive a command with a parameter from the PC
  7. #include <CmdMessenger.h> // CmdMessenger
  8. SYSTEM_MODE(SEMI_AUTOMATIC);
  9. // Blinking led variables
  10. bool ledState = 0; // Current state of Led
  11. const int kBlinkLed = D7; // Pin of internal Led
  12. const int motorUp = A0;
  13. const int motorDown = A4;
  14. const int motorLeft = A6;
  15. const int motorRight = A2;
  16. // Attach a new CmdMessenger object to the default Serial port
  17. CmdMessenger cmdMessenger = CmdMessenger(Serial);
  18. // We can define up to a default of 50 cmds total, including both directions (send + receive)
  19. // and including also the first 4 default command codes for the generic error handling.
  20. // If you run out of message slots, then just increase the value of MAXCALLBACKS in CmdMessenger.h
  21. // This is the list of recognized commands. These can be commands that can either be sent or received.
  22. // In order to receive, attach a callback function to these events
  23. //
  24. // Note that commands work both directions:
  25. // - All commands can be sent
  26. // - Commands that have callbacks attached can be received
  27. //
  28. // This means that both sides should have an identical command list:
  29. // both sides can either send it or receive it (or even both)
  30. // Commands
  31. enum
  32. {
  33. kSetLed, // Command to request led to be set in specific state
  34. kSetMotor,
  35. };
  36. // Callbacks define on which received commands we take action
  37. void attachCommandCallbacks()
  38. {
  39. cmdMessenger.attach(kSetLed, OnSetLed);
  40. cmdMessenger.attach(kSetMotor, OnSetMotor);
  41. }
  42. // Callback function that sets led on or off
  43. void OnSetLed()
  44. {
  45. // Read led state argument, interpret string as boolean
  46. ledState = cmdMessenger.readBoolArg();
  47. // Set led
  48. digitalWrite(kBlinkLed, ledState?HIGH:LOW);
  49. }
  50. void OnSetMotor()
  51. {
  52. String motorDirection = cmdMessenger.readStringArg();
  53. bool state = cmdMessenger.readBoolArg();
  54. int intensity = LOW;
  55. if(!state){
  56. intensity = HIGH;
  57. }
  58. if(motorDirection == "Up"){
  59. digitalWrite(motorUp, intensity);
  60. }
  61. else if(motorDirection == "Down"){
  62. digitalWrite(motorDown, intensity);
  63. }
  64. else if(motorDirection == "Left"){
  65. digitalWrite(motorLeft, intensity);
  66. }
  67. else if(motorDirection == "Right"){
  68. digitalWrite(motorRight, intensity);
  69. }
  70. }
  71. // Setup function
  72. void setup()
  73. {
  74. RGB.control(true);
  75. pinMode(motorUp,OUTPUT);
  76. pinMode(motorRight,OUTPUT);
  77. pinMode(motorDown,OUTPUT);
  78. pinMode(motorLeft,OUTPUT);
  79. digitalWrite(motorUp, HIGH);
  80. digitalWrite(motorRight, HIGH);
  81. digitalWrite(motorDown, HIGH);
  82. digitalWrite(motorLeft, HIGH);
  83. // Listen on serial connection for messages from the PC
  84. // 115200 is the max speed on Arduino Uno, Mega, with AT8u2 USB
  85. // Use 57600 for the Arduino Duemilanove and others with FTDI Serial
  86. Serial.begin(115200);
  87. // Adds newline to every command
  88. cmdMessenger.printLfCr();
  89. // Attach my application's user-defined callback methods
  90. attachCommandCallbacks();
  91. // set pin for blink LED
  92. pinMode(kBlinkLed, OUTPUT);
  93. }
  94. // Loop function
  95. void loop()
  96. {
  97. // Process incoming serial data, and perform callbacks
  98. cmdMessenger.feedinSerialData();
  99. }