Quick background: I'm a graduate student in cognitive/experimental psychology (vision research, specifically). I'm trying to automate a pre-existing device. I need an experimenter to press a button to start a block of ten trials. Each trial consists of right or left being chosen for a servo randomly, the servo moving into position, a switch (for an external shutter mechanism) being thrown, a delay, the servo re-centering, and the Arduino waiting for either a right-button or left-button press. A bit of serial data is sent out, and the trial procedure repeats until ten have passed.
What I have gotten down:
Arduino waits for experimenter's button push.
Arduino generates random number, moves servo.
Arduino completes switch for the external shutter mechanism.
Delays.
Arduino re-centers servo.
Arduino outputs serial data.
What I don't have down:
Accepting right or left button pushes.
Waiting until the right or left button has been pushed before restarting the trial loop.
#include <Servo.h>
int triggerPin = 4;
int expPin = 2;
int rightPin = 7;
int leftPin = 8;
Servo myservo;
int eval=HIGH;
int randNumber;
int pos;
int rightval;
int rightval2;
int leftval;
int leftval2;
int trial;
int x;
void setup(){
pinMode(triggerPin, OUTPUT);
pinMode(expPin, INPUT);
Serial.begin(9600);
randomSeed(analogRead(5));
myservo.attach(9);
}
void loop(){
eval=digitalRead(expPin); //This section centers the servo
if(eval==LOW){ //Awaits the experimenter's
eval=HIGH; //button push, and begins a
myservo.write(90); //Set of ten trials.
trial=1;
delay(100);
for(trial=1; trial<11;){
randNumber=random(1000)%2;
pos=randNumber*180;
myservo.write(pos);
delay(500);
digitalWrite(triggerPin, HIGH);
delay(100);
digitalWrite(triggerPin, LOW);
delay(3000);
myservo.write(90);
delay(500);
[b]//Right about here is where I'd like the program to wait for either the right or left button to be pushed (rightPin or leftPin to be written as "LOW"),store that to a variable, and then continue to the code below.
Serial.print("Trial:"); Serial.print(trial);
Serial.print(",");
if(pos>90){Serial.println("R");}
if(pos<90){Serial.println("L");}
trial=trial+1;
}
}
}
I'd really appreciate any help or advice. I actually scaled this project back considerably from my initial goals (which, in retrospect, were way too extensive) but this is sort of the bare minimum of what I need. I'm admittedly stumped as to how I should go about this.
Thanks!
If anyone's actually interested in the psychometric procedure: My lab has a table set up to measure visual acuity using sine gratings and diffusion filters. The system is set up for a forced choice task (determining right or left tilt from center for the sine grating), but at the moment, right and left offset are done by the experimenter, who also writes down the subject's (verbal) response for their perception. This allows us to generate a contrast sensitivity function for a subject, which is useful to my research but incredibly laborious to collect data for, as this is pretty much all done by hand.