Hi all. I am making an experiment that has four possible conditions (LL, LR, RL, RR). I need each of these conditions to have 30 trials each but also be random (thus random, but equal trails among four conditions). I think there is something wrong with the logic of the code. It works for some combinations of trials for the four conditions but I believe I just got lucky on the combination of trials not hitting 30 before another combinations of LL, LR, RL, RR hit 30 trials. Thus the issue with the code is it will stop short of 120 trials when one or two conditions are met. Any ideas how to update this code to get all four conditions to be random but 30 equal trials? (p.s.; the beginning of the code gives low to high and high to low frequencies so participants know what a low and high frequency should feel like to make a response).
int viboutL = 5;
int viboutR = 6;
int ButtonL = 4;
int ButtonR = 7;
int buttonStateL;
int buttonStateR;
float time;
int run;
int count;
int NUM_TRIALS = 120;
int lower_limit = 0;
int upper_limit = NUM_TRIALS;
int zero = 0;
int max = NUM_TRIALS;
int LL = 0;
int LH = 0;
int RL = 0;
int RH = 0;
void setup()
{
int count = 0;
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(viboutL, OUTPUT);
pinMode(viboutR, OUTPUT);
pinMode(ButtonL, INPUT_PULLUP);
pinMode(ButtonR, INPUT_PULLUP);
run = 0;
}
void loop()
{
if ((digitalRead(ButtonL) == 0) && (digitalRead(ButtonR) == 0))
{
count = 0;
delay(500);
analogWrite(viboutL, 64);
analogWrite(viboutR, 64);
delay(2000);
analogWrite(viboutL, 0);
analogWrite(viboutR, 0);
delay(1000);
analogWrite(viboutL, 255);
analogWrite(viboutR, 255);
delay(2000);
analogWrite(viboutL, 0);
analogWrite(viboutR, 0);
delay(2000);
analogWrite(viboutL, 255);
analogWrite(viboutR, 255);
delay(2000);
analogWrite(viboutL, 0);
analogWrite(viboutR, 0);
delay(1000);
analogWrite(viboutL, 64);
analogWrite(viboutR, 64);
delay(2000);
analogWrite(viboutL, 0);
analogWrite(viboutR, 0);
delay(5000);
if (run == 0) // if run = 0 it will move onto next line
{
run = 255; // sets run to 255
}
else // if run did not = 0 then it goes to this line
{
run = 0; // sets run to 0 i.e. one can press both buttons again to start program
}
}
// if run is > 0
if (run > 0) // if run is higher than 0 then it will move onto the experiment
{
if (count < NUM_TRIALS)
{
delay(500);
int SIDE = random(lower_limit, upper_limit);
int LOHI = random(zero, max);
if (SIDE < NUM_TRIALS / 2)
{
lower_limit++;
Serial.print("0,");
Serial.print(count + 1);
Serial.print(",");
Serial.print("Left");
Serial.print(",");
float input_delay = random(0, 5000);
delay(input_delay);
time = micros();
if ((LOHI < NUM_TRIALS / 2) && LL < 30) {
zero++;
analogWrite(viboutL, 64);
Serial.print("Low");
Serial.print(",");
LL = LL + 1;
}
else if (LH < 30)
{
max--;
analogWrite(viboutL, 255);
Serial.print("High");
Serial.print(",");
LH = LH + 1;
}
else if ((LH >= 30) && (LL >= 30))
{return;}
// Keep track of if they got it wrong or not
int IncompatibleButton = 0;
while (digitalRead(ButtonL) == 1)
{
// While you are waiting on a left click check also for a right click. If they click right then
// remember that my setting the variable
if (digitalRead(ButtonR) == 0)
{
IncompatibleButton = 1;
// This magic keyword will force the while loop to end
break;
}
}
if (IncompatibleButton == 1)
{
analogWrite(viboutL, 0);
analogWrite(viboutR, 0);
Serial.print("Right Button");
Serial.print(",");
time = micros() - time;
Serial.print(time / 1000000, 6);
Serial.print(",");
Serial.println(input_delay/1000); //last thing printed for current trial
delay(2000);
}
else
{
// This is your currently existing happy path
analogWrite(viboutL, 0);
analogWrite(viboutR, 0);
Serial.print("Left button");
Serial.print(",");
time = micros() - time;
Serial.print(time / 1000000, 6);
Serial.print(",");
Serial.println(input_delay/1000); //last thing printed for current trial
delay(2000);
}
}
if (SIDE >= NUM_TRIALS / 2)
{
upper_limit--;
Serial.print("0,");
Serial.print(count + 1);
Serial.print(",");
Serial.print("Right");
Serial.print(",");
float input_delay = random(0, 5000);
delay(input_delay);
time = micros();
if ((LOHI < NUM_TRIALS / 2) && (RL < 30)){
zero++;
analogWrite(viboutR, 64);
Serial.print("Low");
Serial.print(",");
RL = RL + 1;
}
else if (RH < 30) {
max--;
analogWrite(viboutR, 255);
Serial.print("High");
Serial.print(",");
RH = RH + 1;
}
else if ((RH >= 30) && (RL >= 30))
{return;}
// Keep track of if they got it wrong or not
int IncompatibleButton = 0;
while (digitalRead(ButtonR) == 1)
{
// While you are waiting on a left click check also for a right click. If they click right then
// remember that my setting the variable
if (digitalRead(ButtonL) == 0)
{
IncompatibleButton = 1;
// This magic keyword will force the while loop to end
break;
}
}
if (IncompatibleButton == 1)
{
analogWrite(viboutR, 0);
analogWrite(viboutL, 0);
Serial.print("Left button");
Serial.print(",");
time = micros() - time;
Serial.print(time / 1000000, 6);
Serial.print(",");
Serial.println(input_delay/1000); //last thing printed for current trial
delay(2000);
}
else
{
// This is your currently existing happy path
analogWrite(viboutR, 0);
analogWrite(viboutL, 0);
Serial.print("Right button");
Serial.print(",");
time = micros() - time;
Serial.print(time / 1000000, 6);
Serial.print(",");
Serial.println(input_delay/1000); //last thing printed for current trial
delay(2000);
}
}
count = count + 1;
}
}
}