|
| 1 | +/* |
| 2 | + FORCE example code |
| 3 | + by Lex Kravitz and Bridget Matikainen-Ankney |
| 4 | +
|
| 5 | + alexxai@wustl.edu |
| 6 | + May, 2021 |
| 7 | + |
| 8 | + KravitzLabDevices/FORCE_library is licensed under the GNU General Public License v3.0 |
| 9 | +
|
| 10 | + Permissions of this strong copyleft license are conditioned on making available complete source code of licensed works |
| 11 | + and modifications, which include larger works using a licensed work, under the same license. Copyright and license |
| 12 | + notices must be preserved. Contributors provide an express grant of patent rights. |
| 13 | + |
| 14 | +*/ |
| 15 | + |
| 16 | +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | +// DON'T EDIT THESE LINES |
| 18 | +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 19 | +#include <Force.h> //Include FORCE library |
| 20 | +String ver = "Force"; //unique identifier text |
| 21 | +Force force(ver); //start FORCE object |
| 22 | + |
| 23 | +int prob_left = 70; // Probability of pellet on the high-reward poke |
| 24 | +int prob_right = 30; // Probability of pellet on the low-reward poke |
| 25 | +int trialsToSwitch = 20; // # of trials before probabilities on the pokes switch |
| 26 | +int trial_counter = 0; // Tracks how many pellets have been obtained in this set of probabilities |
| 27 | +int highp_counter = 0; // Tracks how many pokes in a row in high probability poke |
| 28 | +int trialTimeout = 5; //timeout duration after each poke, set to 0 to remove the timeout |
| 29 | +int probs[5] = {10, 30, 50, 70, 90}; |
| 30 | +unsigned long trial_start = 0; |
| 31 | +unsigned long trial_length = 5000; |
| 32 | +bool trial_available = false; |
| 33 | +bool press = false; |
| 34 | + |
| 35 | +void setup() { |
| 36 | + force.begin(); //setup FORCE |
| 37 | + force.trial_window = 60000; |
| 38 | +} |
| 39 | + |
| 40 | +void loop() { |
| 41 | + |
| 42 | + /////////////////////////////////////////////////////////////////////// |
| 43 | + // This is the non-stationary part of the task /// |
| 44 | + // meaning that probabilities change every trialsToSwitch trials /// |
| 45 | + /////////////////////////////////////////////////////////////////////// |
| 46 | + force.run(false); |
| 47 | + if(trial_counter == trialsToSwitch) { |
| 48 | + highp_counter = 0; |
| 49 | + trial_counter = 0; |
| 50 | + prob_left = probs[random(0,5)]; |
| 51 | + prob_right = 100-prob_left; |
| 52 | + } |
| 53 | + |
| 54 | + else if (highp_counter == 8) { |
| 55 | + highp_counter = 0; |
| 56 | + trial_counter = 0; |
| 57 | + prob_left = probs[random(0,5)]; |
| 58 | + prob_right = 100-prob_left; |
| 59 | + } |
| 60 | + |
| 61 | + ///////////////////////////////////////////////////////////////// |
| 62 | + /// Here we start the task. Mouse has to poke on the left //// |
| 63 | + /// to start a trial, and then the bandit task starts //// |
| 64 | + /// within 5 seconds of the start of the tone /// |
| 65 | + //////////////////////////////////////////////////////////////// |
| 66 | + force.readPoke(); |
| 67 | + if (force.poke) { |
| 68 | + trial_start = millis(); |
| 69 | + force.Tone(); |
| 70 | + trial_available = true; |
| 71 | + } |
| 72 | + |
| 73 | + while (((millis()-trial_start) < force.trial_window) && trial_available == true) { |
| 74 | + force.ratioLeft = prob_left; |
| 75 | + force.ratioRight = prob_right; |
| 76 | + force.trials_per_block = trial_counter; |
| 77 | + force.FRC = highp_counter; |
| 78 | + force.library_version = trialsToSwitch; |
| 79 | + force.run(true); |
| 80 | + |
| 81 | + ///////////////////////////////////////////////////////////// |
| 82 | + //// If mouse presses the left lever /// |
| 83 | + ///////////////////////////////////////////////////////////// |
| 84 | + if ((force.pressLengthLeft > force.hold_timeLeft) && force.LeftActive) { |
| 85 | + if (prob_left > 50) { |
| 86 | + highp_counter ++; |
| 87 | + } |
| 88 | + else { |
| 89 | + highp_counter = 0; |
| 90 | + } |
| 91 | + delay(500); |
| 92 | + if (random(100) < prob_left) { |
| 93 | + force.Tone(); |
| 94 | + force.DispenseLeft(); |
| 95 | + trial_counter ++; |
| 96 | + } |
| 97 | + else { |
| 98 | + force.Tone(300,600); |
| 99 | + } |
| 100 | + press = true; |
| 101 | + trial_available = false; |
| 102 | + } |
| 103 | + |
| 104 | + ////////////////////////////////////////////////////////////// |
| 105 | + //// If mouse presses the right lever //// |
| 106 | + ///////////////////////////////////////////////////////////// |
| 107 | + if ((force.pressLengthRight > force.hold_timeRight) && force.RightActive) { |
| 108 | + if (prob_right > 50) { |
| 109 | + highp_counter ++; |
| 110 | + } |
| 111 | + else { |
| 112 | + highp_counter = 0; |
| 113 | + } |
| 114 | + delay(500); |
| 115 | + if (random(100) < prob_right) { |
| 116 | + force.Tone(); |
| 117 | + force.DispenseRight(); |
| 118 | + trial_counter ++; |
| 119 | + } |
| 120 | + else { |
| 121 | + force.Tone(300,600); |
| 122 | + } |
| 123 | + press = true; |
| 124 | + trial_available = false; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + ////////////////////////////////////////////////////////////// |
| 129 | + //// If there was no press during the time window //// |
| 130 | + ///////////////////////////////////////////////////////////// |
| 131 | + if (trial_available && press == false) { |
| 132 | + force.Tone(300,600); |
| 133 | + } |
| 134 | + |
| 135 | + ////////////////////////////////////////////////////////////// |
| 136 | + //// Finish trial and start inter-trial timeout //// |
| 137 | + ///////////////////////////////////////////////////////////// |
| 138 | + if (trial_available) { |
| 139 | + press = false; |
| 140 | + trial_available = false; |
| 141 | + force.Timeout(trialTimeout); |
| 142 | + force.Tone(); |
| 143 | + } |
| 144 | +} |
0 commit comments