Digital I/O Demo 5 -- Creating a response time game with the RESPONSEPixx ================================================================================== This demo showcases a game we usually run on demo nights at conferences. It tests reaction times using a beep and a RESPONSEPixx. The RESPONSEPixx must be plugged into the digital In of any of our devices. The game rules are as follow: - If there is a beep and a button lights up, press that button as fast as possible. - If there is no beep, you must not press the button. The beep is created using a sin wave: ``audioWave = sin([1:32]/32*2*pi);``. Since we are going to light up buttons on the RESPONSEPixx and it is plugged into digital inputs, we must change some of the pins to be treated as output pins by our device. ``Datapixx('SetDinDataDirection', hex2dec('1F0000'));``, sets up 5 pins to be treated as digital outputs (0x1F 00 00 is 0b0001 1111 0000 0000 0000 0000 0000 0000). We also enable debouncing on the button box. This makes it so that when you press a button, the vibration caused by the push does not trigger from the button box: ``Datapixx('EnableDinDebounce');``. To keep track of the button presses we use a logger built in the device. The logger keeps track of which inputs were triggered and a precise time-stamp of when the trigger happened ``Datapixx('SetDinLog');``. For every button press, you will get 2 data points, one for when the button was pressed and one for when it was released. We usually only care about the first value, but the 2nd can be useful. Data is acquired by first updating the local registers: ``Datapixx('RegWrRd');``, and then receiving the data as follows ``[data timetags] = Datapixx('ReadDinLog');``. The status of the digital in when every value is 0 will be (0x00FFFF), or all 1. When a button press happens, one the of digital ins will become zero. For red, we get 0x00FFFE, for green we get 0x00FFFB. You can always convert to binary in order to double check the values, as it is easier to understand a binary expression. To compare with a value, we usually use a bit-wise and operator. In Matlab, since we are handling values as decimals, we can just use the normal ``~=`` and ``=`` compare operators. .. literalinclude:: ../DatapixxToolbox/DatapixxDemos/DatapixxSimonGame.m :language: matlab :emphasize-lines: 34, 38-41, 45-48, 76, 92, 95, 103, 104, 112, 113, 136 :linenos: