Newer
Older
TestStandRepository / Software / Arduino / libraries / Arduino-Libraries / CmdMessenger / Examples / DataLogging / DataLogging.ino
  1. // *** DataLogging ***
  2.  
  3. // This example expands the previous SendandReceiveArguments example.
  4. // The Arduino will now wait for the StartLogging command, before sending analog data to the PC
  5. // and sent multiple float values in scientific format.
  6.  
  7. #include <CmdMessenger.h> // CmdMessenger
  8.  
  9. // Attach a new CmdMessenger object to the default Serial port
  10. CmdMessenger cmdMessenger = CmdMessenger(Serial);
  11.  
  12. // Thermocouple pins
  13. const int AnalogPin1 = 0;
  14. const int AnalogPin2 = 1;
  15. bool acquireData = false;
  16. const unsigned long sampleInterval = 100; // 0.1 second interval, 10 Hz frequency
  17. unsigned long previousSampleMillis = 0;
  18. long startAcqMillis = 0;
  19.  
  20. // This is the list of recognized commands. These can be commands that can either be sent or received.
  21. // In order to receive, attach a callback function to these events
  22. enum
  23. {
  24. // Commands
  25. kAcknowledge , // Command to acknowledge that cmd was received
  26. kError , // Command to report errors
  27. kStartLogging , // Command to request logging start (typically PC -> Arduino)
  28. kPlotDataPoint , // Command to request datapoint plotting (typically Arduino -> PC)
  29. };
  30.  
  31. // Commands we send from the PC and want to receive on the Arduino.
  32. // We must define a callback function in our Arduino program for each entry in the list below.
  33.  
  34. void attachCommandCallbacks()
  35. {
  36. // Attach callback methods
  37. cmdMessenger.attach(OnUnknownCommand);
  38. cmdMessenger.attach(kStartLogging, OnStartLogging);
  39. }
  40.  
  41. // ------------------ C A L L B A C K S -----------------------
  42.  
  43. // Called when a received command has no attached function
  44. void OnUnknownCommand()
  45. {
  46. cmdMessenger.sendCmd(kError,"Command without attached callback");
  47. }
  48.  
  49. // Callback function that responds that Arduino is ready (has booted up)
  50. void OnArduinoReady()
  51. {
  52. cmdMessenger.sendCmd(kAcknowledge,"Arduino ready");
  53. }
  54.  
  55. // Callback function calculates the sum of the two received float values
  56. void OnStartLogging()
  57. {
  58. // Start data acquisition
  59. startAcqMillis = millis();
  60. acquireData = true;
  61. cmdMessenger.sendCmd(kAcknowledge,"Start Logging");
  62. }
  63.  
  64. // ------------------ M A I N ----------------------
  65.  
  66. // Setup function
  67. void setup()
  68. {
  69. // Listen on serial connection for messages from the pc
  70. Serial.begin(115200);
  71.  
  72. // Adds newline to every command
  73. cmdMessenger.printLfCr();
  74.  
  75. // Attach my application's user-defined callback methods
  76. attachCommandCallbacks();
  77.  
  78. // Send the status to the PC that says the Arduino has booted
  79. cmdMessenger.sendCmd(kAcknowledge,"Arduino has started!");
  80. }
  81.  
  82. // Returns if it has been more than interval (in ms) ago. Used for periodic actions
  83. bool hasExpired(unsigned long &prevTime, unsigned long interval) {
  84. if ( millis() - prevTime > interval ) {
  85. prevTime = millis();
  86. return true;
  87. } else
  88. return false;
  89. }
  90.  
  91. // Loop function
  92. void loop()
  93. {
  94. // Process incoming serial data, and perform callbacks
  95. cmdMessenger.feedinSerialData();
  96. // Do measurement after certain sample interval
  97. if (hasExpired(previousSampleMillis,sampleInterval))
  98. {
  99. if (acquireData) {
  100. measure();
  101. }
  102. }
  103. }
  104.  
  105. // simple readout of two Analog pins.
  106. void measure() {
  107. float seconds = (float) (millis()-startAcqMillis) /1000.0 ;
  108. float Analog1 = analogRead(AnalogPin1);
  109. float Analog2 = analogRead(AnalogPin2);
  110. cmdMessenger.sendCmdStart(kPlotDataPoint);
  111. cmdMessenger.sendCmdArg(seconds,4);
  112. cmdMessenger.sendCmdSciArg(Analog1);
  113. cmdMessenger.sendCmdSciArg(Analog2);
  114. cmdMessenger.sendCmdEnd();
  115. }