cgnuino
Public Member Functions | List of all members
CgnControl Class Reference

Communicates with external control apprication running on a secondary PC. More...

#include <cgnuino.h>

Public Member Functions

 CgnControl (char=10)
 Constructor. More...
 
String update ()
 Checks the serial buffer for a new input line.
 
int getCode ()
 Shows decomposed code for the last serial input.
 
String getValue ()
 Shows decomposed value for the last serial input.
 

Detailed Description

In normal psychological experiments in humans, no "manpowered" monitoring of task progression is usually required. Once the task is run for a given participant, values of the variables in the task will change by themselves, i.e., as you programmed in the code. However, especially in animal experiments, there are situations you come to wish for on-line modification of variable without stopping the task. For example, you may want to change the time length of certain task period or parameter, depending on the animal's temporary motivational state. You may also want to prepare more or less different task modes in a single Arduino sketch, and manually switch them pro re nata.

Although this is somewhat a difficult request for "compiled language" like Arduino, you can achieve it by sending command through a serial connection and receiving it via CgnControl class. CgnControl reads incoming serial text from Arduino's standard Serial using readStringUntil method. The received text is assumed to be composed of two elements, "code" and "value", separated by a colon (e.g., 1:2000, 2:3.14, or 5:trainingMode). A "code" is an integer which precede the colon, and can be used for conditional branching in your sketch (detailed example usage will be presented below). A "value" is an arbitrary text that succeeds the colon, and is the value you want to pass to Arduino. CgnControl decompose received text into these two elements by spliting it at intervened colon. By accessing these elements by getCode and getValue methods, you can prepare arbitrary conditional branches in your sketch.

int x;
float y;
String z;
ctrl.update();
switch (ctrl.getCode()) {
case 1:
x = ctrl.getValue().toInt();
break;
case 2:
y = ctrl.getValue().toFloat();
break;
case 5:
z = ctrl.getValue();
break;
}

In the example above, you can dynamically change the value of x to 2000 by sending 1:2000 through a serial. Similarly, sending 2:3.14 will set y as 3.14, and 5:trainingMode set z as "trainingMode". As this example shows, how the received value is used in conditional branches is completely up to you. In other words, the received value is simply left in String type, so you need to put appropriate type casting and explicitly determine which variable should store that value in each case branches.

If received text is composed only one character, CgnControl regards it as an ascii-coded integer. This allows you an easy conditional control on task's behavior by one-character serial emittion. For example, on receiving a text "A", CgnControl regards it as a "code" of 65 (ascii code of upper "A"). Samely sending "a" is received as a "code" of 97. Therefore, just typing "A" and "a", you can turn on and off the 13th GPIO pin in the example code below.

ctrl.update();
switch (ctrl.getCode()) {
case 65:
digitalWrite(13, HIGH);
break;
case 97:
digitalWrite(13, LOW);
break;
}

As this example clearly shows, you don't need to restrict the usage of CgnControl for variable modification. In fact, it offers an flexible mechanism with which you can perform arbitrary on-line conditional branching through serial interaction.

Examples:
Control.ino.

Constructor & Destructor Documentation

CgnControl::CgnControl ( char  e = 10)
Parameters
eEOL for serial inputs (by default \n).

The documentation for this class was generated from the following files: