Hi
Can any one help. I have programmed an intentional binding task (dot rotating round clock, changing colour 250 ms following keypress) in Python using Psychopy Builder which was fairly easy and it works fine. I wish to run the experiment online on Pavlovia and there is a particular problem with running a specific line of code - I have spent a lot of time trying to debug, but I have now run out of ideas (and so has chat GPT).
The JS code is below. The problem is that section 3a - the colour changing logic which is meant to make the dot change colour if the f key is pressed is not executing. The problem is not with the colour change logic, as it doesn’t work even if I replace that simply with a console log statement. Instead it seems to be that the if (key_pressed == ‘f’) not recognised at this point. I have tried all sorts, including nesting all of section 3 into section 2 to try to get it within the scope of the initializing logic, but that does not work either (that seems to prevent all of section 3 being executed).
Thanks for reading
//section1
// Get the current time
let current_time = t; // Assuming ‘t’ is the trial clock or frame time
//Define allowed keys (f and j key presses)
let allowed_keys = [‘f’, ‘j’];
let key_pressed = null
let keys = key_resp_12.getKeys({ keyList: allowed_keys });
//section 2
if (keys.length > 0 && !key_flag) { // Process key press only if no previous key press
key_pressed = keys[0].name;
console.log(allowedKey ${key_pressed} pressed.
);
console.log(key pressed at ${current_time} seconds.
);
//section 2a check key-pressed is registering correctly
if (key_pressed === ‘f’){
console.log(f pressed
);}
psychoJS.experiment.addData('key_pressed', key_pressed); // Record the key that was pressed
// Capture the current dot_3 position at the time of key press
let dot_x = dot_3.pos[0]; // Get dot_3's current x position
let dot_y = dot_3.pos[1]; // Get dot_3's current y position
let dot_distance = Math.sqrt(Math.pow(dot_x - center[0], 2) + Math.pow(dot_y - center[1], 2));
// Calculate angle of dot_3 relative to center
let dx = dot_x - center[0];
let dy = dot_y - center[1];
let angle_rad = Math.atan2(dy, dx); // Angle in radians
let angle_deg = angle_rad * (180 / Math.PI); // Convert to degrees
// Ensure adjusted_angle falls within the range [0, 360)
let adjusted_angle = (90 - angle_deg + 360) % 360; // Adjust for clock position (0 at 12 o'clock)
// Map angle to a clock position (1 to 59.5 in 0.5 degree increments)
let dot_position = adjusted_angle / 6; // Scale angle to fit between 0 and 60
let press_position = Math.round(dot_position * 2) / 2; // Round to nearest half degree
// Store dot_3's position and clock position
psychoJS.experiment.addData('press_position', press_position); // Store the clock position
console.log(`press position ${press_position}.`);
// Set the time when the color changes (750ms after wrong key press)
color_change_time = current_time + 0.250;
console.log(`ColorPR will change at ${color_change_time} seconds.`);
key_flag = true;
}
// section 3 colour change and set stop time
if (color_change_time && current_time >= color_change_time && !color_changed_flag) {
console.log(key_flag: ${key_flag}, color_changed_flag: ${color_changed_flag}, current_time: ${current_time}, color_change_time: ${color_change_time}
);
let dot_color_label = null; // Variable to hold the color name
// section 3a colour change
if (key_pressed == 'f') {
console.log(`fpressed so dot will change to green.`);
dot_3.fillColor = new util.Color([(-1), 1, (-1)]); // Green
dot_color_label = 'green';
}
psychoJS.experiment.addData('dot_color', dot_color_label);
console.log(`dot colour changed to ${dot_color_label}.`);
// Set stop_time after color change (between 0.8 and 1.3 seconds)
stop_time = current_time + Math.random() * (1.8 - 0.8) + 0.8;
console.log(`Stop time set to ${stop_time} seconds.`);
color_changed_flag = true;
}