I'm new to programming (and stackoverflow) so forgive me if I'm making mistakes posting this question. Currently trying to build a program in processing that basically allows you to measure how long it takes the user to press a key after a visual signal appears.
The user should be able to start the experiment by pressing the SPACE key. At a random time (between 2 and 6 seconds) the color of a field will change (e.g., black to red) – this is the stimulus. When the field changed to red, the program waits for the user to press the SPACE key and measures the time needed. The measured time is stored in an array and displayed on the screen. By pressing the key ‘a’ the experiment is ended and the results (e.g., average time and the standard deviation) are shown. By pressing the SPACE key again, a new experiment is started.
So far I've only managed to make a background that changes color at a exactly 6 seconds. This has to be between 2 and 6 seconds randomly or the stimulus won't appear randomly.
Can someone help me how to fix this?
Code:
int savedTime;
int totalTime = 6000;
void setup() {
size(200, 200);
background(0);
savedTime = millis();
}
void draw() {
// Calculate how much time has passed
int passedTime = millis() - savedTime;
// seconds passed?
if (passedTime > totalTime) {
println("... seconds have passed!");
background(255,0,0); // Color a new background
savedTime = millis(); // Save the current time to restart the timer
}
}
The problem is that variable totalTime
remains constantly at 6000
(milliseconds) so the user will always observe the same duration between the first keypress and the display change.
In order to add this behaviour, the following steps are needed:
Get a source of random.
With C language, easiest is to call the standard library function
rand()
.
This function returns a pseudo-random number, which is absolutely enough for such a toy application.
Assign the random value to totalTime
.
This must be done in function setup()
.
Add the remaining parts of the game. The actual measurement of the user reaction time until the key is pressed again, is still missing...