cgnuino
|
Emits asynchroneous digital-out. More...
#include <cgnuino.h>
Public Member Functions | |
CgnDO (byte, byte=1) | |
Consructor. More... | |
void | update () |
Lowers down the pins that finished determined time length of output. More... | |
void | out (byte, uint32_t) |
Starts putting out from a pin for determined time length. More... | |
Digital-out pins make Arduino boards emit digital signal, i.e., high (5V [or 3.3V]) or low (0V) voltage. These pins are normally turned on and off via Arduino's built-in digitalWrite
function. For example, if you want to flash an LED connected to a given pin for a second, you can achieve it with a simple code as below.
Since delay
function sleeps all the execution of your board for a given length (designated in millisecond), the LED will be extinguished 1000 ms after it is lighted. This way you can activate arbitrary external devices for any time length you want. However, as mentioned above, when you use delay
function, all the performance of your Arduino board is suspended until the designated time length has passed. This is problematic in controlling behavioral tasks. There are times that you want to turn on a given digital-out pin for a certain length, while you keep continuing other processes (e.g., monitoring subject's button press through digital-in pins). In other words, you will miss subject's button press and release while you are suspending your board using delay
function.
CgnDO class provides an easy way to overcome this problem. At construction, CgnDO class prepares multiple pins as digital outputs. Maximal number of pins for one instance of this class is determined by a constant N_CGNDO
. To put out high digital output, use out
method by designating both the index of the digital-out pin (but in count from the predetermined first output pin instead of the absolute pin number of the board) and the time length of the output in millisecond. This puts up the designated digital-out pin to high voltage. Then you need to repeatedly call update
method. This method checks if the required time has passed, and if so, turns off the pin to low voltage. In normal, this monitoring is achieved by calling update
method in the beginning of your main loop
function.
Because of the functional mechanism mentioned above, CgnDO class is accurate and makes sense only when your loop
is repeated in a reasonably frequent cycle. For example, if your loop
is run every 5 ms on average, the length of digital output from CgnDO class is accurate with maximal 5 ms deviation, which would be acceptable in standard psychological tasks. Note that you can easily monitor the frequency of your loop
by using CgnValtiel class. Also note that outputs from CgnDO class can be simultaneously done from multiple pins with respectively different time lengths. LchikaWave example will provide a simple example of this usage of CgnDO class.
CgnDO::CgnDO | ( | byte | firstPin, |
byte | numberOfOutputs = 1 |
||
) |
firstPin | First pin number for digital-out pins. |
numberOfOutputs | Number of digital outputs in use. |
void CgnDO::out | ( | byte | i, |
uint32_t | outputMs | ||
) |
i | Index of DO pin to emit digital output. |
outputMs | Time length of output in [ms]. |
void CgnDO::update | ( | ) |
loop
function.