Im using an arduino to record reaction times of someone moving their hands to the brakes of a bike after seeing a visual stimulus using force sensors. The times recorded are the times each hand leaves the handle and the time each hand reaches the brake. Everything works fine so long as they move their right hand first. All of the times recorded show correctly. But, if they move their left hand first, the time overwrites until they move their right hand and it shows the same time for both initial reactions. I tried switching my code around for debugging to see if I could make the opposite occur (left overwriting right), but the same thing happened and the ordering did not change anything.
An example of the recorded times in each scenario:
If I take my right hand off first (works correctly):
L: initial time: 1.00; time to brake: 1.67
R: initial time: 0.98; time to brake: 1.59
If I take my left hand off first (works incorrectly, initial time overwrites):
L: initial time: 1.05; time to brake: 1.74
R: initial time: 1.05; time to brake: 1.80
Here is the code:
const int FSR_PIN = A1;
const int FSR_PIN2 = A0;
const int FSR_PIN3 = A3;
const int FSR_PIN4 = A2;
int firsttime = 1;
unsigned long tempTimeL = 0;
unsigned long tempTimeR = 0;
unsigned long startTime;
unsigned long pressTime;
unsigned long timeAt1L = 0;
unsigned long timeAt2L = 0;
unsigned long timeStampL = 0;
unsigned long timeStamp2L = 0;
unsigned long timeAt1R = 0;
unsigned long timeAt2R = 0;
unsigned long timeStampR = 0;
unsigned long timeStamp2R = 0;
int counterL = 0;
int counterR = 0;
int trialNum = 0;
void setup() {
Serial.begin(9600);
pinMode(FSR_PIN, INPUT);
pinMode(FSR_PIN2, INPUT);
pinMode(FSR_PIN3, INPUT);
pinMode(FSR_PIN4, INPUT);
pinMode(2, INPUT_PULLUP);// define pin two as input for push button
pinMode(10, OUTPUT);// defind pin 10 as output
digitalWrite(10, HIGH);
}
void loop() {
int button = digitalRead(2);
if((button == LOW) && ((counterL) == 0 && (counterR) == 0)){
digitalWrite(10,LOW);
if(firsttime == 1){
startTime = millis();
firsttime=0;
}
pressTime = millis()- startTime;
} else if (firsttime == 0){
firsttime = 1;
digitalWrite(10, HIGH);
}
if (pressTime > 1 && firsttime == 0){
tempTimeL = millis();
tempTimeR = millis();
startTime = tempTimeL - pressTime;
}
int fsrADC = analogRead(FSR_PIN);
int fsrADC2 = analogRead(FSR_PIN2);
int fsrADC3 = analogRead(FSR_PIN3);
int fsrADC4 = analogRead(FSR_PIN4);
if ((fsrADC > 10) || (fsrADC3 > 10) && (counterL == 0) && (counterR == 0) && (tempTimeL > 0) && (tempTimeR > 0)) {
if (fsrADC > 10) {
timeStampL = millis();
tempTimeL = 0;
timeAt1L = timeStampL - startTime;
}
if (fsrADC3 > 10) {
timeStampR = millis();
tempTimeR = 0;
timeAt1R = timeStampR - startTime;
}
}
if ((fsrADC > 10) || (fsrADC3 > 10) && (counterL == 0) && (counterR == 0) && (tempTimeL > 0) && (tempTimeR > 0)) {
if (fsrADC > 10) {
timeStampL = millis();
tempTimeL = 0;
timeAt1L = timeStampL - startTime;
}
if (fsrADC3 > 10) {
timeStampR = millis();
tempTimeR = 0;
timeAt1R = timeStampR - startTime;
}
}
if ((fsrADC > 10) && (counterL == 0) && (tempTimeL > 10)) {
timeStampL = millis();
timeAt1L = timeStampL - startTime;
}
if ((fsrADC < 3) && (counterL == 0) && (timeStampL > 10)) {
timeAt1L = timeStampL - startTime;
counterL = 1;
}
if ((fsrADC2 > 5) && (counterL == 1) && (timeStampL > 10)) {
timeStampL = 0;
timeStamp2L = millis();
timeAt2L = timeStamp2L - startTime;
counterL = 2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
if ((fsrADC3 > 10) && (counterR == 0) && (tempTimeR > 0)) {
tempTimeR = 0;
timeStampR = millis();
}
if ((fsrADC3 < 3) && (counterR == 0) && (timeStampR > 10)) {
timeAt1R = timeStampR - startTime;
counterR = 1;
}
if ((fsrADC4 > 5) && (counterR == 1) && (timeStampR > 10)) {
timeStampR = 0;
timeStamp2R= millis();
timeAt2R = timeStamp2R - startTime;
counterR = 2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
if ((counterL == 2) && (counterR == 2) ) {
Serial.print("Trial: ");
Serial.println(trialNum);
trialNum++;
Serial.print("L: ");
Serial.print(timeAt1L / 1000.0);
Serial.print(",");
Serial.println(timeAt2L / 1000.0);
Serial.print("R: ");
Serial.print(timeAt1R / 1000.0);
Serial.print(",");
Serial.println(timeAt2R / 1000.0);
counterL = 0;
counterR = 0;
}
}