Klausj
Published © GPL3+

Checking Your Reaction Time

Yet another project to measure somebody’s reaction time. The test subject has to solve a simple math task before pressing a key.

BeginnerFull instructions provided652
Checking Your Reaction Time

Things used in this project

Story

Read more

Code

my Library for using the Multi Function Shield

Arduino
Install the zip library
No preview (download only).

reaction3.ino

Arduino
performs reaction time check
/*
  (C) 2021 Klaus J. Koch - Marburg
  prints easy calculation tasks on LEDs
  and records time for the answer.
  After 10 trials the average is displayed.
*/
#include <MFS3.h>
// texts:
// replace texts by words in your favourite language
char SOUND[] = "tone";
char YES[]   = "yes1";
char NO[]    = "no 2";
char HELLO[] = "HE\\o"; // there is a code for "LL"
char SUBTRACT[] = "subt";
char ERROR[]    = "Oops";
char TOO_LATE[] = "dead";
char GOOD[]     = "Good";
char BAD[]      = "bad ";
char CONTINUE[] = "cont";
char AVERAGE[]  = "avrg";

MFS3 mfs;
boolean sndOn = false;

void setup() {
  Serial.begin(9600);
  Serial.println(__FILE__);
  randomSeed(analogRead(A6));
  mfs.begin();
  sndOn = askSnd();
}

char buff[4];
int count = 1;
int cntOk = 0;
float sum = 0;

void loop() {
  mfs.D4(count & 1);
  mfs.D3(count & 2);
  mfs.D2(count & 4);
  mfs.D1(count & 8);
  mfs.print(HELLO, 1000); // welcome
  mfs.print(SUBTRACT);
  /* what is \\?
    this is the code 0x5C for
    double lower-case "L"
  */
  float now = second();
  float startTime = now + random(3, 10);
  boolean keyPressed = false;
  byte response = 0;
  while (second() < startTime) {
    response = mfs.S123();
    if (response) keyPressed = true;
  }
  // now the startTime is reached.
  if (keyPressed) {
    // pressed too early!!!
    mfs.print(ERROR, 1000);
    return;
    // new trial
  }
  // print request + sound on:
  int result = makeNewTask();
  mfs.print(buff);
  if (sndOn) mfs.tone(1);
  float tooLate = second() + 10;
  // wait for key:
  while (!response) {
    // maximum time exceeded?
    if (second() > tooLate) {
      mfs.tone(0);
      mfs.print(TOO_LATE, 5000);
      return;
    }
    response = mfs.S123();
  }
  // key has been pressed
  mfs.tone(0);
  float whenPressed = second();
  // calculate zhe difference:
  float reactionTime = whenPressed - startTime;
  // was the answer correct?
  if (response == (1 << result)) {
    mfs.print(GOOD, 1000);
    cntOk++;
    sum = sum + reactionTime;
  }
  else mfs.print(BAD, 1000);

  // show reactionTime:
  mfs.print(reactionTime, 1E3);
  // continue?
  mfs.print(CONTINUE, 2000);
  count++;
  if (count > 10) {
    // evaluation:
    mfs.print(GOOD, 1000);
    mfs.print(cntOk, 1000);
    if (cntOk > 0) {
      mfs.print(AVERAGE, 2000);
      float mean = sum / cntOk;
      mfs.print(mean, 5E3);
    }
    count = 1;
    cntOk = 0;
  }
}

boolean askSnd() {
  const char * msg[] = {SOUND, YES, NO};
  int i = 0;
  while (true) {
    mfs.print(msg[i]);
    // disp this msg for 1 second:
    unsigned long t = millis() + 1000;
    while (millis() < t) {
      switch (mfs.S123()) {
        case 2: return true;
        case 4: return false; 
      }
    }
    // next msg:
    if (++i > 2) i = 0;
  }
}

float second() {
  return millis() * 0.001;
}

int makeNewTask() {
  // new task: a - b = c
  int b = random(10);    // 0 to 9
  int c = random(3) + 1; // 1 to 3
  int a = b + c;         // 1 to 12
  if (a < 10) buff[0] = ' ';
  else buff[0] = '1';
  buff[1] = '0' + (a % 10);
  buff[2] = '-';
  buff[3] = '0' + b;
  // in buff the new task is stored
  return c;
}

Credits

Klausj
89 projects • 8 followers

Comments