josephcesario
New member
Hi everyone:
I have what I imagine is a really basic and easy question about timing, so my apologies if the answer is obvious. What follows is a description of how and why I'm doing what I'm doing, but the basic question boils down to: Am I doing whatever I can to get the best timing resolution, in terms of how quickly my host computer reads signals from the Teensy? Or am I missing something obvious that I should do to make the host computer register information from the Teensy more quickly?
I'm using Teensy 3.2 (which is great!) to register how quickly someone pushes a button after seeing an image. The basic setup is simple: A software program (PsychoPy) displays an image on the computer screen. A person has to push a mechanical button as soon as they see the image. The software program registers the button press and provides the timing by linking the button press with when the image was presented.
I use the Teensy to capture the button press by connecting the two terminals on the mechanical button to the ground and the digital pin on the Teensy (pin 5 in the code below). As you can gather from the code, when the person presses the button, the Teensy sends that the Key Z has been pressed. The software program reads this as the keyboard Z key having been pressed and registers the time it took the Z key to be pressed relative to the start of displaying the image, thereby giving me the time it took the person to respond.
**So the software program displays an image, the person presses a button, the Teensy registers that and says that the Z key has been pressed, and the software program reads that the Z key was pressed and tells me how long it took the person to respond.**
(sidenote: I'm basically using the Teensy to pretend that it's a keyboard because the software program can easily read keypresses and does not as easily read other kinds of inputs that the Teensy might be able to provide.)
My understanding is that usb devices have some variable amount of timing lag because the host computer polls the usb device every X milliseconds, not continuously. If the person presses a keyboard key at 2 milliseconds after the image appears, but the computer doesn't poll the usb device for another 5 milliseconds, I will incorrectly see that it took the person 7 milliseconds to respond instead of 2. I'm trying to use the Teensy to avoid this kind of delay, if at all possible.
My question is, is the code below (which works perfectly well) missing something? Is the code sending the information from the Teensy to the host computer as soon as it's obtained or is there some other way of doing this? Basically, I want the host computer to be able to read the Z keypress from the Teensy absolutely as soon as possible.
Again, my apologies if there is an obvious answer here. I checked the existing documentation as best I could and I *think* I have it right, but I'm not sure.
Thank you!
CODE:
bool notCurrentlyPressed = true;
void setup() {
Serial.begin(38400);
pinMode(5, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(5) == LOW) {
if (notCurrentlyPressed == true) {
Keyboard.set_key1(KEY_Z);
Keyboard.send_now ();
notCurrentlyPressed = false;
}
}
else {
if (notCurrentlyPressed == false) {
Keyboard.release (KEY_Z);
notCurrentlyPressed = true;
}
}
delay(50);
}
I have what I imagine is a really basic and easy question about timing, so my apologies if the answer is obvious. What follows is a description of how and why I'm doing what I'm doing, but the basic question boils down to: Am I doing whatever I can to get the best timing resolution, in terms of how quickly my host computer reads signals from the Teensy? Or am I missing something obvious that I should do to make the host computer register information from the Teensy more quickly?
I'm using Teensy 3.2 (which is great!) to register how quickly someone pushes a button after seeing an image. The basic setup is simple: A software program (PsychoPy) displays an image on the computer screen. A person has to push a mechanical button as soon as they see the image. The software program registers the button press and provides the timing by linking the button press with when the image was presented.
I use the Teensy to capture the button press by connecting the two terminals on the mechanical button to the ground and the digital pin on the Teensy (pin 5 in the code below). As you can gather from the code, when the person presses the button, the Teensy sends that the Key Z has been pressed. The software program reads this as the keyboard Z key having been pressed and registers the time it took the Z key to be pressed relative to the start of displaying the image, thereby giving me the time it took the person to respond.
**So the software program displays an image, the person presses a button, the Teensy registers that and says that the Z key has been pressed, and the software program reads that the Z key was pressed and tells me how long it took the person to respond.**
(sidenote: I'm basically using the Teensy to pretend that it's a keyboard because the software program can easily read keypresses and does not as easily read other kinds of inputs that the Teensy might be able to provide.)
My understanding is that usb devices have some variable amount of timing lag because the host computer polls the usb device every X milliseconds, not continuously. If the person presses a keyboard key at 2 milliseconds after the image appears, but the computer doesn't poll the usb device for another 5 milliseconds, I will incorrectly see that it took the person 7 milliseconds to respond instead of 2. I'm trying to use the Teensy to avoid this kind of delay, if at all possible.
My question is, is the code below (which works perfectly well) missing something? Is the code sending the information from the Teensy to the host computer as soon as it's obtained or is there some other way of doing this? Basically, I want the host computer to be able to read the Z keypress from the Teensy absolutely as soon as possible.
Again, my apologies if there is an obvious answer here. I checked the existing documentation as best I could and I *think* I have it right, but I'm not sure.
Thank you!
CODE:
bool notCurrentlyPressed = true;
void setup() {
Serial.begin(38400);
pinMode(5, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(5) == LOW) {
if (notCurrentlyPressed == true) {
Keyboard.set_key1(KEY_Z);
Keyboard.send_now ();
notCurrentlyPressed = false;
}
}
else {
if (notCurrentlyPressed == false) {
Keyboard.release (KEY_Z);
notCurrentlyPressed = true;
}
}
delay(50);
}