Skip to content

4. Triggering

raffaelemazziotti edited this page Apr 22, 2023 · 3 revisions

If you require synchronization with other equipment for your experiments, a simple solution is available that can trigger events automatically. This solution is based on Arduino DUE boards and requires only basic coding knowledge. As of now, MEYE only accepts manual pushbutton triggers, but we were able to use Arduino DUE to create a fake keyboard that can translate digital events to push button events.

This setup can be used to connect MEYE to a stimulation computer (via LPT port, for example) or a response box. To manage events automatically, the Arduino DUE and the keyboard library can be used. This library can emulate a keyboard using specific Arduino boards such as DUE, Leonardo, Esplora, etc. Additionally, we used the Bounce2 library to mitigate any possible noise issues.

Arduino keyboard

This image shows simple wiring to implement automatic triggering events using the following digital pins:

DIGITAL Keyboard
3 q
4 w
5 e
6 r

This is the code to load on your Arduino DUE board:

#include <Bounce2.h>  // install this library from the library manager
#include <Keyboard.h> // import the keyboard emulation library

// 4 Channels/Buttons initialization
Bounce btn1 = Bounce(); 
Bounce btn2 = Bounce(); 
Bounce btn3 = Bounce(); 
Bounce btn4 = Bounce(); 

void setup() {
   Serial.begin(9600); // Serial communication initialization (not used here but still usefull)
   Keyboard.begin(); // Keyboard emulation initialization (this is required to send keyboard events to the PC)

   btn1.attach(3,INPUT_PULLUP);  // digital pin 3 is assigned to button1, INPUT_PULLUP means that the channel is 5V as default and the event is triggered when 0V is sent to the channel.
   btn1.interval(25); // Use a debounce interval of 25 milliseconds

   btn2.attach(4,INPUT_PULLUP); 
   btn2.interval(25); 

   btn3.attach(5,INPUT_PULLUP); 
   btn3.interval(25); 

   btn4.attach(6,INPUT_PULLUP); 
   btn4.interval(25); 

}

void loop() {

   btn1.update(); // update button status
   btn2.update();
   btn3.update();
   btn4.update();

   /////////////////// btn1
   if (btn1.fell()) // when the button goes from (5V to 0V)
   {
      Keyboard.press('q'); // trigger
   }

   if (btn1.rose()) // when the button goes from (0V to 5V)
   {
      Keyboard.release('q'); // release the trigger
   }
 
   /////////////////// btn2 
   if (btn2.fell())
   {
      Keyboard.press('w'); 
   }

   if (btn2.rose())
   {
      Keyboard.release('w');
   }

   /////////////////// btn3
   if (btn3.fell())
   {
      Keyboard.press('e'); // trigger
   }

   if (btn3.rose())
   {
      Keyboard.release('e');
   }

   /////////////////// btn4
   if (btn4.fell())
   {
      Keyboard.press('r'); // trigger
   }

   if (btn4.rose())
   {
      Keyboard.release('r');
   }
 
}
Clone this wiki locally